Last active
November 29, 2017 12:08
-
-
Save normanlolx/177954fd61a5ef880bd6bb925b86682c to your computer and use it in GitHub Desktop.
Drupal 8 add external link field class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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