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.