Link render array style attribute getting stripped in event subscriber

ph flag

I'm trying to generate a link as part of a string which will get sent by email. The link has to be styled to look like a button.

I'm doing this in a token event subscriber.

Here's my current code:

$url = Url::fromUri('');
$link = new Link('Click me', $url);
$link = $link->toRenderable();
$link['#attributes'] = [
  'class' => ['nm-share', 'nm-share-' . $type_name],
  'target' => '_top',
  'style' => 'display:inline-block;width:213px;height:30px;color:#FFFFFF;text-decoration:none;text-align:center;padd  ing:15px 0px 0px;background:' . $colour . ';font:normal 17px/17px Arial, Helvetica, sans-serif;box-sizing:content-box;-webki  t-box-sizing:content-box;-moz-box-sizing:content-box;',

When the link is rendered it has the class and target attributes, but the style attribute is missing. How can I stop that from happening?

Converting my code to use an inline_template element seems like a messy solution.

The weird thing is if I put the same code in a drush script and run the script the style attribute does get rendered.

ru flag
See [Add inline style to render array attributes, without XSS filtering]( - short answer: You can't because this is filtered due XSS attacks. A workaround might be using an `inline_template` render element
Lambic avatar
ph flag
Not really, I guess I could render my own anchor tag in an inline_template but I'd prefer to let the Link class do that for me.
ru flag
after `$link = $link->toRenderable()` there is nothing left of a link class anymore, you are in the world of render arrays now. A link object with a method like `setCssStyle()` would be a horrible mix of concerns.
Lambic avatar
ph flag
What I'm saying is I should be able to use the Link object to generate my render array then add attributes to it. Weirdly this works in drush php, but not in an event subscriber
ph flag

Well this turned out to not be about render arrays at all.

I was using devel_debug() to send the rendered string to my drupal_debug.txt, and it turns out devel_debug() passes the text it receives through a text filter which strips out the style tag. The rendered string does in fact contain the style attribute.

To confuse my situation further, the rendered string was getting passed to another drupal instance via an api call, and that instance was also stripping out the style tags.

Ultimately this came down to symfony_mailer module stripping out style tags from the email body.

I sit in a Tesla and translated this thread with Ai:


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.