public function NameFormatter::viewElements in Name Field 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/ NameFormatter.php, line 280
Class
- NameFormatter
- Plugin implementation of the 'name' formatter.
Namespace
Drupal\name\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
if (!$items
->count()) {
return $elements;
}
$settings = $this->settings;
$format = isset($settings['format']) ? $settings['format'] : 'default';
$is_multiple = $this->fieldDefinition
->getFieldStorageDefinition()
->isMultiple() && $items
->count() > 1;
$list_format = $is_multiple && !empty($settings['list_format']) ? $settings['list_format'] : '';
$extra = $this
->parseAdditionalComponents($items);
$extra['url'] = empty($settings['link_target']) ? NULL : $this
->getLinkableTargetUrl($items);
$item_array = [];
foreach ($items as $item) {
$components = $item
->toArray() + $extra;
$item_array[] = $components;
}
$this->formatter
->setSetting('markup', $this
->getSetting('markup'));
if ($list_format) {
$elements[0]['#markup'] = $this->formatter
->formatList($item_array, $format, $list_format, $langcode);
}
else {
foreach ($item_array as $delta => $item) {
$elements[$delta]['#markup'] = $this->formatter
->format($item, $format, $langcode);
}
}
return $elements;
}