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

dpi created an issue. See original summary.

dpi’s picture

Assigned: dpi » Unassigned
Status: Active » Needs review
StatusFileSize
new2.31 KB
dpi’s picture

jibran’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
jibran’s picture

StatusFileSize
new2.05 KB
jibran’s picture

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jibran’s picture

StatusFileSize
new1.67 KB
dpi’s picture

#8 is missing class imports.

jibran’s picture

StatusFileSize
new681 bytes
new2.01 KB

Thanks updated the patch.

ravi.shankar’s picture

Status: Needs work » Needs review
dpi’s picture

Added tests

The last submitted patch, 12: 3059026-path-catch-12-test-only.patch, failed testing. View results

jibran’s picture

Thanks, for adding the tests. Patch looks good just some minor observations.

  1. +++ b/core/modules/path/tests/modules/path_entity_test/path_entity_test.module
    @@ -0,0 +1,35 @@
    +      ->setDisplayConfigurable('form', TRUE)
    

    We don't need this.

  2. +++ b/core/modules/path/tests/modules/path_entity_test/src/Entity/PathEntityTestExternalLink.php
    @@ -0,0 +1,33 @@
    +  public function toUrl($rel = 'canonical', array $options = []) {
    +    return Url::fromUri('http://example.com/');
    +  }
    

    FYI: this is what linky module is doing as well.

  3. +++ b/core/modules/path/tests/src/Kernel/PathFieldItemListTest.php
    @@ -0,0 +1,75 @@
    +    $this->assertEquals('', $entity->path->value);
    ...
    +    $this->assertNull($entity->path->value);
    ...
    +    $this->assertNull($entity->path->value);
    

    Why the first one is also not asserNull?

  4. +++ b/core/modules/path/tests/src/Kernel/PathFieldItemListTest.php
    @@ -0,0 +1,75 @@
    +    // Entities with canonical URLS to external (aka unrouted) throw
    

    s/URLS/URLs

dpi’s picture

StatusFileSize
new5.92 KB
new1.8 KB
new7.95 KB

Thanks @jibran, feedback is good and addressed.

dpi’s picture

Issue summary: View changes

Expanded issue summary

dinesh18’s picture

I 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

The last submitted patch, 15: 3059026-path-catch-15-test-only.patch, failed testing. View results

dpi’s picture

Tests pass / fail as expected.

jibran’s picture

Thanks, for addressing the feedback.

+++ b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php
@@ -26,15 +28,26 @@ class PathFieldItemList extends FieldItemList {
+      catch (EntityMalformedException $exception) {
+      }
+      catch (UndefinedLinkTemplateException $exception) {
+      }
+      catch (\UnexpectedValueException $exception) {
+      }

Let's add some comments here to explain why and in which case these exceptions will be thrown.

dpi’s picture

Let's add some comments here to explain why and in which case these exceptions will be thrown.

Each of these three exceptions is thrown directly by the called methods toUrl and getInternalPath, which document these exceptions, not deeper down the stack. I think this request would be a case of over-documentation.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

That makes sense. I was just rerolling the patch for the most part so I think it is fair to RTBC the patch.

jibran’s picture

StatusFileSize
new7.94 KB

Another reroll.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs issue summary update

One 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?

dpi’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

@alexpott the field can be added manually, or more likely due to the popular Pathauto project. Summary updated.

alexpott’s picture

So 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.

  // Verify that the configuration data isn't null (as is the case before the
  // module's initialization, in tests), so that in_array() won't fail.
  if ($enabled_entity_types = $config->get('enabled_entity_types')) {

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.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

dpi’s picture

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kim.pepper’s picture

Issue tags: +#pnx-sprint
joachim’s picture

> 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.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

Status: Needs review » Needs work

> 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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.