public function MiconLinkFormatter::viewElements in Micon 8
Same name and namespace in other branches
- 2.x micon_link/src/Plugin/Field/FieldFormatter/MiconLinkFormatter.php \Drupal\micon_link\Plugin\Field\FieldFormatter\MiconLinkFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides LinkFormatter::viewElements
File
- micon_link/
src/ Plugin/ Field/ FieldFormatter/ MiconLinkFormatter.php, line 158
Class
- MiconLinkFormatter
- Plugin implementation of the 'micon_link' formatter.
Namespace
Drupal\micon_link\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = parent::viewElements($items, $langcode);
$entity = $items
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$title = $this
->getSetting('title');
$default_position = $this
->getSetting('position');
$text_only = $this
->getSetting('text_only');
foreach ($element as $delta => &$item) {
$icon = $this
->getSetting('icon');
if ($title && empty($items[$delta]->title)) {
$item['#title'] = $this->token
->replace($title, [
$entity_type => $entity,
]);
}
if (!$icon && !empty($item['#options']['attributes']['data-icon'])) {
$icon = $item['#options']['attributes']['data-icon'];
}
if ($icon) {
$position = !empty($item['#options']['attributes']['data-icon-position']) ? $item['#options']['attributes']['data-icon-position'] : $default_position;
$micon = $this
->micon($item['#title'])
->setIcon($icon);
if ($position == 'after') {
$micon
->setIconAfter();
}
$item['#title'] = $micon;
unset($item['#options']['attributes']['data-icon']);
}
if ($text_only) {
$item = [
'#markup' => $item['#title'],
];
}
}
return $element;
}