View source
<?php
namespace Drupal\ctools_block\Plugin\Block;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityField extends BlockBase implements ContextAwarePluginInterface, ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityFieldManager;
protected $fieldTypeManager;
protected $formatterManager;
protected $entityTypeId;
protected $fieldName;
protected $fieldDefinition;
protected $fieldStorageDefinition;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, FormatterPluginManager $formatter_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->fieldTypeManager = $field_type_manager;
$this->formatterManager = $formatter_manager;
list(, $entity_type_id, $field_name) = explode(':', $plugin_id);
$this->entityTypeId = $entity_type_id;
$this->fieldName = $field_name;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('plugin.manager.field.formatter'));
}
public function build() {
$entity = $this
->getContextValue('entity');
$build = [];
$field = $entity->{$this->fieldName};
$display_settings = $this
->getConfiguration()['formatter'];
$build['field'] = $field
->view($display_settings);
$build['#cache']['contexts'] = $this
->getCacheContexts();
$build['#cache']['tags'] = $this
->getCacheTags();
$build['#cache']['max-age'] = $this
->getCacheMaxAge();
return $build;
}
protected function blockAccess(AccountInterface $account) {
$entity = $this
->getContextValue('entity');
$access = $entity
->access('view', $account, TRUE);
if ($access
->isAllowed()) {
if ($entity instanceof FieldableEntityInterface && $entity
->hasField($this->fieldName)) {
$field_access = $this->entityTypeManager
->getAccessControlHandler($this->entityTypeId)
->fieldAccess('view', $this
->getFieldDefinition(), $account);
if ($field_access) {
$build = $entity
->get($this->fieldName)
->view($this->configuration['formatter']);
if (Element::children($build)) {
return AccessResult::allowed();
}
}
}
return AccessResult::forbidden();
}
return $access;
}
public function defaultConfiguration() {
$field_type_definition = $this
->getFieldTypeDefinition();
return [
'formatter' => [
'label' => 'above',
'type' => isset($field_type_definition['default_formatter']) ? $field_type_definition['default_formatter'] : '',
'settings' => [],
'third_party_settings' => [],
'weight' => 0,
],
];
}
public function blockForm($form, FormStateInterface $form_state) {
$config = $this
->getConfiguration();
$form['formatter_label'] = [
'#type' => 'select',
'#title' => $this
->t('Label'),
'#options' => [
'above' => $this
->t('Above'),
'inline' => $this
->t('Inline'),
'hidden' => '- ' . $this
->t('Hidden') . ' -',
'visually_hidden' => '- ' . $this
->t('Visually Hidden') . ' -',
],
'#default_value' => $config['formatter']['label'],
];
$form['formatter_type'] = [
'#type' => 'select',
'#title' => $this
->t('Formatter'),
'#options' => $this
->getFormatterOptions(),
'#default_value' => $config['formatter']['type'],
'#ajax' => [
'callback' => [
static::class,
'formatterSettingsAjaxCallback',
],
'wrapper' => 'formatter-settings-wrapper',
'effect' => 'fade',
],
];
$form['#process'][] = [
$this,
'formatterSettingsProcessCallback',
];
$form['formatter_settings_wrapper'] = [
'#prefix' => '<div id="formatter-settings-wrapper">',
'#suffix' => '</div>',
];
$form['formatter_settings_wrapper']['formatter_settings'] = [
'#tree' => TRUE,
];
return $form;
}
public function formatterSettingsProcessCallback(array &$element, FormStateInterface $form_state, array &$complete_form) {
$config = $this
->getConfiguration();
$parents_base = $element['#parents'];
$formatter_parent = array_merge($parents_base, [
'formatter_type',
]);
$formatter_settings_parent = array_merge($parents_base, [
'formatter_settings',
]);
$settings_element =& $element['formatter_settings_wrapper']['formatter_settings'];
$settings_element['#parents'] = $formatter_settings_parent;
$formatter_name = NestedArray::getValue($form_state
->getUserInput(), $formatter_parent) ?: $element['formatter_type']['#default_value'];
$formatter = $this
->getFormatter($formatter_name, $form_state
->getValue('formatter_label'), $form_state
->getValue($formatter_settings_parent, $config['formatter']['settings']), $config['formatter']['third_party_settings']);
$settings_element = array_merge($formatter
->settingsForm($settings_element, $form_state), $settings_element);
$complete_form['#formatter_array_parents'] = $element['#array_parents'];
return $element;
}
public static function formatterSettingsAjaxCallback(array $form, FormStateInterface $form_state) {
$formatter_array_parents = $form['#formatter_array_parents'];
return NestedArray::getValue($form, array_merge($formatter_array_parents, [
'formatter_settings_wrapper',
]));
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['formatter']['label'] = $form_state
->getValue('formatter_label');
$this->configuration['formatter']['type'] = $form_state
->getValue('formatter_type');
$this->configuration['formatter']['settings'] = (array) $form_state
->getValue('formatter_settings');
}
protected function getFieldDefinition() {
if (empty($this->fieldDefinition)) {
$field_map = $this->entityFieldManager
->getFieldMap();
$bundle = reset($field_map[$this->entityTypeId][$this->fieldName]['bundles']);
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($this->entityTypeId, $bundle);
$this->fieldDefinition = $field_definitions[$this->fieldName];
}
return $this->fieldDefinition;
}
protected function getFieldStorageDefinition() {
if (empty($this->fieldStorageDefinition)) {
$field_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($this->entityTypeId);
$this->fieldStorageDefinition = $field_definitions[$this->fieldName];
}
return $this->fieldStorageDefinition;
}
protected function getFieldTypeDefinition() {
return $this->fieldTypeManager
->getDefinition($this
->getFieldStorageDefinition()
->getType());
}
protected function getFormatterOptions() {
return $this->formatterManager
->getOptions($this
->getFieldStorageDefinition()
->getType());
}
protected function getFormatter($type, $label, array $settings, array $third_party_settings) {
return $this->formatterManager
->createInstance($type, [
'field_definition' => $this
->getFieldDefinition(),
'view_mode' => 'default',
'prepare' => TRUE,
'label' => $label,
'settings' => $settings,
'third_party_settings' => $third_party_settings,
]);
}
public function __wakeup() {
parent::__wakeup();
$this->fieldStorageDefinition = NULL;
}
}