Problem/Motivation

Currently, there is no way to set default url route parameters besides modifying the entity class itself, but this may not always be practical. An example would be if a module would be adding link templates / entity routes to another module's entity.

Proposed resolution

I propose creating an alter hook call in the base Entity class' urlRouteParameter() method. See API Changes below.

Remaining tasks

Api documentation and hook already added. Needs peer review.

API changes

Entity API additions for a new alter hook hook_entity_url_route_parameters_alter().

There is currently another issue (#2651974: field_ui_entity_operation() cannot respect route parameters because of incorrectly named routes) that I see as slightly related in the sense that another method could be created to encapsulate urlRouteParameters(), which itself could be public (such as getRouteParameters()), and that method could then call the alter hook after urlRouteParameters() is called to make sure that the alter hook is called after an entity's default parameters for every entity.

CommentFileSizeAuthor
#45 interdiff_2941880_43_45.txt2.47 KB_pratik_
#45 2941880-45.patch6.82 KB_pratik_
#45 2941880-44.patch7.1 KB_pratik_
#44 interdiff_2941880_43_44.txt2.19 KB_pratik_
#44 2941880-43.patch7.15 KB_pratik_
#43 2941880-43.patch7.15 KBNikhil_110
#42 2941880-nr-bot.txt144 bytesneeds-review-queue-bot
#33 interdiff_28_30.txt1.26 KBanmolgoyal74
#33 2941880-30.patch7.66 KBanmolgoyal74
#30 interdiff_26_28.txt2.71 KBanmolgoyal74
#30 2941880-28.patch7.23 KBanmolgoyal74
#28 2941880-27.patch5.73 KBravi.shankar
#26 2941880-26.patch5.12 KBanmolgoyal74
#24 interdiff_22_24.txt2.97 KBanmolgoyal74
#24 2941880-24.patch5.41 KBanmolgoyal74
#22 2941880-22.patch5.47 KBgrathbone
#17 2941880-17.patch5.7 KBgrathbone
#14 2941880-14.patch4.88 KBgrathbone
#12 2941880-12.patch4.86 KBgrathbone
#5 2941880-3.patch1.47 KBgrathbone
#2 2941880-2.patch1.47 KBgrathbone
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

grathbone created an issue. See original summary.

grathbone’s picture

Status: Active » Needs review
FileSize
1.47 KB
grathbone’s picture

Issue summary: View changes
grathbone’s picture

Issue summary: View changes
grathbone’s picture

FileSize
1.47 KB

Woops. This is the patch that works. Incorrectly chose the wrong patch before.

grathbone’s picture

grathbone’s picture

Issue summary: View changes

The last submitted patch, 2: 2941880-2.patch, failed testing. View results

tim.plunkett’s picture

+++ b/core/lib/Drupal/Core/Entity/Entity.php
@@ -317,6 +317,8 @@ protected function urlRouteParameters($rel) {
+    \Drupal::moduleHandler()->alter('entity_url_route_parameters', $uri_route_parameters, $this, $rel);

Most other entity hooks would fire for the generic case and also the entity-type specific case.

The first param would change to [$this->getEntityTypeId() . '_url_route_parameters', 'entity_url_route_parameters']

and then be documented as hook_ENTITY_TYPE_url_route_parameters() in addition to the one you have.

tim.plunkett’s picture

Title: Adding the ability to alter an entity's default url route parameters » Adding the ability to alter an entity's default URL route parameters
Version: 8.4.x-dev » 8.6.x-dev

Status: Needs review » Needs work

The last submitted patch, 5: 2941880-3.patch, failed testing. View results

grathbone’s picture

Status: Needs work » Needs review
FileSize
4.86 KB

Made change listed in #10.
Fixed tests to add module_handler to the container.

Status: Needs review » Needs work

The last submitted patch, 12: 2941880-12.patch, failed testing. View results

grathbone’s picture

Status: Needs work » Needs review
FileSize
4.88 KB

Woops. Rerolled for 8.6.x.

tim.plunkett’s picture

Looks great!

  1. +++ b/core/lib/Drupal/Core/Entity/entity.api.php
    @@ -2109,5 +2109,42 @@ function hook_entity_extra_field_info_alter(&$info) {
    + * Alter an entity's default url route parameters.
    ...
    + *   The url route parameters.
    ...
    + *   The entity for which the url route parameters are used.
    ...
    + * Alter an entity's default url route parameters.
    ...
    + *   The url route parameters.
    ...
    + *   The entity for which the url route parameters are used.
    

    Nit: replace url with URL

  2. +++ b/core/lib/Drupal/Core/Entity/entity.api.php
    --- a/core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php
    +++ b/core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php
    
    +++ b/core/tests/Drupal/Tests/Core/Entity/EntityLinkTest.php
    --- a/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php
    +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php
    

    These tests (maybe just the EntityUrlTest, maybe both?) should be expanded to assert that the alter is called.

grathbone’s picture

@tim.plunkett how should I go about doing that assertion and to what extent? Do you have any examples I could look at? My UnitTest knowledge is fairly slim at this point.

Edit: Disregard. I think I found a good example.

grathbone’s picture

Added an alter hook test to EntityUnitTest (since toLink uses toUrl, it would be redundant to add a test to EntityLinkTest).
Also updated 'url' instances in the api documentation to 'URL'.

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.

kristiaanvandeneynde’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me. Maybe Tim can confirm this is what he expected to see in the test.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: 2941880-17.patch, failed testing. View results

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.

grathbone’s picture

Status: Needs work » Needs review
FileSize
5.47 KB

Rerolled for 8.7.x.

Status: Needs review » Needs work

The last submitted patch, 22: 2941880-22.patch, failed testing. View results

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
5.41 KB
2.97 KB

Updated for Drupal 8.8

Status: Needs review » Needs work

The last submitted patch, 24: 2941880-24.patch, failed testing. View results

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
5.12 KB

Status: Needs review » Needs work

The last submitted patch, 26: 2941880-26.patch, failed testing. View results

ravi.shankar’s picture

Status: Needs work » Needs review
FileSize
5.73 KB

Status: Needs review » Needs work

The last submitted patch, 28: 2941880-27.patch, failed testing. View results

anmolgoyal74’s picture

anmolgoyal74’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 30: 2941880-28.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
7.66 KB
1.26 KB
Anybody’s picture

Issue summary: View changes

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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Nikhil_110’s picture

Attached patch against Drupal 10.1.x

_pratik_’s picture

Status: Needs work » Needs review
FileSize
7.15 KB
2.19 KB

Fix for failures in #43
Thanks

_pratik_’s picture

_pratik_’s picture

Status: Needs review » Needs work

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.