View source
<?php
namespace Drupal\field_formatter\Plugin\Field\FieldFormatter;
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;
abstract class FieldFormatterBase extends EntityReferenceFormatterBase {
protected $entityTypeManager;
protected $viewDisplay;
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()) {
$languageManager = \Drupal::languageManager();
$entityUrl = $entity
->toUrl('canonical', [
'language' => $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 = \Drupal::service('entity_type.bundle.info');
$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();
}, \Drupal::service('entity_field.manager')
->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
->isSubclassOf(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);
}
}