diff --git a/diff.links.task.yml b/diff.links.task.yml index 9a904c9..caa8a36 100644 --- a/diff.links.task.yml +++ b/diff.links.task.yml @@ -6,4 +6,4 @@ diff.general_settings: diff.fields_list: route_name: diff.fields_list base_route: diff.general_settings - title: Configurable Fields + title: Fields diff --git a/diff.services.yml b/diff.services.yml index c3cff31..2c3e1a0 100755 --- a/diff.services.yml +++ b/diff.services.yml @@ -14,13 +14,10 @@ services: diff.entity_parser: class: Drupal\diff\DiffEntityParser - arguments: ['@plugin.manager.diff.builder', '@entity.manager', '@config.factory'] + arguments: ['@plugin.manager.diff.builder', '@entity_type.manager', '@config.factory'] diff.breadcrumb: class: Drupal\diff\DiffBreadcrumbBuilder parent: system.breadcrumb.default tags: - { name: breadcrumb_builder, priority: 1000 } - - diff.diff_fields: - class: Drupal\diff\DiffFields diff --git a/src/DiffBuilderManager.php b/src/DiffBuilderManager.php index 3f5d0c0..b942ddf 100644 --- a/src/DiffBuilderManager.php +++ b/src/DiffBuilderManager.php @@ -9,6 +9,7 @@ namespace Drupal\diff; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Plugin\DefaultPluginManager; /** @@ -36,4 +37,29 @@ class DiffBuilderManager extends DefaultPluginManager { $this->alterInfo('field_diff_builder_info'); } + /** + * Define whether a field should be displayed or not as a diff change. + * + * Check if the field is not a key and revisionable to define if it should be + * displayed in the diff comparison. + * + * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition + * The field storage definition. + * + * @return bool + * TRUE if the field will be displayed. + */ + public function showDiff(FieldStorageDefinitionInterface $field_storage_definition) { + $show_diff = FALSE; + // Check if the field is revisionable. + if ($field_storage_definition->isRevisionable()) { + $show_diff = TRUE; + // Check if the field is bundle or revision, if so don't dislay it. + $entity_type = \Drupal::entityTypeManager()->getDefinition($field_storage_definition->getTargetEntityTypeId()); + if (in_array($field_storage_definition->getName(), [$entity_type->getKey('bundle'), $entity_type->getKey('revision')])) { + $show_diff = FALSE; + } + } + return $show_diff; + } } diff --git a/src/DiffEntityParser.php b/src/DiffEntityParser.php index 3dece0f..3115181 100644 --- a/src/DiffEntityParser.php +++ b/src/DiffEntityParser.php @@ -9,7 +9,7 @@ namespace Drupal\diff; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\ContentEntityInterface; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Language\LanguageInterface; class DiffEntityParser { @@ -36,20 +36,20 @@ class DiffEntityParser { * * @var \Drupal\Core\Entity\EntityManagerInterface */ - protected $entityManager; + protected $entityTypeManager; /** * Constructs an EntityComparisonBase object. * * @param DiffBuilderManager $diffBuilderManager * The diff field builder plugin manager. - * @param EntityManagerInterface $entityManager + * @param EntityTypeManagerInterface $entityTypeManager * Entity Manager service. * @param ConfigFactoryInterface $configFactory * The configuration factory. */ - public function __construct(DiffBuilderManager $diffBuilderManager, EntityManagerInterface $entityManager, ConfigFactoryInterface $configFactory) { - $this->entityManager = $entityManager; + public function __construct(DiffBuilderManager $diffBuilderManager, EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $configFactory) { + $this->entityManager = $entityTypeManager; $this->config = $configFactory->get('diff.settings'); $this->pluginsConfig = $configFactory->get('diff.plugins'); $this->diffBuilderManager = $diffBuilderManager; @@ -78,6 +78,7 @@ class DiffEntityParser { } $entity_type_id = $entity->getEntityTypeId(); + // Load the diff plugin definitions. $diff_plugin_definitions = $this->diffBuilderManager->getDefinitions(); $plugins = []; foreach ($diff_plugin_definitions as $plugin_definition) { @@ -95,27 +96,31 @@ class DiffEntityParser { foreach ($entity as $field_items) { $field_name = $field_items->getFieldDefinition()->getName(); - $show_diff = \Drupal::service('diff.diff_fields')->showDiff($field_items->getFieldDefinition()->getFieldStorageDefinition()); + // Define if the current field should be displayed as a diff change. + $show_diff = $this->diffBuilderManager->showDiff($field_items->getFieldDefinition()->getFieldStorageDefinition()); if (!$show_diff) { continue; } + // Get the plugin configuration set for this field. $plugin_config = $this->pluginsConfig->get('fields.' . $entity_type_id . '.' . $field_name); - // @todo if there is no plugin settings override, pick the first. + // Load the plugins that can be applied to this field. $plugin_options = []; if (isset($plugins[$field_items->getFieldDefinition()->getType()])) { foreach ($plugins[$field_items->getFieldDefinition()->getType()] as $id) { $plugin_options[$id] = $diff_plugin_definitions[$id]['label']; } } + $plugin = NULL; + // If there is no a plugin defined take the first available. if (!$plugin_config) { $default_plugin['type'] = array_keys($plugin_options)[0]; $plugin = $this->diffBuilderManager->createInstance($default_plugin['type'], []); } - + // If there is a plugin defined create an instance of it. if ($plugin_config && $plugin_config['type'] != 'hidden') { $plugin = $this->diffBuilderManager->createInstance($plugin_config['type'], $plugin_config['settings']); } diff --git a/src/DiffFields.php b/src/DiffFields.php deleted file mode 100644 index 292a7d6..0000000 --- a/src/DiffFields.php +++ /dev/null @@ -1,28 +0,0 @@ -isRevisionable()) { - $show_diff = TRUE; - $entity_type = \Drupal::entityTypeManager()->getDefinition($field_storage_definition->getTargetEntityTypeId()); - if (in_array($field_storage_definition->getName(), [$entity_type->getKey('bundle'), $entity_type->getKey('revision')])) { - $show_diff = FALSE; - } - } - return $show_diff; - } -} diff --git a/src/EntityComparisonBase.php b/src/EntityComparisonBase.php index 793baae..a29f868 100644 --- a/src/EntityComparisonBase.php +++ b/src/EntityComparisonBase.php @@ -118,11 +118,11 @@ class EntityComparisonBase extends ControllerBase { foreach ($left_values as $field_name => $values) { $field_definition = $left_entity->getFieldDefinition($field_name); - $left_key = $left_entity->getEntityTypeId() . '.' .$field_definition->getName(); + $left_key = $left_entity->getEntityTypeId() . '.' . $field_definition->getName(); // Get the compare settings for this field type. $compare_settings = $this->pluginsConfig->get('fields.' . $left_key); $result[$left_key] = [ - '#name' => ($compare_settings['settings']['show_header'] == 0) ? '' : $field_definition->getLabel(), + '#name' => (isset($compare_settings['settings']['show_header']) && $compare_settings['settings']['show_header'] == 0) ? '' : $field_definition->getLabel(), '#settings' => $compare_settings, ]; @@ -142,10 +142,10 @@ class EntityComparisonBase extends ControllerBase { // Fields which exist only on the right entity. foreach ($right_values as $field_name => $values) { $field_definition = $right_entity->getFieldDefinition($field_name); - $right_key = $right_entity->getEntityTypeId() . '.' .$field_definition->getName(); + $right_key = $right_entity->getEntityTypeId() . '.' . $field_definition->getName(); $compare_settings = $this->pluginsConfig->get('fields.' . $right_key); $result[$right_key] = [ - '#name' => ($compare_settings['settings']['show_header'] == 1) ? $field_definition->getLabel() : '', + '#name' => (isset($compare_settings['settings']['show_header']) && $compare_settings['settings']['show_header'] == 0) ? '' : $field_definition->getLabel(), '#settings' => $compare_settings, ]; $result[$right_key] += $this->combineFields(array(), $right_values[$field_name]); diff --git a/src/Form/FieldsSettingsForm.php b/src/Form/FieldsSettingsForm.php index fbea020..e832c48 100644 --- a/src/Form/FieldsSettingsForm.php +++ b/src/Form/FieldsSettingsForm.php @@ -2,7 +2,8 @@ namespace Drupal\diff\Form; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; @@ -22,16 +23,18 @@ class FieldsSettingsForm extends ConfigFormBase { protected $config; /** - * Wrapper object for writing/reading configuration from diff.plugins.yml + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $pluginConfig; + protected $entityTypeManager; /** - * The entity manager. + * The entity field manager. * - * @var \Drupal\Core\Entity\EntityManagerInterface + * @var \Drupal\Core\Entity\EntityFieldManager */ - protected $entityManager; + protected $entityFieldManager; /** * The field type plugin manager service. @@ -48,16 +51,19 @@ class FieldsSettingsForm extends ConfigFormBase { protected $diffBuilderManager; /** - * Constructs a FieldTypesListSettingsForm object. + * FieldsSettingsForm constructor. * * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager * @param \Drupal\Component\Plugin\PluginManagerInterface $diffBuilderManager + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */ - public function __construct(PluginManagerInterface $plugin_manager, PluginManagerInterface $diffBuilderManager, EntityManagerInterface $entityManager) { + public function __construct(PluginManagerInterface $plugin_manager, PluginManagerInterface $diffBuilderManager, EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager) { $this->config = $this->config('diff.plugins'); $this->fieldTypePluginManager = $plugin_manager; $this->diffBuilderManager = $diffBuilderManager; - $this->entityManager = $entityManager; + $this->entityTypeManager = $entityTypeManager; + $this->entityFieldManager = $entityFieldManager; } /** @@ -67,7 +73,8 @@ class FieldsSettingsForm extends ConfigFormBase { return new static( $container->get('plugin.manager.field.field_type'), $container->get('plugin.manager.diff.builder'), - $container->get('entity.manager') + $container->get('entity_type.manager'), + $container->get('entity_field.manager') ); } @@ -85,7 +92,6 @@ class FieldsSettingsForm extends ConfigFormBase { return [ 'diff.plugins', 'diff.settings', - 'diff.field_plugins' ]; } @@ -123,15 +129,15 @@ class FieldsSettingsForm extends ConfigFormBase { // Build a row in the table for every field of every entity type. // Get all the field plugins. - foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_name => $entity_type) { + foreach ($this->entityTypeManager->getDefinitions() as $entity_type_name => $entity_type) { // Exclude non-revisionable entities if (!$entity_type->isRevisionable()) { continue; } - $field_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_name); + $field_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_name); foreach ($field_definitions as $field_name => $field_definition) { - $show_diff = \Drupal::service('diff.diff_fields')->showDiff($field_definition); + $show_diff = $this->diffBuilderManager->showDiff($field_definition); if (!$show_diff) { continue; } @@ -178,9 +184,6 @@ class FieldsSettingsForm extends ConfigFormBase { 'wrapper' => 'field-display-overview-wrapper', 'effect' => 'fade', ], - '#field_name' => $field_name, - '#field_type' => $field_type, - '#entity_type' => $entity_type_label, '#field_key' => $field_key, ]; @@ -192,7 +195,7 @@ class FieldsSettingsForm extends ConfigFormBase { '#markup' => array_shift($labels), ]; - $field_type_label = \Drupal::service('plugin.manager.field.field_type')->getDefinitions()[$field_type]['label']; + $field_type_label = $this->fieldTypePluginManager->getDefinitions()[$field_type]['label']; $field_row['field_type'] = [ '#markup' => $field_type_label, ]; @@ -248,8 +251,6 @@ class FieldsSettingsForm extends ConfigFormBase { 'wrapper' => 'field-display-overview-wrapper', 'effect' => 'fade', ), - '#field_type' => $field_type, - '#field_name' => $field_name, '#field_key' => $field_key, ), 'settings_edit_form' => array(), @@ -271,22 +272,22 @@ class FieldsSettingsForm extends ConfigFormBase { 'settings' => $plugin->buildConfigurationForm(array(), $form_state), 'actions' => array( '#type' => 'actions', - 'save_settings' => $base_button + array( - '#type' => 'submit', - '#button_type' => 'primary', - '#name' => $field_key . '_plugin_settings_update', - '#value' => $this->t('Update'), - '#op' => 'update', - ), - 'cancel_settings' => $base_button + array( - '#type' => 'submit', - '#name' => $field_key . '_plugin_settings_cancel', - '#value' => $this->t('Cancel'), - '#op' => 'cancel', - // Do not check errors for the 'Cancel' button, but make sure we - // get the value of the 'plugin type' select. - '#limit_validation_errors' => [['fields', $field_key, 'plugin', 'type']], - ), + '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', + // Do not check errors for the 'Cancel' button, but make sure we + // get the value of the 'plugin type' select. + '#limit_validation_errors' => [['fields', $field_key, 'plugin', 'type']], + ], ), ); $field_row['settings_edit'] = array(); @@ -403,7 +404,6 @@ class FieldsSettingsForm extends ConfigFormBase { $state = new FormState(); $state->setValues($settings); $state->set('fields', $field_key); - //$state->set('field_type', $field_type); $plugin = $this->diffBuilderManager->createInstance($field_values['plugin']['type'], array()); // Send the values to the plugins form validate handler. $plugin->validateConfigurationForm($form, $state); @@ -427,19 +427,15 @@ class FieldsSettingsForm extends ConfigFormBase { $plugin_settings = $form_state->get('plugin_settings'); $fields = $form_values['fields']; - // Remove from configuration the keys of the field types which have no - // plugin selected. We need to clear this keys from configuration first - // and then save the settings for the fields which have a plugin selected. - // If we do both writing and clearing in the same for, the values won't get - // saved. + // Set the plugin type as hidden of the fields which have no plugin + // selected. foreach ($fields as $field_key => $field_values) { - // If there is no plugin selected remove the key from config file. if ($field_values['plugin']['type'] == 'hidden') { $this->config->set('fields.' . $field_key, ['type' => 'hidden']); } } $this->config->save(); - // For field types that have a plugin selected save the settings. + // For fields that have a plugin selected save the settings. foreach ($fields as $field_key => $field_values) { if ($field_values['plugin']['type'] != 'hidden') { // Get plugin settings. They lie either directly in submitted form diff --git a/src/Tests/DiffPluginTest.php b/src/Tests/DiffPluginTest.php index bba7a9e..e5fe384 100644 --- a/src/Tests/DiffPluginTest.php +++ b/src/Tests/DiffPluginTest.php @@ -369,8 +369,8 @@ class DiffPluginTest extends WebTestBase { // Enable the comparison of the link's title field. $config = \Drupal::configFactory()->getEditable('diff.plugins'); - $settings = $config->get('fields.node.field_link.settings'); $settings['compare_title'] = TRUE; + $config->set('fields.node.field_link.type', 'link_field_diff_builder'); $config->set('fields.node.field_link.settings', $settings); $config->save(); @@ -539,9 +539,9 @@ class DiffPluginTest extends WebTestBase { public function testTextWithSummaryPlugin() { // Enable the comparison of the summary. $config = \Drupal::configFactory()->getEditable('diff.plugins'); - $settings = $config->get('field_types.text_with_summary.settings'); $settings['compare_summary'] = TRUE; - $config->set('field_types.text_with_summary.settings', $settings); + $config->set('fields.node.body.type', 'text_summary_field_diff_builder'); + $config->set('fields.node.body.settings', $settings); $config->save(); // Create an article, setting the body field.