Empty node path in views causes 500 error

Created on 11 December 2017, about 7 years ago
Updated 26 February 2023, almost 2 years ago

Hi everyone!
Recently I ran into a problem on one of my websites. I made a view with non-mandatory relationship with parent node entity, referenced from entity reference field. I tried to display paths of parent nodes, but some of entities has no parent node and paths must be empty. But instead of some empty fields I got 500 error with message:

Symfony\Component\Routing\Exception\InvalidParameterException: Parameter "node" for route "entity.node.canonical" must match "\d+" ("" given) to generate a corresponding URL. in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 202 of /var/www/html/www/core/lib/Drupal/Core/Routing/UrlGenerator.php).

I dig into this problem and mentioned the following code in /core/modules/node/src/Plugin/views/field/Path.php

public function render(ResultRow $values) {
    $nid = $this->getValue($values, 'nid');    
    return [
      '#markup' => \Drupal::url('entity.node.canonical', ['node' => $nid], ['absolute' => $this->options['absolute']]),
    ];
  }

It's easy to guess the source of this problem - there is no option for empty field. So I added the following lines to this method:

if (!$nid) {
      return '';
    }

And now it works. Also with this code I can use standard views empty field handler as usual.
I hope this small solution could help anyone.
Patch will be attached below in the first comment.

🐛 Bug report
Status

Closed: outdated

Version

9.5

Component
Node system  →

Last updated 4 days ago

No maintainer
Created by

🇧🇟Belarus gun_dose

Live updates comments and jobs are added and updated live.
  • views

    Involves, uses, or integrates with views. In Drupal 8 core, use the “VDC” tag instead.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand quietone

    @gun_dose, Thank you for reporting this problem and supplying a patch. We rely on issue reports like this to improve Drupal core.

    I tested this on Drupal 10.1.x, standard install. I added a node reference field to the Page content type and used devel_generate to make content. I then made a view as described in the Issue Summary. All the nodes had an entry in the reference field, so I removed it from one of the page node. The view displayed without error.

    Therefore, closing as outdated. If this is incorrect reopen the issue, by setting the status to 'Active', and add a comment explaining what still needs to be done.

    Thanks!

Production build 0.71.5 2024