public function ExampleMultilingualField::viewElements in Extra Field 8.2
Same name and namespace in other branches
- 8 modules/extra_field_example/src/Plugin/ExtraField/Display/ExampleMultilingualField.php \Drupal\extra_field_example\Plugin\ExtraField\Display\ExampleMultilingualField::viewElements()
Returns the renderable array of the field item(s).
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: The field's host entity.
Return value
array A renderable array of field elements. If this contains children, the field output will be rendered as a multiple value field with each child as a field item.
Overrides ExtraFieldDisplayFormattedInterface::viewElements
File
- modules/
extra_field_example/ src/ Plugin/ ExtraField/ Display/ ExampleMultilingualField.php, line 26
Class
- ExampleMultilingualField
- Example Extra field Display.
Namespace
Drupal\extra_field_example\Plugin\ExtraField\DisplayCode
public function viewElements(ContentEntityInterface $entity) {
$elements = [];
$cache = new CacheableMetadata();
$tagsField = $this
->getTagsField();
if ($tagsField && !$tagsField
->isEmpty()) {
// Build the field output as a concatenated string of tags.
$tags = [];
foreach ($tagsField as $item) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $tag */
$tag = $item->entity;
$tags[] = $tag
->label();
$cache
->addCacheableDependency($tag);
}
$elements = [
'#markup' => implode(', ', $tags),
];
}
else {
// Mark the result as empty to make sure no field wrapper is applied.
$this->isEmpty = TRUE;
}
$cache
->applyTo($elements);
return $elements;
}