View source
<?php
namespace Drupal\field_ui\Form;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\PluginSettingsInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field_ui\Element\FieldUiTable;
use Drupal\field_ui\FieldUI;
abstract class EntityDisplayFormBase extends EntityForm {
protected $displayContext;
protected $pluginManager;
protected $entityDisplayRepository;
protected $entityFieldManager;
protected $fieldTypes;
protected $entity;
public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, EntityDisplayRepositoryInterface $entity_display_repository = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) {
$this->fieldTypes = $field_type_manager
->getDefinitions();
$this->pluginManager = $plugin_manager;
if (!$entity_display_repository) {
@trigger_error('Calling EntityDisplayFormBase::__construct() with the $entity_display_repository argument is supported in Drupal 8.7.0 and will be required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_display_repository = \Drupal::service('entity_display.repository');
}
$this->entityDisplayRepository = $entity_display_repository;
if (!$entity_field_manager) {
@trigger_error('Calling EntityDisplayFormBase::__construct() with the $entity_field_manager argument is supported in Drupal 8.7.0 and will be required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityFieldManager = $entity_field_manager;
}
public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
$route_parameters = $route_match
->getParameters()
->all();
return $this
->getEntityDisplay($route_parameters['entity_type_id'], $route_parameters['bundle'], $route_parameters[$this->displayContext . '_mode_name']);
}
public function getRegions() {
return [
'content' => [
'title' => $this
->t('Content'),
'invisible' => TRUE,
'message' => $this
->t('No field is displayed.'),
],
'hidden' => [
'title' => $this
->t('Disabled', [], [
'context' => 'Plural',
]),
'message' => $this
->t('No field is hidden.'),
],
];
}
public function getRegionOptions() {
$options = [];
foreach ($this
->getRegions() as $region => $data) {
$options[$region] = $data['title'];
}
return $options;
}
protected function getFieldDefinitions() {
$context = $this->displayContext;
return array_filter($this->entityFieldManager
->getFieldDefinitions($this->entity
->getTargetEntityTypeId(), $this->entity
->getTargetBundle()), function (FieldDefinitionInterface $field_definition) use ($context) {
return $field_definition
->isDisplayConfigurable($context);
});
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$field_definitions = $this
->getFieldDefinitions();
$extra_fields = $this
->getExtraFields();
$form += [
'#entity_type' => $this->entity
->getTargetEntityTypeId(),
'#bundle' => $this->entity
->getTargetBundle(),
'#fields' => array_keys($field_definitions),
'#extra' => array_keys($extra_fields),
];
if (empty($field_definitions) && empty($extra_fields) && ($route_info = FieldUI::getOverviewRouteInfo($this->entity
->getTargetEntityTypeId(), $this->entity
->getTargetBundle()))) {
$this
->messenger()
->addWarning($this
->t('There are no fields yet added. You can add new fields on the <a href=":link">Manage fields</a> page.', [
':link' => $route_info
->toString(),
]));
return $form;
}
$table = [
'#type' => 'field_ui_table',
'#header' => $this
->getTableHeader(),
'#regions' => $this
->getRegions(),
'#attributes' => [
'class' => [
'field-ui-overview',
],
'id' => 'field-display-overview',
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'field-weight',
],
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'field-parent',
'subgroup' => 'field-parent',
'source' => 'field-name',
],
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'field-region',
'subgroup' => 'field-region',
'source' => 'field-name',
],
],
];
foreach ($field_definitions as $field_name => $field_definition) {
$table[$field_name] = $this
->buildFieldRow($field_definition, $form, $form_state);
}
foreach ($extra_fields as $field_id => $extra_field) {
$table[$field_id] = $this
->buildExtraFieldRow($field_id, $extra_field);
}
$form['fields'] = $table;
if ($this->entity
->getMode() == 'default') {
$display_mode_options = $this
->getDisplayModeOptions();
unset($display_mode_options['default']);
if ($display_mode_options) {
$form['modes'] = [
'#type' => 'details',
'#title' => $this
->t('Custom display settings'),
];
$default = [];
if ($enabled_displays = array_filter($this
->getDisplayStatuses())) {
$default = array_keys(array_intersect_key($display_mode_options, $enabled_displays));
}
natcasesort($display_mode_options);
$form['modes']['display_modes_custom'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Use custom display settings for the following @display_context modes', [
'@display_context' => $this->displayContext,
]),
'#options' => $display_mode_options,
'#default_value' => $default,
];
$form['modes']['display_modes_link'] = $this
->getDisplayModesLink();
}
}
$form['refresh_rows'] = [
'#type' => 'hidden',
];
$form['refresh'] = [
'#type' => 'submit',
'#value' => $this
->t('Refresh'),
'#op' => 'refresh_table',
'#submit' => [
'::multistepSubmit',
],
'#ajax' => [
'callback' => '::multistepAjax',
'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade',
'progress' => 'none',
],
'#attributes' => [
'class' => [
'visually-hidden',
],
],
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Save'),
];
$form['#attached']['library'][] = 'field_ui/drupal.field_ui';
return $form;
}
protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
$field_name = $field_definition
->getName();
$display_options = $this->entity
->getComponent($field_name);
$label = $field_definition
->getLabel();
if (empty($this
->getApplicablePluginOptions($field_definition))) {
$this->entity
->removeComponent($field_name)
->save();
$display_options = $this->entity
->getComponent($field_name);
}
$regions = array_keys($this
->getRegions());
$field_row = [
'#attributes' => [
'class' => [
'draggable',
'tabledrag-leaf',
],
],
'#row_type' => 'field',
'#region_callback' => [
$this,
'getRowRegion',
],
'#js_settings' => [
'rowHandler' => 'field',
'defaultPlugin' => $this
->getDefaultPlugin($field_definition
->getType()),
],
'human_name' => [
'#plain_text' => $label,
],
'weight' => [
'#type' => 'textfield',
'#title' => $this
->t('Weight for @title', [
'@title' => $label,
]),
'#title_display' => 'invisible',
'#default_value' => $display_options ? $display_options['weight'] : '0',
'#size' => 3,
'#attributes' => [
'class' => [
'field-weight',
],
],
],
'parent_wrapper' => [
'parent' => [
'#type' => 'select',
'#title' => $this
->t('Label display for @title', [
'@title' => $label,
]),
'#title_display' => 'invisible',
'#options' => array_combine($regions, $regions),
'#empty_value' => '',
'#attributes' => [
'class' => [
'js-field-parent',
'field-parent',
],
],
'#parents' => [
'fields',
$field_name,
'parent',
],
],
'hidden_name' => [
'#type' => 'hidden',
'#default_value' => $field_name,
'#attributes' => [
'class' => [
'field-name',
],
],
],
],
'region' => [
'#type' => 'select',
'#title' => $this
->t('Region for @title', [
'@title' => $label,
]),
'#title_display' => 'invisible',
'#options' => $this
->getRegionOptions(),
'#default_value' => $display_options ? $display_options['region'] : 'hidden',
'#attributes' => [
'class' => [
'field-region',
],
],
],
];
$field_row['plugin'] = [
'type' => [
'#type' => 'select',
'#title' => $this
->t('Plugin for @title', [
'@title' => $label,
]),
'#title_display' => 'invisible',
'#options' => $this
->getApplicablePluginOptions($field_definition),
'#default_value' => $display_options ? $display_options['type'] : 'hidden',
'#parents' => [
'fields',
$field_name,
'type',
],
'#attributes' => [
'class' => [
'field-plugin-type',
],
],
],
'settings_edit_form' => [],
];
$plugin = $this->entity
->getRenderer($field_name);
$base_button = [
'#submit' => [
'::multistepSubmit',
],
'#ajax' => [
'callback' => '::multistepAjax',
'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade',
],
'#field_name' => $field_name,
];
if ($form_state
->get('plugin_settings_edit') == $field_name) {
$field_row['plugin']['settings_edit_form'] = [];
if ($plugin) {
$settings_form = $plugin
->settingsForm($form, $form_state);
$third_party_settings_form = $this
->thirdPartySettingsForm($plugin, $field_definition, $form, $form_state);
if ($settings_form || $third_party_settings_form) {
$field_row['plugin']['#cell_attributes'] = [
'colspan' => 3,
];
$field_row['plugin']['settings_edit_form'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'field-plugin-settings-edit-form',
],
],
'#parents' => [
'fields',
$field_name,
'settings_edit_form',
],
'label' => [
'#markup' => $this
->t('Plugin settings'),
],
'settings' => $settings_form,
'third_party_settings' => $third_party_settings_form,
'actions' => [
'#type' => 'actions',
'save_settings' => $base_button + [
'#type' => 'submit',
'#button_type' => 'primary',
'#name' => $field_name . '_plugin_settings_update',
'#value' => $this
->t('Update'),
'#op' => 'update',
],
'cancel_settings' => $base_button + [
'#type' => 'submit',
'#name' => $field_name . '_plugin_settings_cancel',
'#value' => $this
->t('Cancel'),
'#op' => 'cancel',
'#limit_validation_errors' => [
[
'fields',
$field_name,
'type',
],
],
],
],
];
$field_row['#attributes']['class'][] = 'field-plugin-settings-editing';
}
}
}
else {
$field_row['settings_summary'] = [];
$field_row['settings_edit'] = [];
if ($plugin) {
$summary = $plugin
->settingsSummary();
$this
->alterSettingsSummary($summary, $plugin, $field_definition);
if (!empty($summary)) {
$field_row['settings_summary'] = [
'#type' => 'inline_template',
'#template' => '<div class="field-plugin-summary">{{ summary|safe_join("<br />") }}</div>',
'#context' => [
'summary' => $summary,
],
'#cell_attributes' => [
'class' => [
'field-plugin-summary-cell',
],
],
];
}
$settings_form = $plugin
->settingsForm($form, $form_state);
$third_party_settings_form = $this
->thirdPartySettingsForm($plugin, $field_definition, $form, $form_state);
if (!empty($settings_form) || !empty($third_party_settings_form)) {
$field_row['settings_edit'] = $base_button + [
'#type' => 'image_button',
'#name' => $field_name . '_settings_edit',
'#src' => 'core/misc/icons/787878/cog.svg',
'#attributes' => [
'class' => [
'field-plugin-settings-edit',
],
'alt' => $this
->t('Edit'),
],
'#op' => 'edit',
'#limit_validation_errors' => [
[
'fields',
$field_name,
'type',
],
],
'#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
'#suffix' => '</div>',
];
}
}
}
return $field_row;
}
protected function buildExtraFieldRow($field_id, $extra_field) {
$display_options = $this->entity
->getComponent($field_id);
$regions = array_keys($this
->getRegions());
$extra_field_row = [
'#attributes' => [
'class' => [
'draggable',
'tabledrag-leaf',
],
],
'#row_type' => 'extra_field',
'#region_callback' => [
$this,
'getRowRegion',
],
'#js_settings' => [
'rowHandler' => 'field',
],
'human_name' => [
'#markup' => $extra_field['label'],
],
'weight' => [
'#type' => 'textfield',
'#title' => $this
->t('Weight for @title', [
'@title' => $extra_field['label'],
]),
'#title_display' => 'invisible',
'#default_value' => $display_options ? $display_options['weight'] : 0,
'#size' => 3,
'#attributes' => [
'class' => [
'field-weight',
],
],
],
'parent_wrapper' => [
'parent' => [
'#type' => 'select',
'#title' => $this
->t('Parents for @title', [
'@title' => $extra_field['label'],
]),
'#title_display' => 'invisible',
'#options' => array_combine($regions, $regions),
'#empty_value' => '',
'#attributes' => [
'class' => [
'js-field-parent',
'field-parent',
],
],
'#parents' => [
'fields',
$field_id,
'parent',
],
],
'hidden_name' => [
'#type' => 'hidden',
'#default_value' => $field_id,
'#attributes' => [
'class' => [
'field-name',
],
],
],
],
'region' => [
'#type' => 'select',
'#title' => $this
->t('Region for @title', [
'@title' => $extra_field['label'],
]),
'#title_display' => 'invisible',
'#options' => $this
->getRegionOptions(),
'#default_value' => $display_options ? $display_options['region'] : 'hidden',
'#attributes' => [
'class' => [
'field-region',
],
],
],
'plugin' => [
'type' => [
'#type' => 'hidden',
'#value' => $display_options ? 'visible' : 'hidden',
'#parents' => [
'fields',
$field_id,
'type',
],
'#attributes' => [
'class' => [
'field-plugin-type',
],
],
],
],
'settings_summary' => [],
'settings_edit' => [],
];
return $extra_field_row;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($edit_field = $form_state
->get('plugin_settings_edit')) {
$form_state
->set('plugin_settings_update', $edit_field);
}
parent::submitForm($form, $form_state);
$form_values = $form_state
->getValues();
if ($this->entity
->getMode() == 'default' && !empty($form_values['display_modes_custom'])) {
$display_modes = $this
->getDisplayModes();
$current_statuses = $this
->getDisplayStatuses();
$statuses = [];
foreach ($form_values['display_modes_custom'] as $mode => $value) {
if (!empty($value) && empty($current_statuses[$mode])) {
if (!$this->entityTypeManager
->getStorage($this->entity
->getEntityTypeId())
->load($this->entity
->getTargetEntityTypeId() . '.' . $this->entity
->getTargetBundle() . '.' . $mode)) {
$display = $this
->getEntityDisplay($this->entity
->getTargetEntityTypeId(), $this->entity
->getTargetBundle(), 'default')
->createCopy($mode);
$display
->save();
}
$display_mode_label = $display_modes[$mode]['label'];
$url = $this
->getOverviewUrl($mode);
$this
->messenger()
->addStatus($this
->t('The %display_mode mode now uses custom display settings. You might want to <a href=":url">configure them</a>.', [
'%display_mode' => $display_mode_label,
':url' => $url
->toString(),
]));
}
$statuses[$mode] = !empty($value);
}
$this
->saveDisplayStatuses($statuses);
}
$this
->messenger()
->addStatus($this
->t('Your settings have been saved.'));
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$form_values = $form_state
->getValues();
if ($this->entity instanceof EntityWithPluginCollectionInterface) {
$form_values = array_diff_key($form_values, $this->entity
->getPluginCollections());
}
foreach ($form['#fields'] as $field_name) {
$values = $form_values['fields'][$field_name];
if ($values['region'] == 'hidden') {
$entity
->removeComponent($field_name);
}
else {
$options = $entity
->getComponent($field_name);
if ($form_state
->get('plugin_settings_update') === $field_name) {
$default_settings = $this->pluginManager
->getDefaultSettings($options['type']);
$options['settings'] = isset($values['settings_edit_form']['settings']) ? array_intersect_key($values['settings_edit_form']['settings'], $default_settings) : [];
$options['third_party_settings'] = isset($values['settings_edit_form']['third_party_settings']) ? $values['settings_edit_form']['third_party_settings'] : [];
$form_state
->set('plugin_settings_update', NULL);
}
$options['type'] = $values['type'];
$options['weight'] = $values['weight'];
$options['region'] = $values['region'];
if (isset($values['label'])) {
$options['label'] = $values['label'];
}
$entity
->setComponent($field_name, $options);
}
}
foreach ($form['#extra'] as $name) {
if ($form_values['fields'][$name]['region'] == 'hidden') {
$entity
->removeComponent($name);
}
else {
$entity
->setComponent($name, [
'weight' => $form_values['fields'][$name]['weight'],
'region' => $form_values['fields'][$name]['region'],
]);
}
}
}
public function multistepSubmit($form, FormStateInterface $form_state) {
$trigger = $form_state
->getTriggeringElement();
$op = $trigger['#op'];
switch ($op) {
case 'edit':
$field_name = $trigger['#field_name'];
$form_state
->set('plugin_settings_edit', $field_name);
break;
case 'update':
$field_name = $trigger['#field_name'];
$form_state
->set('plugin_settings_edit', NULL);
$form_state
->set('plugin_settings_update', $field_name);
$this->entity = $this
->buildEntity($form, $form_state);
break;
case 'cancel':
$form_state
->set('plugin_settings_edit', NULL);
break;
case 'refresh_table':
$updated_rows = explode(' ', $form_state
->getValue('refresh_rows'));
$plugin_settings_edit = $form_state
->get('plugin_settings_edit');
if ($plugin_settings_edit && in_array($plugin_settings_edit, $updated_rows)) {
$form_state
->set('plugin_settings_edit', NULL);
}
break;
}
$form_state
->setRebuild();
}
public function multistepAjax($form, FormStateInterface $form_state) {
$trigger = $form_state
->getTriggeringElement();
$op = $trigger['#op'];
switch ($op) {
case 'edit':
$updated_rows = [
$trigger['#field_name'],
];
$updated_columns = [
'plugin',
];
break;
case 'update':
case 'cancel':
$updated_rows = [
$trigger['#field_name'],
];
$updated_columns = [
'plugin',
'settings_summary',
'settings_edit',
];
break;
case 'refresh_table':
$updated_rows = array_values(explode(' ', $form_state
->getValue('refresh_rows')));
$updated_columns = [
'settings_summary',
'settings_edit',
];
break;
}
foreach ($updated_rows as $name) {
foreach ($updated_columns as $key) {
$element =& $form['fields'][$name][$key];
$element['#prefix'] = '<div class="ajax-new-content">' . (isset($element['#prefix']) ? $element['#prefix'] : '');
$element['#suffix'] = (isset($element['#suffix']) ? $element['#suffix'] : '') . '</div>';
}
}
return $form['fields'];
}
public function tablePreRender($elements) {
return FieldUiTable::tablePreRender($elements);
}
public function reduceOrder($array, $a) {
return FieldUiTable::reduceOrder($array, $a);
}
protected function getExtraFields() {
$context = $this->displayContext == 'view' ? 'display' : $this->displayContext;
$extra_fields = $this->entityFieldManager
->getExtraFields($this->entity
->getTargetEntityTypeId(), $this->entity
->getTargetBundle());
return isset($extra_fields[$context]) ? $extra_fields[$context] : [];
}
protected abstract function getEntityDisplay($entity_type_id, $bundle, $mode);
protected function getApplicablePluginOptions(FieldDefinitionInterface $field_definition) {
$options = $this->pluginManager
->getOptions($field_definition
->getType());
$applicable_options = [];
foreach ($options as $option => $label) {
$plugin_class = DefaultFactory::getPluginClass($option, $this->pluginManager
->getDefinition($option));
if ($plugin_class::isApplicable($field_definition)) {
$applicable_options[$option] = $label;
}
}
return $applicable_options;
}
protected abstract function getDefaultPlugin($field_type);
protected abstract function getDisplayModes();
protected abstract function getDisplayModeOptions();
protected abstract function getDisplayModesLink();
public function getRowRegion(&$row) {
$regions = $this
->getRegions();
if (!isset($regions[$row['region']['#value']])) {
$row['region']['#value'] = 'hidden';
}
return $row['region']['#value'];
}
protected function getDisplays() {
$load_ids = [];
$display_entity_type = $this->entity
->getEntityTypeId();
$entity_type = $this->entityTypeManager
->getDefinition($display_entity_type);
$config_prefix = $entity_type
->getConfigPrefix();
$ids = $this
->configFactory()
->listAll($config_prefix . '.' . $this->entity
->getTargetEntityTypeId() . '.' . $this->entity
->getTargetBundle() . '.');
foreach ($ids as $id) {
$config_id = str_replace($config_prefix . '.', '', $id);
list(, , $display_mode) = explode('.', $config_id);
if ($display_mode != 'default') {
$load_ids[] = $config_id;
}
}
return $this->entityTypeManager
->getStorage($display_entity_type)
->loadMultiple($load_ids);
}
protected function getDisplayStatuses() {
$display_statuses = [];
$displays = $this
->getDisplays();
foreach ($displays as $display) {
$display_statuses[$display
->get('mode')] = $display
->status();
}
return $display_statuses;
}
protected function saveDisplayStatuses($display_statuses) {
$displays = $this
->getDisplays();
foreach ($displays as $display) {
$new_status = $display_statuses[$display
->get('mode')];
if ($new_status !== $display
->status()) {
$display
->set('status', $new_status);
$display
->save();
}
}
}
protected abstract function getTableHeader();
protected abstract function getOverviewUrl($mode);
protected abstract function thirdPartySettingsForm(PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state);
protected abstract function alterSettingsSummary(array &$summary, PluginSettingsInterface $plugin, FieldDefinitionInterface $field_definition);
}