Problem/Motivation

All entity handlers are instantiated per entity type, and they get the entity type definition passed in by EntityTypeManager:

  public function createHandlerInstance($class, EntityTypeInterface $definition = NULL) {
    if (is_subclass_of($class, 'Drupal\Core\Entity\EntityHandlerInterface')) {
      $handler = $class::createInstance($this->container, $definition);
    }
    else {
      $handler = new $class($definition);
    }

However, the two route provider handlers in core choose to ignore this:

  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $container->get('entity_type.manager'),
      $container->get('entity_field.manager')
    );
  }

Instead, they expect the entity type to be passed in all over again:

  public function getRoutes(EntityTypeInterface $entity_type) {

This is confusing, and pointless, and could lead to bugs if developers see this method and assume that one route provider object can handle any entity type.

Let's deprecate the parameter, and have the route providers use the $entity_type they are given on construction.

Steps to reproduce

Proposed resolution

Deprecate the `$entity_type` parameter of
`EntityRouteProviderInterface::getRoutes()` and have route providers read the
entity type they are given on construction. Make the parameter optional and add
a BC layer for providers that still declare it as required. Remove the parameter
in drupal:13.0.0.

Remaining tasks

- [x] Write the patch
- [x] Create change record
- [ ] Review

User interface changes

None.

API changes

- The `$entity_type` parameter of `EntityRouteProviderInterface::getRoutes()`
is deprecated in drupal:11.4.0 and removed in drupal:13.0.0.
- Implementing `getRoutes()` with a **required** `$entity_type` parameter is
deprecated; the parameter must be optional.
- Route providers now read the entity type injected on construction.

Data model changes

Release notes snippet

Issue fork drupal-2977140

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

joachim created an issue. See original summary.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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.

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.

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.

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.

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.

smustgrave’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +stale-issue-cleanup

Thank you for creating this issue to improve Drupal.

We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

Thanks!

joachim’s picture

Status: Postponed (maintainer needs more info) » Active
Issue tags: +Novice

Yup it's still there.

skifdesu made their first commit to this issue’s fork.

skifdesu’s picture

Status: Active » Needs review

Added a merge request deprecating entity_type in the route provider.

joachim’s picture

I don't think the new parameter to __construct() needs BC handling, because the class has a create(), and so any creation must go through that. But will ask to check.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs change record

Not 100% sure either but think we should use constructor promotion if we are touching these files. Would be in scope to fix other parameters on these files too, least that's what we have done in the past. Think this will need a CR as well.

Also summary appears to not be using the template so tagged for that.

Thanks.

acbramley’s picture

Title: deprecated pointless parameter EntityRouteProviderInterface::getRoutes($entity_type) » Deprecate pointless parameter EntityRouteProviderInterface::getRoutes($entity_type)

We also need to solve BC, see MR comment and also even with those fixes you will need to add a constructor to any route providers that don't have one (i.e the same changes as the RevisionHtmlRouteProvider

As for the constructor parameter (question in #18), I don't think it needs to be nullable, EntityTypeManager::createHandlerInstance always injects it

acbramley’s picture

Issue tags: -Novice

Removing novice tag based on the BC complexity

skifdesu’s picture

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Needs work

Thank you for your work on this issue, but it isn't ready for review yet. The issue is tagged as needing an issue summary update and a change record. The summary should be updated to include the full template. All deprecation message URLs in the merge request need to be updated to the URL of the change record.

skifdesu’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update, -Needs change record

Updated the issue summary and created the change record.

smustgrave’s picture

Status: Needs review » Needs work

Believe we missed the 11.4 boat so will have to be bumped to 11.5 please.

quietone’s picture

Title: Deprecate pointless parameter EntityRouteProviderInterface::getRoutes($entity_type) » Let route providers read the entity type on construction

The title reads as if this is a just deprecation of an unused variable, so changing title to reflect the larger change here.

skifdesu’s picture

Status: Needs work » Needs review