View source
<?php
namespace Drupal\diff\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\diff\DiffBuilderManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Form\FormState;
class FieldsSettingsForm extends ConfigFormBase {
protected $entityTypeManager;
protected $entityFieldManager;
protected $fieldTypePluginManager;
protected $diffBuilderManager;
public function __construct(ConfigFactoryInterface $config_factory, PluginManagerInterface $plugin_manager, DiffBuilderManager $diff_builder_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($config_factory);
$this->fieldTypePluginManager = $plugin_manager;
$this->diffBuilderManager = $diff_builder_manager;
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.field.field_type'), $container
->get('plugin.manager.diff.builder'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'));
}
public function getFormId() {
return 'diff_admin_plugins';
}
protected function getEditableConfigNames() {
return [
'diff.plugins',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['fields'] = array(
'#type' => 'table',
'#tree' => TRUE,
'#header' => $this
->getTableHeader(),
'#empty' => $this
->t('No field types found.'),
'#prefix' => '<div id="field-display-overview-wrapper">',
'#suffix' => '</div>',
'#attributes' => array(
'class' => array(
'field-ui-overview',
),
'id' => 'field-display-overview',
),
);
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_name => $entity_type) {
if (!$entity_type
->isRevisionable()) {
continue;
}
$field_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($entity_type_name);
foreach ($field_definitions as $field_name => $field_definition) {
$show_diff = $this->diffBuilderManager
->showDiff($field_definition);
if (!$show_diff) {
continue;
}
$key = $entity_type_name . '__' . $field_name;
$form['fields'][$key] = $this
->buildFieldRow($entity_type, $field_definition, $form_state);
}
}
$this->diffBuilderManager
->clearCachedDefinitions();
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Save'),
);
$form['#attached']['library'][] = 'field_ui/drupal.field_ui';
$form['#attached']['library'][] = 'diff/diff.general';
return $form;
}
protected function buildFieldRow(EntityTypeInterface $entity_type, FieldStorageDefinitionInterface $field_definition, FormStateInterface $form_state) {
$entity_type_label = $entity_type
->getLabel();
$field_name = $field_definition
->getName();
$field_type = $field_definition
->getType();
$field_key = $entity_type
->id() . '__' . $field_name;
$display_options = $this->diffBuilderManager
->getSelectedPluginForFieldStorageDefinition($field_definition);
$plugin_options = $this->diffBuilderManager
->getApplicablePluginOptions($field_definition);
$base_button = [
'#submit' => [
[
$this,
'multiStepSubmit',
],
],
'#ajax' => [
'callback' => [
$this,
'multiStepAjax',
],
'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade',
],
'#field_key' => $field_key,
];
$field_row['entity_type'] = [
'#markup' => $entity_type_label,
];
$labels = _diff_field_label($entity_type
->id(), $field_name);
$field_row['field_label'] = [
'#markup' => array_shift($labels),
];
$field_type_label = $this->fieldTypePluginManager
->getDefinitions()[$field_type]['label'];
$field_row['field_type'] = [
'#markup' => $field_type_label,
];
if ($type = $form_state
->getValue([
'fields',
$field_key,
'plugin',
'type',
])) {
$display_options['type'] = $type;
}
$plugin_settings = $form_state
->get('plugin_settings');
if (isset($plugin_settings[$field_key]['settings'])) {
$modified = FALSE;
if (!empty($display_options['settings'])) {
foreach ($display_options['settings'] as $key => $value) {
if ($plugin_settings[$field_key]['settings'][$key] != $value) {
$modified = TRUE;
break;
}
}
}
if ($modified && empty($_SESSION['messages']['warning'])) {
$this
->messenger()
->addWarning($this
->t('You have unsaved changes.'));
}
$display_options['settings'] = $plugin_settings[$field_key]['settings'];
}
$field_row['plugin'] = array(
'type' => array(
'#type' => 'select',
'#options' => $plugin_options,
'#empty_option' => $this
->t("- Don't compare -"),
'#empty_value' => 'hidden',
'#title_display' => 'invisible',
'#attributes' => array(
'class' => array(
'field-plugin-type',
),
),
'#default_value' => $display_options,
'#ajax' => array(
'callback' => [
$this,
'multiStepAjax',
],
'method' => 'replace',
'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade',
),
'#field_key' => $field_key,
),
'settings_edit_form' => array(),
);
$plugin = $this
->getPlugin($display_options);
if ($form_state
->get('plugin_settings_edit') == $field_key) {
$field_row['plugin']['settings_edit_form'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'field-plugin-settings-edit-form',
),
),
'#parents' => [
'fields',
$field_key,
'settings_edit_form',
],
'label' => array(
'#markup' => $this
->t('Plugin settings:' . ' <span class="plugin-name">' . $plugin_options[$display_options['type']] . '</span>'),
),
'settings' => $plugin
->buildConfigurationForm(array(), $form_state),
'actions' => array(
'#type' => 'actions',
'save_settings' => $base_button + [
'#type' => 'submit',
'#button_type' => 'primary',
'#name' => $field_key . '_plugin_settings_update',
'#value' => $this
->t('Update'),
'#op' => 'update',
],
'cancel_settings' => $base_button + [
'#type' => 'submit',
'#name' => $field_key . '_plugin_settings_cancel',
'#value' => $this
->t('Cancel'),
'#op' => 'cancel',
'#limit_validation_errors' => [
[
'fields',
$field_key,
'plugin',
'type',
],
],
],
),
);
$field_row['settings_edit'] = array();
$field_row['#attributes']['class'][] = 'field-plugin-settings-editing';
}
else {
$field_row['settings_edit'] = [];
if ($plugin) {
$field_row['settings_edit'] = $base_button + array(
'#type' => 'image_button',
'#name' => $field_key . '_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_key,
'plugin',
'type',
],
],
'#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
'#suffix' => '</div>',
);
}
}
return $field_row;
}
public function multiStepSubmit(array $form, FormStateInterface $form_state) {
$trigger = $form_state
->getTriggeringElement();
$op = $trigger['#op'];
switch ($op) {
case 'edit':
$field_key = $trigger['#field_key'];
$form_state
->set('plugin_settings_edit', $field_key);
break;
case 'update':
$field_key = $trigger['#field_key'];
if ($plugin_settings = $form_state
->getValue([
'fields',
$field_key,
'settings_edit_form',
'settings',
])) {
$form_state
->set([
'plugin_settings',
$field_key,
'settings',
], $plugin_settings);
}
$form_state
->set('plugin_settings_edit', NULL);
break;
case 'cancel':
$form_state
->set('plugin_settings_edit', NULL);
break;
}
$form_state
->setRebuild();
}
public function multiStepAjax(array $form, FormStateInterface $form_state) {
$trigger = $form_state
->getTriggeringElement();
if (isset($trigger['#op'])) {
$op = $trigger['#op'];
$updated_rows = [];
$updated_columns = [];
switch ($op) {
case 'edit':
$updated_rows = [
$trigger['#field_key'],
];
$updated_columns = array(
'plugin',
);
break;
case 'update':
case 'cancel':
$updated_rows = [
$trigger['#field_key'],
];
$updated_columns = array(
'plugin',
'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 validateForm(array &$form, FormStateInterface $form_state) {
$form_values = $form_state
->getValues();
$plugin_settings = $form_state
->get('plugin_settings');
$fields = $form_values['fields'];
foreach ($fields as $field_key => $field_values) {
if ($field_values['plugin']['type'] != 'hidden') {
$settings = array();
$key = NULL;
if (isset($field_values['settings_edit_form']['settings'])) {
$settings = $field_values['settings_edit_form']['settings'];
$key = 1;
}
elseif (isset($plugin_settings[$field_key]['settings'])) {
$settings = $plugin_settings[$field_key]['settings'];
$key = 2;
}
if (!empty($settings)) {
$state = new FormState();
$state
->setValues($settings);
$state
->set('fields', $field_key);
$plugin = $this->diffBuilderManager
->createInstance($field_values['plugin']['type'], []);
$plugin
->validateConfigurationForm($form, $state);
if ($key == 1) {
$form_state
->setValue([
'fields',
$field_key,
'settings_edit_form',
'settings',
], $state
->getValues());
}
elseif ($key == 2) {
$form_state
->set([
'plugin_settings',
$field_key,
'settings',
], $state
->getValues());
}
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_values = $form_state
->getValues();
$plugin_settings = $form_state
->get('plugin_settings');
$fields = $form_values['fields'];
$config = $this
->config('diff.plugins');
foreach ($fields as $field_key => $field_values) {
$config_key = preg_replace('/__/', '.', $field_key, 1);
if ($field_values['plugin']['type'] == 'hidden') {
$config
->set('fields.' . $config_key, [
'type' => 'hidden',
'settings' => [],
]);
}
else {
$configuration = [];
if ($config
->get('fields.' . $config_key . '.type') == $field_values['plugin']['type'] && $config
->get('fields.' . $config_key . '.settings')) {
$configuration = $config
->get('fields.' . $config_key . '.settings');
}
$plugin = $this->diffBuilderManager
->createInstance($field_values['plugin']['type'], $configuration);
$values = NULL;
if (isset($field_values['settings_edit_form']['settings'])) {
$values = $field_values['settings_edit_form']['settings'];
}
elseif (isset($plugin_settings[$field_key]['settings'])) {
$values = $plugin_settings[$field_key]['settings'];
}
if ($values) {
$state = new FormState();
$state
->setValues($values);
$plugin
->submitConfigurationForm($form, $state);
}
$config
->set('fields.' . $config_key, [
'type' => $field_values['plugin']['type'],
'settings' => $plugin
->getConfiguration(),
]);
}
}
$config
->save();
$this
->messenger()
->addStatus($this
->t('Your settings have been saved.'));
}
protected function getPlugin(array $configuration) {
if ($configuration && isset($configuration['type']) && $configuration['type'] != 'hidden') {
if (!isset($configuration['settings'])) {
$configuration['settings'] = array();
}
return $this->diffBuilderManager
->createInstance($configuration['type'], $configuration['settings']);
}
return NULL;
}
protected function getTableHeader() {
return array(
'entity_type' => $this
->t('Entity Type'),
'field_name' => $this
->t('Field'),
'field_type' => $this
->t('Field Type'),
'plugin' => $this
->t('Plugin'),
'settings_edit' => '',
);
}
}