View source
<?php
namespace Drupal\field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class FieldFormatterBase extends EntityReferenceFormatterBase {
protected $entityTypeManager;
protected $viewDisplay;
protected $entityTypeBundleInfo;
protected $languageManager;
protected $entityFieldManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LanguageManagerInterface $language_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->languageManager = $language_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityFieldManager = $entity_field_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('language_manager'), $container
->get('entity_type.bundle.info'), $container
->get('entity_field.manager'));
}
protected abstract function getViewDisplay($bundle_id);
public static function defaultSettings() {
return parent::defaultSettings() + [
'link_to_entity' => FALSE,
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$entity_type = $this->entityTypeManager
->getDefinition($this->fieldDefinition
->getTargetEntityTypeId());
$form['link_to_entity'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Link to this @entity_label', [
'@entity_label' => $entity_type
->getLabel(),
]),
'#description' => $this
->t('Links the field to this (parent) entity (if supported by the field formatter, overriding referenced entity link settings).'),
'#default_value' => $this
->getSetting('link_to_entity'),
];
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
if ($field_name = $this
->getSetting('field_name')) {
$summary[] = $this
->t('Field %field_name displayed.', [
'%field_name' => $field_name,
]);
}
else {
$summary[] = $this
->t('Field not configured.');
}
if ($this
->getSetting('link_to_entity')) {
$entity_type = $this->entityTypeManager
->getDefinition($this->fieldDefinition
->getTargetEntityTypeId());
$summary[] = $this
->t('Linked to this (parent) @entity_label', [
'@entity_label' => $entity_type
->getLabel(),
]);
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$entities = $this
->getEntitiesToView($items, $langcode);
$build = [];
foreach ($entities as $delta => $entity) {
$viewDisplay = $this
->getViewDisplay($entity
->bundle());
if (!empty($viewDisplay)) {
$entityDisplay = $viewDisplay
->build($entity);
if ($this
->getSetting('link_to_entity') && ($entity = $items
->getEntity()) && $entity
->hasLinkTemplate('canonical') && !$entity
->isNew()) {
$entityUrl = $entity
->toUrl('canonical', [
'language' => $this->languageManager
->getLanguage($langcode),
]);
foreach ($entityDisplay as $field_name => &$field) {
$field[0]['#url'] = $entityUrl;
}
}
$build[$delta] = $entityDisplay;
}
}
return $build;
}
protected function getAvailableFieldNames() {
$field_names = [];
$entity_type_id = $this->fieldDefinition
->getSetting('target_type');
$bundle_info = $this->entityTypeBundleInfo;
$fieldDefinitionHandlerSettings = $this->fieldDefinition
->getSetting('handler_settings');
$target_bundles = empty($fieldDefinitionHandlerSettings['target_bundles']) ? array_keys($bundle_info
->getBundleInfo($entity_type_id)) : $fieldDefinitionHandlerSettings['target_bundles'];
foreach ($target_bundles as $value) {
$bundle_field_names = array_map(function (FieldDefinitionInterface $field_definition) {
return $field_definition
->getLabel();
}, $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $value));
$field_names = array_merge($field_names, $bundle_field_names);
}
return $field_names;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($field_definition
->getTargetEntityTypeId());
return $entity_type
->entityClassImplements(FieldableEntityInterface::class);
}
protected function getSettingFromFormState(FormStateInterface $form_state, $setting) {
$field_name = $this->fieldDefinition
->getName();
if ($form_state
->hasValue([
'fields',
$field_name,
'settings_edit_form',
'settings',
$setting,
])) {
return $form_state
->getValue([
'fields',
$field_name,
'settings_edit_form',
'settings',
$setting,
]);
}
return $this
->getSetting($setting);
}
}