View source
<?php
namespace Drupal\readonly_field_widget\Plugin\Field\FieldWidget;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
class ReadonlyFieldWidget extends WidgetBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
private $fieldFormatterManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, $field_formatter_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->fieldFormatterManager = $field_formatter_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function defaultSettings() {
return [
'label' => 'above',
'formatter_type' => NULL,
'formatter_settings' => NULL,
'show_description' => FALSE,
];
}
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['third_party_settings'], $container
->get('plugin.manager.field.formatter'), $container
->get('entity_type.manager'));
}
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
return $this
->formSingleElement($items, 0, [], $form, $form_state);
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
if ($this
->isDefaultValueWidget($form_state)) {
return [
'#markup' => $this
->t('Widget is set to Read-Only, switch the widget to something else in order to set default values'),
];
}
$entity_type = $items
->getEntity()
->getEntityType()
->id();
$view_builder = $this->entityTypeManager
->getViewBuilder($entity_type);
$formatter_type = $this
->getSetting('formatter_type');
$formatter_settings = $this
->getSetting('formatter_settings');
$options = [
'type' => $formatter_type,
'label' => $this
->getSetting('label'),
'settings' => isset($formatter_settings[$formatter_type]) ? $formatter_settings[$formatter_type] : [],
];
$element['readonly_field'] = $view_builder
->viewField($items, $options);
if ($this
->getSetting('show_description') && !$items
->isEmpty()) {
$element['description'] = [
'#type' => 'container',
[
'#markup' => $this
->getFilteredDescription(),
],
'#attributes' => [
'class' => [
'description',
],
],
];
}
return $element;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$field_type_formatters = $this->fieldFormatterManager
->getOptions($this->fieldDefinition
->getType());
$field_type_definitions = $this->fieldFormatterManager
->getDefinitions();
$formatters = [];
foreach ($field_type_formatters as $formatter_type => $formatter_label) {
if (!empty($field_type_definitions[$formatter_type]) && $field_type_definitions[$formatter_type]['class']::isApplicable($this->fieldDefinition)) {
$formatters[$formatter_type] = $formatter_label;
}
}
$field_name = $this->fieldDefinition
->getName();
$element = [
'label' => [
'#title' => $this
->t('Label'),
'#type' => 'select',
'#options' => $this
->labelOptions(),
'#default_value' => $this
->getSetting('label'),
],
'formatter_type' => [
'#title' => $this
->t('Format'),
'#type' => 'select',
'#options' => $formatters,
'#default_value' => $this
->getSetting('formatter_type'),
],
'show_description' => [
'#title' => $this
->t('Show Description'),
'#description' => $this
->t('Show the configured description under widget.'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('show_description'),
],
];
foreach (array_keys($formatters) as $formatter_plugin_id) {
$formatter_plugin = $this
->getFormatterInstance($formatter_plugin_id);
$settings_form = $formatter_plugin
->settingsForm($form, $form_state);
if (!empty($settings_form)) {
$element['formatter_settings'][$formatter_plugin_id] = [
'#type' => 'fieldset',
'#title' => $formatters[$formatter_plugin_id] . ' ' . $this
->t('Settings'),
'#states' => [
'visible' => [
':input[name="fields[' . $field_name . '][settings_edit_form][settings][formatter_type]"]' => [
'value' => $formatter_plugin_id,
],
],
],
] + $settings_form;
}
}
return $element;
}
public function settingsSummary() {
$formatters = $this->fieldFormatterManager
->getOptions($this->fieldDefinition
->getType());
$label_options = $this
->labelOptions();
$plugin = $this
->getFormatterInstance();
if ($plugin) {
$summary = $plugin
->settingsSummary();
$formatter_type = $this
->getSetting('formatter_type');
if (isset($formatters[$formatter_type])) {
$summary[] = t('Format: @format', [
'@format' => $formatters[$formatter_type],
]);
}
}
$summary[] = t('Label: @label', [
'@label' => $label_options[$this
->getSetting('label')],
]);
$summary[] = t('Show Description: @show_desc', [
'@show_desc' => $this
->getSetting('show_description') ? $this
->t('Yes') : $this
->t('No'),
]);
return $summary;
}
private function getFormatterInstance($plugin_id = NULL) {
$settings = $this
->getSetting('formatter_settings');
if (empty($plugin_id)) {
$plugin_id = $this
->getSetting('formatter_type');
}
$options = [
'view_mode' => 'default',
'field_definition' => $this->fieldDefinition,
'configuration' => [
'type' => $plugin_id,
'settings' => isset($settings[$plugin_id]) ? $settings[$plugin_id] : [],
],
];
return $this->fieldFormatterManager
->getInstance($options);
}
private function labelOptions() {
return [
'above' => $this
->t('Above'),
'inline' => $this
->t('Inline'),
'hidden' => '- ' . $this
->t('Hidden') . ' -',
'visually_hidden' => '- ' . $this
->t('Visually Hidden') . ' -',
];
}
public function errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state) {
return FALSE;
}
public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
}
}