Problem/Motivation

The current implementation of the title resolution mechanism does not behave as expected when both _raw_variables and _title_arguments are set in a route. Specifically, when _raw_variables is defined in the route, the getTitle() method continues to return the static value specified in the route rather than utilizing the provided _title_arguments. This creates confusion and prevents dynamic title generation based on URL parameters.

Steps to reproduce

  1. Create a route with URL parameters and set _title and _title_arguments
  2. Observe the displayed title
  3. The route display static titles as defined in the routing configuration, without reflecting any changes from the URL parameters or title arguments
  4. ex :

    custom_module.test_route:
      path: '/test-route/{test}/{test2}'
      defaults:
        _controller: '\Drupal\custom_module\Controller\CustomPageController::content'
        _title: 'Static title @test @test2'
        _title_arguments:
          '@test': 'value'
          '@test2': 'value2'
      requirements:
        _permission: 'access content'

Proposed resolution

API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Controlle...

The code for getting the arguments for the route in TitleResolver.php file in getTitle() is as follows:

      $args = [];
      if (($raw_parameters = $request->attributes->get('_raw_variables'))) {
        foreach ($raw_parameters->all() as $key => $value) {
          $args['@' . $key] = $value;
          $args['%' . $key] = $value;
        }
      }
      if ($title_arguments = $route->getDefault('_title_arguments')) {
        $args = array_merge($args, (array) $title_arguments);
      }

Here '_title_arguments' are the arguments specified in the routing file, which IMO, should be the default value in case "_raw_variables" are not there.

So IMO, the array_merge() should be:
array_merge((array) $title_arguments, $args);

Remaining tasks

- Needs Review

User interface changes

- None

API changes

- None

Data model changes

- None

Release notes snippet

- None

Issue fork drupal-3081044

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

Vivek Panicker created an issue. See original summary.

vivek panicker’s picture

vivek panicker’s picture

Status: Active » Needs review

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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

This will need a test case to show the issue.

Did not test for the issue.

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.

nlisgo’s picture

Issue tags: +Bug Smash Initiative

This seems to be clearly described. Tagged as Needs tests and it should be easy to add a test.

nlisgo’s picture

Assigned: Unassigned » nlisgo

Will work on a fix with test.

nlisgo’s picture

Status: Needs work » Needs review
nlisgo’s picture

smustgrave’s picture

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

Can issue summary be updated using the issue template please

nlisgo’s picture

Assigned: nlisgo » Unassigned
shalini_jha’s picture

Issue summary: View changes
shalini_jha’s picture

Issue summary: View changes
shalini_jha’s picture

Status: Needs work » Needs review

I have tested MR 8584, and after these changes, when the parameter is available in the URL, the getTitle method returns the value from the URL parameter instead of the static value from the routing. Additionally, I have updated the issue summary.
Moving this to NR, Kindly review.

vivek panicker’s picture

Thanks @shalini.

@smustgrave the issue summary is fixed and MR is tested now.

smustgrave’s picture

Status: Needs review » Needs work

This issue needs a rebase 488 commits behind, the pipeline can't be reliable at that point.

shalini_jha’s picture

Status: Needs work » Needs review

I have rebased this MR, and the pipeline has passed successfully. Moving it to 'Needs Review'.
Kindly review.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs issue summary update

So see that 2 tests have been added but when the test-only feature is ran only testStaticTitleWithDefaultArgumentsOverridden is failing.

shalini_jha’s picture

Status: Needs work » Needs review

I have verified that the testStaticTitleWithDefaultArguments method checks the default behaviour of using static titles with preset arguments without testing dynamic overrides, which is why it does not fail. The issue is specifically addressed in testStaticTitleWithDefaultArgumentsOverridden, where dynamic URL parameters should replace default values but are not being applied correctly.

Please let me know if we should remove the method that tests the default functionality. Moving this to "Needs Review."

smustgrave’s picture

Status: Needs review » Needs work

Lets consolidate them. The code seems very similar so probably overlaps.

shalini_jha’s picture

Assigned: Unassigned » shalini_jha
shalini_jha’s picture

Assigned: shalini_jha » Unassigned
Status: Needs work » Needs review

I have consolidated both test methods into a single one and tested it. It is working as expected. Moving this to NR for review. Kindly review.

Failing test :

There was 1 failure:

1) Drupal\Tests\Core\Controller\TitleResolverTest::testStaticTitleWithArguments
Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
 Drupal\Core\StringTranslation\TranslatableMarkup Object (
     'string' => 'static title @test @test2'
     'arguments' => Array (
-        '@test' => 'override value'
+        '@test' => 'value'
         '%test' => 'override value'
         '@test2' => 'value2'
     )
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback appears to be addressed.

quietone’s picture

Issue tags: +Needs title update

I read the IS, comments and the MR. This looks in order. I don't know much about routing so did some investigation.

The code being changed was added in 2014 in [ #2120235] but it doesn't address the purpose of the _title_arguments. The Structure of routes states

_title_arguments: Additional arguments for the title text passed along to t().

If that is true then I wonder if this is working as designed. If it is not this needs a title update.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

This is a change in behaviour. Potentially someone might be relying on the ability of _title_arguments to override the arguments based on the routes raw parameters. That means I think we need a change record. Unfortunately there is no documentation in core about _title_arguments as ideally the behaviour should be documented there. The change being made here however is inline with the documentation linked to from #32 as this now means that _title_arguments can only define additional arguments and values and not override stuff coming from the route.

shalini_jha’s picture

Status: Needs work » Needs review

Thank you for the review and feedback. Based on the suggestions provided, I tested the functionality after implementing the recommended changes, and it is now working as expected. There is no need for an array_merge after retrieving the arguments from _raw_variables. I also re-ran the tests, and they are passing successfully.

I have also drafted a CR based on the work done and provided an explanation of the changes made. However, I’m unsure if anything else needs to be added. I kindly request you to review it and share your feedback. Thank you!

smustgrave’s picture

Title: Route "_title_arguments" computation may be incorrect » Allow default title arguments to be overridden
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs title update, -Needs change record

Think the title of the MR probably makes more sense.

CR seems well detailed

Believe all feedback has been addressed.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

As this is a subtle behaviour change I'm going to only put this in 11.2.x to give people a chance to notice this change.

Committed 0c9b4fc and pushed to 11.x. Thanks!

  • alexpott committed 0c9b4fcd on 11.x
    Issue #3081044 by shalini_jha, nlisgo, vivek panicker, smustgrave,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.