Problem/Motivation

The $title variable in PathBasedBreadcrumbBuilder.php of system module could be returned as a render array, but new Link($title, $url) requires $title variable to be a string.

$title = $this->titleResolver->getTitle($route_request, $route_match->getRouteObject());

Returns for us a render array with #markup and #allowed_tags for a taxonomy term entity which is rendered via a custom taxonomy_term view instead of $title as a simple string.

I have this bug with Drupal core 8.7.11, but maybe it also occurs for newer releases. Maybe it is not only related to core, because we also use TVI contrib module and a lot of other stuff.

Steps to reproduce

Proposed resolution

Copy the documentation from \Drupal\Core\Utility\LinkGeneratorInterface::generate

   * @param string|array|\Drupal\Component\Render\MarkupInterface $text
   *   The link text for the anchor tag as a translated string or render array.
   *   Strings will be sanitized automatically. If you need to output HTML in
   *   the link text, use a render array or an already sanitized string such as
   *   the output of \Drupal\Component\Utility\Xss::filter() or
   *   \Drupal\Component\Render\FormattableMarkup.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3163487

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

IT-Cru created an issue. See original summary.

IT-Cru’s picture

Added my patch with my extra is_string check for $title variable.

IT-Cru’s picture

Status: Active » Needs review

Status: Needs review » Needs work
idebr’s picture

Link can take an array as its title, because \Drupal\Core\Utility\LinkGenerator::generate() has the following code:

    if (is_array($text)) {
      $text = $this->renderer->render($text);
    }

The documentation for $text was updated in #2875807: Drupal::l() / Link::fromTextAndUrl $text documented as string, actually accepts string|array|\Drupal\Component\Render\MarkupInterface, but it seems we missed a spot since it still a string in the Link __construct method.

larowlan’s picture

Issue tags: +Bug Smash Initiative

Are you saying this 'works as designed' @idebr and all we need to do is update docs?

idebr’s picture

#6 Correct, Link can take an array as $text.

raman.b’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Needs work » Needs review
FileSize
1.99 KB

Updating all the instances of string $text to string|array|\Drupal\Component\Render\MarkupInterface $text in Drupal\Core\Link

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

dawehner’s picture

I wonder whether it would be sensible to use exactly the same documentation as on the link generator, so the two places are in sync.

   * @param string|array|\Drupal\Component\Render\MarkupInterface $text
   *   The link text for the anchor tag as a translated string or render array.
   *   Strings will be sanitized automatically. If you need to output HTML in
   *   the link text, use a render array or an already sanitized string such as
   *   the output of \Drupal\Component\Utility\Xss::filter() or
   *   \Drupal\Component\Render\FormattableMarkup.
idebr’s picture

Issue summary: View changes
Status: Needs review » Needs work

Agreed with #10, I updated the 'Proposed resolution' in the issue summary accordingly

sudiptadas19 made their first commit to this issue’s fork.

sudiptadas19’s picture

Status: Needs work » Needs review

Added the documentation changes for Link class $text var. Available in MR403

JeroenT’s picture

Status: Needs review » Needs work

Test are failing because of:

FILE: /var/www/html/core/lib/Drupal/Core/Link.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 26 | ERROR | [x] Whitespace found at end of line
 99 | ERROR | [x] Return type must not contain variable name "$text"
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 131ms; Memory: 4MB
sudiptadas19’s picture

Status: Needs work » Needs review

@JeroenT added a fix for the test failing. Please review.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

guilhermevp’s picture

Status: Needs review » Reviewed & tested by the community

Patch applies, works as proposed summary text. Moving to RTBC.

larowlan’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 0790536 and pushed to 9.3.x. Thanks!

Backported to 9.2.x

  • larowlan committed 393a419 on 9.2.x
    Issue #3163487 by sudiptadas19, IT-Cru, raman.b, idebr, dawehner:...
  • larowlan committed 0790536 on 9.3.x
    Issue #3163487 by sudiptadas19, IT-Cru, raman.b, idebr, dawehner:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.