Skip to content

Instantly share code, notes, and snippets.

@normanlolx
Last active November 29, 2017 12:08
Show Gist options
  • Save normanlolx/177954fd61a5ef880bd6bb925b86682c to your computer and use it in GitHub Desktop.
Save normanlolx/177954fd61a5ef880bd6bb925b86682c to your computer and use it in GitHub Desktop.
Drupal 8 add external link field class
<?php
use Drupal\Core\Render\Element;
/**
* Implements template_preprocess_field().
*
* @param array $variables
*/
function MYTHEME_preprocess_field(&$variables) {
$element = $variables['element'];
if ($element['#field_type'] === 'link') {
$items = Element::children($element);
foreach ($items as $key) {
$options = $element[$key]['#options'];
if (isset($options['external']) && $options['external'] === TRUE) {
$variables['items'][$key]['content']['#options']['attributes']['class'][] = 'link-external';
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment