View source
<?php
namespace Drupal\field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldFormatterFromViewDisplay extends FieldFormatterBase implements ContainerFactoryPluginInterface {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$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_type.manager'));
}
public static function defaultSettings() {
$settings = parent::defaultSettings() + [
'view_mode' => 'default',
'field_name' => '',
];
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$options = [];
$er_target_entity_type = $this->fieldDefinition
->getSetting('target_type');
$er_handler_settings = $this->fieldDefinition
->getSetting('handler_settings');
if (!empty($er_handler_settings['target_bundles'])) {
$er_target_bundles = $er_handler_settings['target_bundles'];
foreach ($er_target_bundles as $er_target_bundle => $er_target_bundle_val) {
$optionsByBundle = \Drupal::service('entity_display.repository')
->getViewModeOptionsByBundle($er_target_entity_type, $er_target_bundle);
foreach ($optionsByBundle as $key => $option) {
$options[$key] = $option;
}
}
}
else {
$options = \Drupal::service('entity_display.repository')
->getViewModeOptions($er_target_entity_type);
}
asort($options);
$form['view_mode'] = [
'#title' => $this
->t('View mode'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $this
->getSetting('view_mode'),
'#empty_option' => 'Default',
'#empty_value' => 'default',
];
$form['field_name'] = [
'#type' => 'select',
'#title' => $this
->t('Field name'),
'#default_value' => $this
->getSetting('field_name'),
'#options' => $this
->getAvailableFieldNames(),
];
return $form;
}
protected function getViewDisplay($bundle_id) {
if (!isset($this->viewDisplay[$bundle_id])) {
$field_name = $this
->getSetting('field_name');
$entity_type_id = $this->fieldDefinition
->getSetting('target_type');
if (($view_mode = $this
->getSetting('view_mode')) && ($view_display = EntityViewDisplay::load($entity_type_id . '.' . $bundle_id . '.' . $view_mode))) {
$components = $view_display
->getComponents();
foreach ($components as $component_name => $component) {
if ($component_name != $field_name) {
$view_display
->removeComponent($component_name);
}
}
$this->viewDisplay[$bundle_id] = $view_display;
}
}
return $this->viewDisplay[$bundle_id];
}
public function settingsSummary() {
$summary = parent::settingsSummary();
if ($view_mode = $this
->getSetting('view_mode')) {
$summary[] = $this
->t('View display %view_mode used.', [
'%view_mode' => $view_mode,
]);
}
else {
$summary[] = $this
->t('View display not configured.');
}
return $summary;
}
}