From b1500e0704ffd222fc026d5fb71e177705001e81 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 18 Jan 2017 15:17:20 +0000
Subject: [PATCH] Issue #2844001 by dawehner, tim.plunkett: Add a
 Url::mergeOptions method

---
 core/lib/Drupal/Core/Url.php             | 18 +++++++++++++++++
 core/tests/Drupal/Tests/Core/UrlTest.php | 25 ++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php
index e7624d8a65a3..ccc62fe750ba 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;
@@ -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.
    *
diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php
index 5324c26405bc..2c157d5bdfe9 100644
--- a/core/tests/Drupal/Tests/Core/UrlTest.php
+++ b/core/tests/Drupal/Tests/Core/UrlTest.php
@@ -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.
    *
-- 
GitLab