diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index 9f78645a238d7e431fe37cf3d4be8ef1551ae9eb..9ea38b213539f6438f5df8e5ba1ce141d809641e 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -451,6 +451,13 @@ public function toString() { return $this->urlGenerator()->generateFromRoute($this->getRouteName(), $this->getRouteParameters(), $this->getOptions()); } + /** + * {@inheritdoc} + */ + public function __toString() { + return $this->toString(); + } + /** * Returns all the information about the route. * diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php index 404702e2a55f2496f5a848e19d6c3a00f6663741..119eee6d78b7b64ec07c64878d458cbd8bbc3d3f 100644 --- a/core/tests/Drupal/Tests/Core/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/UrlTest.php @@ -234,6 +234,24 @@ public function testToString($urls) { } } + /** + * Tests the __toString() method. + * + * @param \Drupal\Core\Url[] $urls + * An array of Url objects. + * + * @depends testUrlFromRequest + * + * @covers ::__toString + */ + public function testMagicToString($urls) { + foreach ($urls as $index => $url) { + $url->setUrlGenerator(\Drupal::urlGenerator()); + $path = array_pop($this->map[$index]); + $this->assertSame($path, (string) $url); + } + } + /** * Tests the toArray() method. *