Problem/Motivation
I'm using a bundle class to override the toUrl()
method on a particular node type. In particular, there's a link field that, if populated, will be used by the toUrl()
method. The reason is that we want to allow cards to link to internal or external links (or no link whatsoever) as elegantly as possible. The code looks like this:
public function toUrl($rel = 'canonical', array $options = []) {
if ($rel === 'canonical') {
if (!empty($this->field_link->uri)) {
return Url::fromUri($this->field_link->uri);
}
else {
return Url::fromRoute('<nolink>');
}
}
return parent::toUrl($rel, $options);
}
I can successfully create such a node and the behavior of toUrl
is as hoped.
Unfortunately, editing the node is not so smooth. The path module throws an erro:
UnexpectedValueException: Unrouted URIs do not have internal representations. in Drupal\Core\Url->getInternalPath() (line 798 of core/lib/Drupal/Core/Url.php).
Drupal\path\Plugin\Field\FieldType\PathFieldItemList->computeValue() (Line: 33)
....
When I fix that by hacking core a bit then I get to the next exception:
UnexpectedValueException: Unrouted URIs do not have internal representations. in Drupal\Core\Url->getInternalPath() (line 798 of core/lib/Drupal/Core/Url.php).
Drupal\path\Plugin\Field\FieldWidget\PathWidget->formElement(Object, 0, Array, Array, Object) (Line: 19)
....
When I fix that then everything is grand.
These exceptions happen even if the url alias element is not on the node form display for this node type.
Steps to reproduce
See above
Proposed resolution
The path module needs to check that $entity->toUrl()->isRouted()
in addition to its existing checks for !$entity->isNew()
in a couple places.
Remaining tasks
Tips for manual testing
Apply the test-only patch.
Enable entity_test
, path
, and path_entity_test_external
. (To enable test modules, add $settings['extension_discovery_scan_tests'] = TRUE;
to your settings.php.)
Then go to /entity_test_external/add
and create and edit these entities. See the errors.
Then apply the fix and see that creating and editing these entities works. And everything should still work for nodes and media, etc.
User interface changes
N/A
API changes
TBD
Data model changes
N/A
Release notes snippet
TBD