There are a few circumstances when dealing with entities with the special computed path field, where if the field is accessed, some exceptions can be thrown. Instead of throwing exceptions, we should appropriately return an empty (NULL) value for the field.
The path field can be added to an entity type definition manually. Pathauto also adds this field to entities in \pathauto_entity_base_field_info().
Scenarios, when $entity->path is read:
- If entity doesnt implement 'canonical' link template,
\Drupal\Core\Entity\Exception\UndefinedLinkTemplateException is thrown.
- If canonical URL is not routable, usually when URL is external.
If entity is unsaved, \Drupal\Core\Entity\EntityMalformedException is thrown. (may not be an issue since path list already checks isNew).
Need to catch potential exceptions thrown in \Drupal\path\Plugin\Field\FieldType\PathFieldItemList::computeValue, by toUrl and getInternalPath
E.g
// Throws \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException|\Drupal\Core\Entity\EntityMalformedException
$url = $entity->toUrl();
// Throws \UnexpectedValueException
$url->getInternalPath();
Comments
Comment #2
dpiComment #3
dpiComment #4
jibranComment #5
jibranNew patch on top of #2336597: Convert path aliases to full featured entities
Comment #6
jibranReroll as per #2336597-173: Convert path aliases to full featured entities
Comment #8
jibranReroll after #3084983: Move all the code related to path aliases to a new (required) "path_alias" module.
Comment #9
dpi#8 is missing class imports.
Comment #10
jibranThanks updated the patch.
Comment #11
ravi.shankar commentedComment #12
dpiAdded tests
Comment #14
jibranThanks, for adding the tests. Patch looks good just some minor observations.
We don't need this.
FYI: this is what linky module is doing as well.
Why the first one is also not asserNull?
s/URLS/URLs
Comment #15
dpiThanks @jibran, feedback is good and addressed.
Comment #16
dpiExpanded issue summary
Comment #17
dinesh18 commentedI have checked the interdiff and patch manually looks good to me. Implemented as per #14.
Let's wait for the patch to pass the automated tests
Comment #19
dpiTests pass / fail as expected.
Comment #20
jibranThanks, for addressing the feedback.
Let's add some comments here to explain why and in which case these exceptions will be thrown.
Comment #21
dpiEach of these three exceptions is thrown directly by the called methods
toUrlandgetInternalPath, which document these exceptions, not deeper down the stack. I think this request would be a case of over-documentation.Comment #22
jibranThat makes sense. I was just rerolling the patch for the most part so I think it is fair to RTBC the patch.
Comment #23
jibranAnother reroll.
Comment #24
alexpottOne thing that's missing from the issue summary is why entity types that have the special path computed field. In core
path_entity_base_field_info()adds the field to'taxonomy_term', 'node', 'media'entity types but none of them have the problems outlined in the issue summary. So why are entity types having this path field added?Comment #25
dpi@alexpott the field can be added manually, or more likely due to the popular Pathauto project. Summary updated.
Comment #26
alexpottSo I guess the question then is why would you enable pathauto on an entity type that would cause this. Which entity types get the path field is user configurable.
It feels like that is a configuration error though and something pathauto should try and detect and tell the user about.
I worry that this change will hide helpful error messages when a developer is creating custom code.
Comment #28
dpiReroll on 9.0.x and removes core version per #3072702: Core extensions should not need to specify the new core_version_requirement in *.info.yml files so that 9.0.x can be installed
Comment #31
kim.pepperComment #32
joachim commented> If entity doesnt implement 'canonical' link template, \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException is thrown.
> If canonical URL is not routable, usually when URL is external.
Couldn't these problems be picked up on entity type rebuild, and exceptions thrown at that point? That would actually improve DX: better to have the exception thrown when you enable the module or do drush cr than when you view an entity.
> I worry that this change will hide helpful error messages when a developer is creating custom code.
I agree.
Comment #34
joachim commented> If entity doesnt implement 'canonical' link template, \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException is thrown.
Don't let the field be added to this entity type.
An exception should be thrown at SOME point to alert a developer to the problem.
> If canonical URL is not routable, usually when URL is external.
Possibly the same -- don't allow the field to be added to this entity type?
> If entity is unsaved, \Drupal\Core\Entity\EntityMalformedException is thrown. (may not be an issue since path list already checks isNew).
Possibly not an issue! Sounds like this case should be investigated more.
Settings to needs work.