Commit b1500e07 authored by alexpott's avatar alexpott

Issue #2844001 by dawehner, tim.plunkett: Add a Url::mergeOptions method

parent d9bc5a13
......@@ -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;
......@@ -674,6 +675,23 @@ public function setOption($name, $value) {
return $this;
}
/**
* 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.
*
......
......@@ -468,6 +468,31 @@ 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());
$url->setOptions([]);
$this->assertEquals([], $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.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment