Problem/Motivation

Drupal::l($text, Url $url) and Drupal\Core\Link::fromTextAndUrl($text, Url $url) currently document $text as a string.

The function actually accepts string|array|\Drupal\Component\Render\MarkupInterface since it uses LinkGeneratorInterface::generate() to output markup.

This documentations is relevant when trying to create the following markup in a renderable array:

<a href="link" aria-label="See Picked Pens">
  <svg> 
    <use xlink:href="#icon-codepen"></use>
  </svg>
</a>

Source: https://css-tricks.com/accessible-svgs/

Proposed resolution

Update Drupal::l() and Drupal\Core\Link $text documentation so the $text input is correctly hinted.

Remaining tasks

  1. Write a patch
  2. Review
  3. Commit

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

idebr created an issue. See original summary.

idebr’s picture

Issue summary: View changes

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

idebr’s picture

Status: Active » Needs review
StatusFileSize
new1.21 KB

Attached patch copies the @param documentation from \Drupal\Core\Utility\LinkGeneratorInterface::generate() to \Drupal\Core\Link::fromTextAndUrl() and \Drupal::l()

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kristen pol’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative

Needs updating for 9.1.x. Note that l() was removed in 9.0.0:

https://www.drupal.org/project/drupal/issues/2606376

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new638 bytes
new1.03 KB

Updating @param documentation for Drupal\Core\Link::fromTextAndUrl() only for 9.1.x as l() was removed in 9.0.0. Kindly review a patch.

kristen pol’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the update. I see that it addresses #10.

Looking at LinkGeneratorInterface, it has this description:

   * @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\Utility\SafeMarkup::format().

and the patch here has:

+++ b/core/lib/Drupal/Core/Link.php
@@ -47,8 +47,8 @@ public function __construct($text, Url $url) {
-   * @param string $text
-   *   The text of the link.
+   * @param string|array|\Drupal\Component\Render\MarkupInterface $text
+   *   The link text for the anchor tag as a translated string or render array.

So it does not include this part:

   *   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\Utility\SafeMarkup::format().

but perhaps this is ok since it's documented in the LinkGeneratorInterface.

Given that and tests are green, marking this RTBC.

  • larowlan committed 1671837 on 9.1.x
    Issue #2875807 by Hardik_Patel_12, idebr, Kristen Pol: Drupal::l() /...
larowlan’s picture

Title: Drupal::l() / Link::fromTextAndUrl $text documented as string, actually accepts string|array|\Drupal\Component\Render\MarkupInterface » [backport] Drupal::l() / Link::fromTextAndUrl $text documented as string, actually accepts string|array|\Drupal\Component\Render\MarkupInterface
Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed 1671837 and pushed to 9.1.x. Thanks!

Cherry-picked to 9.0.x

This needs updating for 8.9.x to make the same change to \Drupal::l() (which doesn't exist in 9.x)

  • larowlan committed fcb2e87 on 9.0.x
    Issue #2875807 by Hardik_Patel_12, idebr, Kristen Pol: Drupal::l() /...
idebr’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new1.23 KB

Attached patch applies to 8.9.x and makes the same change to \Drupal::l()

kristen pol’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the patch. Looks good to me.

1) Patch applies cleanly.

2) Tests pass.

3) Updates Drupal::l() and Link::fromTextAndUrl() as expected and the text is identical in both places and matches what was used in 9.0.x/9.1.x.

  • larowlan committed bb5f6e5 on 8.9.x
    Issue #2875807 by idebr, Hardik_Patel_12, Kristen Pol, larowlan: [...
larowlan’s picture

Title: [backport] Drupal::l() / Link::fromTextAndUrl $text documented as string, actually accepts string|array|\Drupal\Component\Render\MarkupInterface » Drupal::l() / Link::fromTextAndUrl $text documented as string, actually accepts string|array|\Drupal\Component\Render\MarkupInterface
Status: Reviewed & tested by the community » Fixed
larowlan’s picture

C/p to 8.9.x - thanks

Status: Fixed » Closed (fixed)

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