View source
<?php
namespace Drupal\field_collection_tabs\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldCollectionTabsFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $entityDisplayRepository;
protected $entityFieldManager;
protected $entityTypeManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityDisplayRepositoryInterface $entity_display_repository, EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->entityDisplayRepository = $entity_display_repository;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('entity_display.repository'), $container
->get('entity_field.manager'), $container
->get('entity_type.manager'));
}
public static function defaultSettings() {
return [
'title_field' => FALSE,
'view_mode' => FALSE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = array();
$options = array(
$this
->t('No titles'),
);
$field_definition = $this->fieldDefinition;
$fields = $this->entityFieldManager
->getFieldDefinitions('field_collection_item', $field_definition
->getName());
foreach ($fields as $field_name => $field) {
if ($field
->getFieldStorageDefinition()
->isBaseField() == FALSE && $field
->getType() == 'string') {
$options[$field_name] = $this
->t($field
->getLabel());
}
}
$elements['title_field'] = array(
'#type' => 'select',
'#title' => 'Field to use for tab titles',
'#description' => t('Select the field to use for tab titles'),
'#default_value' => $this
->getSetting('title_field'),
'#options' => $options,
);
$displays = $this->entityDisplayRepository
->getAllViewModes();
if (isset($displays['field_collection_item']) && !empty($displays['field_collection_item'])) {
$displays = $displays['field_collection_item'];
$options = array(
$this
->t('Default'),
);
foreach ($displays as $view_mode => $info) {
$options[$view_mode] = $info['label'];
}
$elements['view_mode'] = array(
'#type' => 'select',
'#title' => t('View mode'),
'#options' => $options,
'#default_value' => $this
->getSetting('view_mode'),
'#description' => t('Select the view mode'),
);
}
else {
$elements['view_mode'] = array(
'#markup' => $this
->t('No custom view modes for Field Collection Items'),
);
}
return $elements;
}
public function settingsSummary() {
$summary = [];
$summary[] = $this
->getSetting('title_field') ? $this
->t('Title field: ' . $this
->getSetting('title_field')) : $this
->t('Numbered Tabs');
$summary[] = $this
->getsetting('view_mode') ? $this
->t('View Mode: ' . $this
->getSetting('view_mode')) : $this
->t('View Mode: Full');
return $summary;
}
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;
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;
}
}