1
votes

I'm trying to figure out how to extend a common Link Field in Drupal 8 with an HTML entity (like ยป). My first try was a preprocess function for the field. Unfortunately I didn't manage to set the html option of the link to true Here's how I tried it

function MYTHEME_preprocess_field(&$variables) {
  if ($variables['element']['#field_name'] == 'field_slideshow_link'){
    foreach ($variables['items'] as $idx => $item) {
      $variables['items'][$idx]['content']['#title'] = $variables['items'][$idx]['content']['#title'] . " <span>&raquo;</span>";
      $variables['items'][$idx]['content']['#url']->setOption('html', true);
    }
  }
}

This didn't work. So the only solution I came up with was to manually generate a Link within the template. With just doesn't feel right. Here's how I did it

  <a href="{{ node.field_slideshow_link.0.url }}">{{ node.field_slideshow_link.0.title }} <span>&raquo;</span></a>

Has anyone an idea how to solve this problem more elegantly?

1

1 Answers

0
votes

Had trouble with this too. Found the following solution

$url = [
'#title' => new FormattableMarkup('&@text;', ['@text' => t('raquo')]);
//The rest
];

Did this with html elements like span so not sure if it will work with this. Let me know!