diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index e7624d8..ccc62fe 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -2,6 +2,7 @@ namespace Drupal\Core; +use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Drupal\Core\Routing\RouteMatchInterface; @@ -675,6 +676,23 @@ public function setOption($name, $value) { } /** + * Merges the URL options with any currently set. + * + * In the case of conflict with existing options, the new options will replace + * the existing options. + * + * @param array $options + * The array of options. See \Drupal\Core\Url::fromUri() for details on what + * it contains. + * + * @return $this + */ + public function mergeOptions($options) { + $this->options = NestedArray::mergeDeep($this->options, $options); + return $this; + } + + /** * Returns the URI value for this Url object. * * Only to be used if self::$unrouted is TRUE. diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php index 5324c26..e9a9930 100644 --- a/core/tests/Drupal/Tests/Core/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/UrlTest.php @@ -469,6 +469,29 @@ public function testGetOptions($urls) { } /** + * Tests the setOptions() method. + * + * @covers ::setOptions + */ + public function testSetOptions() { + $url = Url::fromRoute('test_route', []); + $this->assertEquals([], $url->getOptions()); + $url->setOptions(['foo' => 'bar']); + $this->assertEquals(['foo' => 'bar'], $url->getOptions()); + } + + /** + * Tests the mergeOptions() method. + * + * @covers ::mergeOptions + */ + public function testMergeOptions() { + $url = Url::fromRoute('test_route', [], ['foo' => 'bar', 'bar' => ['key' => 'value']]); + $url->mergeOptions(['bar' => ['key' => 'value1', 'key2' => 'value2']]); + $this->assertEquals(['foo' => 'bar', 'bar' => ['key' => 'value1', 'key2' => 'value2']], $url->getOptions()); + } + + /** * Tests the access() method for routed URLs. * * @param bool $access