public function FieldCollectionTabsFormatter::viewElements in Field Collection Tab formatter 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/ FieldCollectionTabsFormatter.php, line 143
Class
- FieldCollectionTabsFormatter
- Plugin implementation of the 'tabs' formatter.
Namespace
Drupal\field_collection_tabs\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$titles = [];
$tabs = [];
$title_field = !empty($this
->getSetting('title_field')) ? $this
->getSetting('title_field') : FALSE;
$view_mode = !empty($this
->getSetting('view_mode')) ? $this
->getSetting('view_mode') : 'full';
foreach ($items as $delta => $item) {
if ($item->value !== NULL) {
$field_collection_item = $item
->getFieldCollectionItem();
$title = $field_collection_item
->get($title_field)->value;
// Preventing a tab from not having a title, seems bad to not have a title for a tab.
if ($title == '' || $title_field == FALSE) {
$title = "Tab " . ($delta + 1);
}
$titles[] = $title;
$render_item = $this->entityTypeManager
->getViewBuilder('field_collection_item')
->view($field_collection_item, $view_mode);
$tabs[] = $render_item;
}
}
$render_array = [
'#theme' => 'field_collection_tabs',
'#titles' => $titles,
'#tabs' => $tabs,
'#field_name' => $title_field,
'#attached' => [
'library' => [
'field_collection_tabs/field_collection_tabs',
],
],
];
return $render_array;
}