On FormattableMarkup::placeholderFormat(), the secure examples are the following.

$this->placeholderFormat('<a href=":foo">link text</a>, ['@foo' => $some_variable]);
$this->placeholderFormat('<a href=":foo" title="static text">link text</a>, ['@foo' => $some_variable]);
$this->placeholderFormat('<a href=":foo">@foo</a>, ['@foo' => $some_variable]);
// Use : placeholder inside an HTML tag.
$this->placeholderFormat('<img src=":foo" />, ['@foo' => '/image.png']);

The placeholders used in the strings are different from the placeholders in the array passed as second argument. For example, the first example uses :foo as placeholder, but the array contains '@foo'. Furthermore, the literal strings passed as first argument do not include string delimiters.

The correct examples are the following one.

$this->placeholderFormat('<a href=":foo">link text</a>', [':foo' => $some_variable]);
$this->placeholderFormat('<a href=":foo" title="static text">link text</a>', [':foo' => $some_variable]);
$this->placeholderFormat('<a href=":foo">@foo</a>', [':foo' => $some_variable]);
// Use : placeholder inside an HTML tag.
$this->placeholderFormat('<img src=":foo" />', [':foo' => '/image.png']);

Issue fork drupal-3498468

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:

Comments

wombatbuddy created an issue. See original summary.

wombatbuddy’s picture

Issue summary: View changes
poker10’s picture

Category: Task » Bug report
Priority: Normal » Minor
Issue tags: +Novice
Related issues: +#2580505: Improve FormattableMarkup documentation

This seems to be caused by changes from #2580505: Improve FormattableMarkup documentation. Tagging as novice.

avpaderno’s picture

avpaderno’s picture

Issue summary: View changes

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

nexusnovaz’s picture

Status: Active » Needs review

I've made the changes in MR !11912 though there I have a question for Line 118. Currently the correct usage in the html tag should be href=":foo", but then @foo is used as the placeholder for the text surrounded by the html tag. However, there is no substitution for this in the array. Should the @foo also be changed to use :foo as it would appear they should have the same output?

avpaderno’s picture

In '<a href=":foo">@foo</a>, @foo can either be left, or replaced. (I would use '<a href=":foo">link text</a>' like the other examples.)

Also, in $this->placeholderFormat('<a href=":foo">@foo</a>, ['@foo' => $some_variable]); a string delimiter is missing. The correct code is the following one.

$this->placeholderFormat('<a href=":foo">link text</a>', [':foo' => $some_variable]);

nexusnovaz’s picture

Nice, thanks for that @avpaderno. I've made those changes and i believe it is all correct. Please can someone review MR !11912

avpaderno’s picture

Title: The documentation for the function FormattableMarkup::placeholderFormat contains typos » The example code given for FormattableMarkup::placeholderFormat() contains typos and syntax errors
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

The changes in the MR are correct.

quietone’s picture

@poker10, thanks for searching for where this was introduced. That is always helpful.

Updated credit. I will commit this presently.

  • quietone committed 88f4168d on 11.1.x
    Issue #3498468 by nexusnovaz, avpaderno, wombatbuddy, poker10: The...

  • quietone committed 7caa7cbd on 11.x
    Issue #3498468 by nexusnovaz, avpaderno, wombatbuddy, poker10: The...
quietone’s picture

Version: 11.x-dev » 11.1.x-dev
Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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