Commit a7254dba authored by Alex Bronstein's avatar Alex Bronstein
Browse files

Issue #2568977 by alexpott, dawehner, pwolanin, Wim Leers: Replace...

Issue #2568977 by alexpott, dawehner, pwolanin, Wim Leers: Replace SafeMarkup::format() in the link generator - it's a bad example to everyone
parent 108e038e
Loading
Loading
Loading
Loading
+5 −9
Changes for core/lib/Drupal.php: 5 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -542,20 +542,16 @@ public static function linkGenerator() {
   *   The link text for the anchor tag.
   * @param \Drupal\Core\Url $url
   *   The URL object used for the link.
   * @param bool $collect_bubbleable_metadata
   *   (optional) Defaults to FALSE. When TRUE, both the generated URL and its
   *   associated bubbleable metadata are returned.
   *
   * @return string|\Drupal\Core\GeneratedLink
   *   An HTML string containing a link to the given route and parameters.
   *   When $collect_bubbleable_metadata is TRUE, a GeneratedLink object is
   *   returned, containing the generated link plus bubbleable metadata.
   * @return \Drupal\Core\GeneratedLink
   *   A GeneratedLink object containing a link to the given route and
   *   parameters and bubbleable metadata.
   *
   * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
   * @see \Drupal\Core\Url
   */
  public static function l($text, Url $url, $collect_bubbleable_metadata = FALSE) {
    return static::getContainer()->get('link_generator')->generate($text, $url, $collect_bubbleable_metadata);
  public static function l($text, Url $url) {
    return static::getContainer()->get('link_generator')->generate($text, $url);
  }

  /**
+16 −1
Changes for core/lib/Drupal/Core/GeneratedLink.php: 16 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@

namespace Drupal\Core;

use Drupal\Component\Utility\SafeStringInterface;
use Drupal\Core\Render\BubbleableMetadata;

/**
@@ -15,7 +16,7 @@
 * Note: not to be confused with \Drupal\Core\Link, which is for passing around
 *   ungenerated links (typically link text + route name + route parameters).
 */
class GeneratedLink extends BubbleableMetadata {
class GeneratedLink extends BubbleableMetadata implements SafeStringInterface {

  /**
   * The HTML string value containing a link.
@@ -46,4 +47,18 @@ public function setGeneratedLink($generated_link) {
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function __toString() {
    return (string) $this->generatedLink;
  }

  /**
   * {@inheritdoc}
   */
  public function jsonSerialize() {
    return $this->__toString();
  }

}
+3 −9
Changes for core/lib/Drupal/Core/Link.php: 3 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -140,20 +140,14 @@ public function setUrl(Url $url) {
  /**
   * Generates the HTML for this Link object.
   *
   * @param bool $collect_bubbleable_metadata
   *   (optional) Defaults to FALSE. When TRUE, both the generated link and its
   *   associated bubbleable metadata are returned.
   *
   * @return string|\Drupal\Core\GeneratedLink
   * @return \Drupal\Core\GeneratedLink
   *   The link HTML markup.
   *   When $collect_bubbleable_metadata is TRUE, a GeneratedLink object is
   *   returned, containing the generated link plus bubbleable metadata.
   *
   * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0. Use
   *   self::toRenderable() instead.
   */
  public function toString($collect_bubbleable_metadata = FALSE) {
    return $this->getLinkGenerator()->generateFromLink($this, $collect_bubbleable_metadata);
  public function toString() {
    return $this->getLinkGenerator()->generateFromLink($this);
  }

  /**
+6 −2
Changes for core/lib/Drupal/Core/Menu/menu.api.php: 6 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -440,7 +440,11 @@ function hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$bread
 *   exposed as the 'link_generator' service or a link generated by _l(). If the
 *   link is a "route link", 'route_name' will be set, otherwise 'path' will be
 *   set. The following keys can be altered:
 *   - text: The link text for the anchor tag as a translated string.
 *   - text: The link text for the anchor tag. If the hook implementation
 *     changes this text it needs to preserve the safeness of the original text.
 *     Using t() or \Drupal\Component\Utility\SafeMarkup::format() with
 *     @placeholder is recommended as this will escape the original text if
 *     necessary. If the resulting text is not marked safe it will be escaped.
 *   - url_is_active: Whether or not the link points to the currently active
 *     URL.
 *   - url: The \Drupal\Core\Url object.
@@ -468,7 +472,7 @@ function hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$bread
function hook_link_alter(&$variables) {
  // Add a warning to the end of route links to the admin section.
  if (isset($variables['route_name']) && strpos($variables['route_name'], 'admin') !== FALSE) {
    $variables['text'] .= ' (Warning!)';
    $variables['text'] = t('@text (Warning!)', ['@text' => $variables['text']]);
  }
}

+1 −1
Changes for core/lib/Drupal/Core/Render/Element/Link.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ public static function preRenderLink($element) {
      $options = NestedArray::mergeDeep($element['#url']->getOptions(), $element['#options']);
      /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */
      $link_generator = \Drupal::service('link_generator');
      $generated_link = $link_generator->generate($element['#title'], $element['#url']->setOptions($options), TRUE);
      $generated_link = $link_generator->generate($element['#title'], $element['#url']->setOptions($options));
      $element['#markup'] = $generated_link->getGeneratedLink();
      $generated_link->merge(BubbleableMetadata::createFromRenderArray($element))
        ->applyTo($element);
Loading