Scenario

1. Have a route with an entity_revision parameter.

Example:

node.revision_edit:
  path: '/node/{node}/revisions/{node_revision}/edit'
  defaults:
    _entity_form: node.edit
    _title: 'Edit revision'
  requirements:
    access: 'update'
  options:
    _node_operation_route: TRUE
    parameters:
      node:
        type: entity:node
      node_revision:
        type: entity_revision:node

2. Access the route with an entity revision ID that doesn't actually exist. Example: /node/1/revisions/-9999/edit

Expected result: 404 page not found

Actual result:

The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\Core\Entity\EntityRepository::getTranslationFromContext() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /core/lib/Drupal/Core/ParamConverter/EntityRevisionParamConverter.php on line 61 in Drupal\Core\Entity\EntityRepository->getTranslationFromContext() (line 82 of core/lib/Drupal/Core/Entity/EntityRepository.php). 

Solution

Have EntityRevisionParamConverter do the proper checking before calling the getTranslationFromContext method.

Comments

stefan.butura created an issue. See original summary.

stefan.butura’s picture

Issue summary: View changes
stefan.butura’s picture

Status: Active » Needs review
hchonov’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Nice find.

+++ b/core/lib/Drupal/Core/ParamConverter/EntityRevisionParamConverter.php
@@ -58,7 +59,9 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Ent
+    return $entity instanceof EntityInterface
...
+      : NULL;

In this case it is sufficient to just check if $entity evaluates to TRUE -
$entity ? $this->entityRepository->getTranslationFromContext($entity) : NULL;

Now we only need a test for it :)

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mohit_aghera’s picture

Status: Needs work » Closed (outdated)
Issue tags: +Bug Smash Initiative

I came across this issue during bugsmash triage.

This has already been fixed in latest Drupal 11.x by issue #2808163: Support dynamic entity types in the EntityRevisionParamConverter

    // If the entity type is translatable, ensure we return the proper
    // translation object for the current context.
    if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
      $entity = $this->entityRepository->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']);
    }

    return $entity;
  }

Latest changes in 11.x looks good. https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.