Problem/Motivation

Currently to the set the destination to the current page,very common, you have to do:
$url->setOption('query', \Drupal::destination()->getAsArray())
To set to another path you have to
$url->setOption('query', ['destination' => 'other/path')

It should be easier.

Proposed resolution

Add
\Drupal\Core\Url::setDestination()
Which will default to current path if known is set.

Remaining tasks

do it

User interface changes

None

API changes

New method

Data model changes

None

CommentFileSizeAuthor
#6 2944791-6.patch757 bytestedbow
#6 interdiff-2-6.txt1.2 KBtedbow
#2 2944791-2.patch908 bytestedbow

Issue fork drupal-2944791

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

tedbow created an issue. See original summary.

tedbow’s picture

Issue tags: +Needs tests
StatusFileSize
new908 bytes

First try

tedbow’s picture

Status: Active » Needs review
borisson_’s picture

I think this is a good improvement. I think that this does make the DX better. But as you said, this needs tests.

dawehner’s picture

  1. +++ b/core/lib/Drupal/Core/Url.php
    @@ -887,4 +887,27 @@ public function setUnroutedUrlAssembler(UnroutedUrlAssemblerInterface $url_assem
    +  public function setDestination($path = NULL) {
    +    if (!isset($path)) {
    +      $destination = \Drupal::destination()->getAsArray();
    +    }
    +    else {
    +      $destination = ['destination' => $path];
    +    }
    

    Instead of having one method with an optional parameter I think it would be a bit nicer to have two: $this->setDestination and $this->setCurrentDestination. This way it is way more obvious what is going on.

  2. +++ b/core/lib/Drupal/Core/Url.php
    @@ -887,4 +887,27 @@ public function setUnroutedUrlAssembler(UnroutedUrlAssemblerInterface $url_assem
    +    if ($query = $this->getOption('query')) {
    +      $query = array_merge($query, $destination);
    +    }
    +    else {
    +      $query = $destination;
    +    }
    +    $this->setOption('query', $query);
    

    What about using $url->mergeOptions for that bit?

tedbow’s picture

StatusFileSize
new1.2 KB
new757 bytes

@dawehner yep those sound good.

dawehner’s picture

That's much nicer!

borisson_’s picture

Status: Needs review » Needs work

Needs work so we can add the tests.

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.

prashant.c made their first commit to this issue’s fork.

prashant.c’s picture

Status: Needs work » Needs review

Was looking for the generic methods for the same. I have raised an MR with some additional checks, like the path should be internal only. Once this is reviewed, we can go ahead with writing tests also.

Current changes need to be reviewed

smustgrave’s picture

Status: Needs review » Needs work

Seems to have pipeline failures. May be worth converting a spot where this is needed to help show why it should be added.

prashant.c’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Test coverage is good, but I mean where in core could this be needed? Typically I see that functions that aren't called anywhere in core get deprecated and removed. So think we need least a spot or two in core where this is needed.

smustgrave’s picture

Don't have to convert all but a few help show usefulness

prashant.c’s picture

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

Was trying to find occurences of

$url->setOption('query', \Drupal::destination()->getAsArray())

and $url->setOption('query', ['destination' => 'other/path'), could only find one, pushed that change.

Tests were added, so I am removing the tag for now.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

Left some comments.

But proposed solution mentions just 1 method. If there's only 1 instance in core that could use it is this fully needed? If so will also require a CR.

jaypan’s picture

If there's only 1 instance in core that could use it is this fully needed?

It's an API helper function that can be used by developers. I often need to set the destination on URLs.

moshe weitzman’s picture

I agree that this is valuable beyond just core. Today I need it for drush site:install - https://github.com/drush-ops/drush/issues/6310.

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.