public function IconSelectFormatterDefault::viewElements in Icon Select 8
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 FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ IconSelectFormatterDefault.php, line 120
Class
- IconSelectFormatterDefault
- Plugin implementation of the icon_select_field text formatter.
Namespace
Drupal\icon_select\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$icons = $items
->referencedEntities();
foreach ($icons as $delta => $icon) {
/** @var \Drupal\Core\Template\Attribute $attributes */
$attributes = new Attribute();
if ($this
->getSetting('apply_dimensions')) {
foreach ([
'width',
'height',
] as $attribute_key) {
if (!empty($attribute_value = $this
->getSetting($attribute_key))) {
$attributes[$attribute_key] = $attribute_value;
}
}
}
// Prepare classes.
$attributes
->addClass('icon', 'icon--' . $icon->field_symbol_id->value);
if ($icon->field_svg_file->entity) {
$elements[$delta] = [
'#theme' => 'icon_select_svg_icon',
'#attributes' => $attributes,
'#symbol_id' => $icon->field_symbol_id->value,
];
}
}
// Attach css / js library.
if (count($elements)) {
$elements['#attached'] = [
'library' => [
'icon_select/drupal.icon_select',
],
];
}
return $elements;
}