Commit 01382db4 authored by alexpott's avatar alexpott

Issue #2416971 by dawehner, pwolanin: Remove Url::__toString()

parent 44323cf7
......@@ -739,13 +739,6 @@ public function toString() {
return $this->urlGenerator()->generateFromRoute($this->getRouteName(), $this->getRouteParameters(), $this->getOptions());
}
/**
* {@inheritdoc}
*/
public function __toString() {
return $this->toString();
}
/**
* Returns the route information for a render array.
*
......
......@@ -117,7 +117,7 @@ function testForum() {
//Check that the basic forum install creates a default forum topic
$this->drupalGet('/forum');
// Look for the "General discussion" default forum
$this->assertRaw(t('<a href="'. Url::fromRoute('forum.page', ['taxonomy_term' => 1]) .'">General discussion</a>'), "Found the default forum at the /forum listing");
$this->assertRaw(t('<a href="'. Url::fromRoute('forum.page', ['taxonomy_term' => 1])->toString() .'">General discussion</a>'), "Found the default forum at the /forum listing");
// Do the admin tests.
$this->doAdminTests($this->admin_user);
......
......@@ -36,7 +36,7 @@ protected function setUp() {
// Update module should not go out to d.o to check for updates. We override
// the url to an invalid update source. No update data will be found.
$this->config('update.settings')->set('fetch.url', (string) Url::fromRoute('<front>', array(), array('absolute' => TRUE)))->save();
$this->config('update.settings')->set('fetch.url', (string) Url::fromRoute('<front>')->setAbsolute()->toString())->save();
}
/**
......
......@@ -340,7 +340,7 @@ protected function doTestAlternateHreflangLinks(Url $url) {
// Retrieve desired link elements from the HTML head.
$links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]',
array(':href' => $language_url->toString(), ':hreflang' => $alternate_langcode));
$this->assert(isset($links[0]), format_string('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', array('%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url)));
$this->assert(isset($links[0]), format_string('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', array('%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url->toString())));
}
}
}
......
......@@ -1503,6 +1503,10 @@ protected function drupalGet($path, array $options = array(), array $headers = a
$out = $new;
}
if ($path instanceof Url) {
$path = $path->toString();
}
$verbose = 'GET request to: ' . $path .
'<hr />Ending URL: ' . $this->getUrl();
if ($this->dumpHeaders) {
......@@ -1694,6 +1698,9 @@ protected function drupalPostForm($path, $edit, $submit, array $options = array(
$out = $new;
}
if ($path instanceof Url) {
$path = $path->toString();
}
$verbose = 'POST request to: ' . $path;
$verbose .= '<hr />Ending URL: ' . $this->getUrl();
if ($this->dumpHeaders) {
......
......@@ -84,7 +84,26 @@ public function testTwigLinkGenerator() {
}
/**
* Tests the file_url Twig functions.
* Tests the magic url to string Twig functions.
*
* @see \Drupal\Core\Url
*/
public function testTwigUrlToString() {
$this->drupalGet('twig-theme-test/url-to-string');
$expected = [
'rendered url: ' . Url::fromRoute('user.register')->toString(),
];
$content = $this->getRawContent();
$this->assertFalse(empty($content), 'Page content is not empty');
foreach ($expected as $string) {
$this->assertRaw('<div>' . $string . '</div>');
}
}
/**
* Tests the automatic/magic calling of toString() on objects, if exists.
*/
public function testTwigFileUrls() {
$this->drupalGet('/twig-theme-test/file-url');
......
......@@ -49,6 +49,16 @@ public function linkGeneratorRender() {
);
}
/**
* Renders a URL to a string.
*/
public function urlToStringRender() {
return [
'#theme' => 'twig_theme_test_url_to_string',
'#test_url' => Url::fromRoute('user.register'),
];
}
/**
* Renders for testing file_url functions in a Twig template.
*/
......
......@@ -42,6 +42,10 @@ function twig_theme_test_theme($existing, $type, $theme, $path) {
'variables' => array('test_url' => NULL),
'template' => 'twig_theme_test.link_generator',
);
$items['twig_theme_test_url_to_string'] = array(
'variables' => array('test_url' => NULL),
'template' => 'twig_theme_test.url_to_string',
);
$items['twig_theme_test_file_url'] = array(
'variables' => array(),
'template' => 'twig_theme_test.file_url',
......
......@@ -26,6 +26,13 @@ twig_theme_test_link_generator:
requirements:
_access: 'TRUE'
twig_theme_test_url_to_string:
path: '/twig-theme-test/url-to-string'
defaults:
_controller: '\Drupal\twig_theme_test\TwigThemeTestController::urlToStringRender'
requirements:
_access: 'TRUE'
twig_theme_test_file_url:
path: '/twig-theme-test/file-url'
defaults:
......
......@@ -715,7 +715,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
'#title' => $this->t('Text'),
'#type' => 'textarea',
'#default_value' => $this->options['alter']['text'],
'#description' => $this->t('The text to display for this field. You may include HTML or <a href="@url">Twig</a>. You may enter data from this view as per the "Replacement patterns" below.', array('@url' => CoreUrl::fromUri('http://twig.sensiolabs.org/documentation'))),
'#description' => $this->t('The text to display for this field. You may include HTML or <a href="@url">Twig</a>. You may enter data from this view as per the "Replacement patterns" below.', array('@url' => CoreUrl::fromUri('http://twig.sensiolabs.org/documentation')->toString())),
'#states' => array(
'visible' => array(
':input[name="options[alter][alter_text]"]' => array('checked' => TRUE),
......
......@@ -50,8 +50,8 @@ public function testEntityOperations() {
foreach ($entities as $entity) {
$operations = \Drupal::entityManager()->getListBuilder('entity_test')->getOperations($entity);
foreach ($operations as $operation) {
$expected_destination = Url::fromUri('internal:/test-entity-operations');
$result = $this->xpath('//ul[contains(@class, dropbutton)]/li/a[contains(@href, :path) and text()=:title]', array(':path' => $operation['url'] . '?destination=' . $expected_destination, ':title' => $operation['title']));
$expected_destination = Url::fromUri('internal:/test-entity-operations')->toString();
$result = $this->xpath('//ul[contains(@class, dropbutton)]/li/a[contains(@href, :path) and text()=:title]', array(':path' => $operation['url']->toString() . '?destination=' . $expected_destination, ':title' => $operation['title']));
$this->assertEqual(count($result), 1, t('Found entity @operation link with destination parameter.', array('@operation' => $operation['title'])));
}
}
......
......@@ -727,7 +727,7 @@ public function renderPreview($display_id, $args = array()) {
if (isset($path)) {
// @todo Views should expect and store a leading /. See:
// https://www.drupal.org/node/2423913
$path = \Drupal::l($path, Url::fromUserInput('/' . $path));
$path = \Drupal::l($path->toString(), $path);
}
else {
$path = t('This display has no path.');
......
......@@ -375,24 +375,6 @@ 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 getRouteName() method.
*
......
......@@ -150,6 +150,12 @@ function twig_render_var($arg) {
if (method_exists($arg, '__toString')) {
return (string) $arg;
}
// You can't throw exceptions in the magic PHP __toString methods, see
// http://php.net/manual/en/language.oop5.magic.php#object.tostring so
// we also support a toString method.
elseif (method_exists($arg, 'toString')) {
return $arg->toString();
}
else {
throw new Exception(t('Object of type "@class" cannot be printed.', array('@class' => get_class($arg))));
}
......@@ -237,6 +243,12 @@ function twig_drupal_escape_filter(\Twig_Environment $env, $string, $strategy =
if (method_exists($string, '__toString')) {
$return = (string) $string;
}
// You can't throw exceptions in the magic PHP __toString methods, see
// http://php.net/manual/en/language.oop5.magic.php#object.tostring so
// we also support a toString method.
elseif (method_exists($string, 'toString')) {
$return = $string->toString();
}
else {
throw new \Exception(t('Object of type "@class" cannot be printed.', array('@class' => get_class($string))));
}
......
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