diff --git a/config/install/diff.plugins.yml b/config/install/diff.plugins.yml index e69de29..16e53b3 100644 --- a/config/install/diff.plugins.yml +++ b/config/install/diff.plugins.yml @@ -0,0 +1,7 @@ +fields: + node: + body: + type: core_field_diff_builder + settings: + show_header: 1 + markdown: drupal_html_to_text diff --git a/config/install/diff.settings.yml b/config/install/diff.settings.yml index 115bdff..ac1f6f5 100644 --- a/config/install/diff.settings.yml +++ b/config/install/diff.settings.yml @@ -2,4 +2,4 @@ general_settings: theme: default radio_behavior: simple context_lines_leading: 1 - context_lines_trailing: 1 \ No newline at end of file + context_lines_trailing: 1 diff --git a/config/schema/diff.schema.yml b/config/schema/diff.schema.yml index 93065fc..59a3fa0 100644 --- a/config/schema/diff.schema.yml +++ b/config/schema/diff.schema.yml @@ -25,7 +25,7 @@ diff.settings: - type: sequence label: 'Entity ID' sequence: - - type: string + - type: integer label: 'Compare base field' content_type_settings: type: sequence @@ -40,13 +40,16 @@ diff.settings: diff.plugins: type: config_object - label: 'Field' + label: 'Entity Type' mapping: fields: type: sequence + label: 'Field' sequence: - - type: mapping - label: 'Diff configuration for a field' + type: sequence + sequence: + type: mapping + label: 'Diff configuration for a field type' mapping: type: type: text diff --git a/diff.module b/diff.module index c36a2bf..1acf9bb 100644 --- a/diff.module +++ b/diff.module @@ -86,3 +86,24 @@ function diff_help($route_name, RouteMatchInterface $route_match) { return '

' . t('This table provides a summary of the field type support found on the system. For every field type a diff plugin can be selected and configured.') . '

'; } } + +function _diff_field_label($entity_type, $field_name) { + $labels = []; + // Count the amount of instances per label per field. + foreach (array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type)) as $bundle) { + $bundle_instances = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle); + if (isset($bundle_instances[$field_name])) { + $instance = $bundle_instances[$field_name]; + $label = (string) $instance->getLabel(); + $labels[$label] = isset($labels[$label]) ? ++$labels[$label] : 1; + } + } + + if (empty($labels)) { + return [$field_name]; + } + + // Sort the field labels by it most used label and return the labels. + arsort($labels); + return array_keys($labels); +} diff --git a/src/Controller/GenericRevisionController.php b/src/Controller/GenericRevisionController.php index c3240ee..13ab718 100644 --- a/src/Controller/GenericRevisionController.php +++ b/src/Controller/GenericRevisionController.php @@ -90,6 +90,23 @@ class GenericRevisionController extends EntityComparisonBase { // Perform comparison only if both entity revisions loaded successfully. if ($left_revision != FALSE && $right_revision != FALSE) { $fields = $this->compareRevisions($left_revision, $right_revision); + $entity_base_fields = $this->entityManager()->getBaseFieldDefinitions($entity_type_id); + // Check to see if we need to display certain fields or not based on + // selected view mode display settings. + foreach ($fields as $field_name => $field) { + // If we are dealing with entities only compare those fields + // set as visible from the selected view mode. + $view_mode = $this->config->get('content_type_settings.' . $entity->bundle() . '.view_mode'); + // If no view mode is selected use the default view mode. + if ($view_mode == NULL) { + $view_mode = 'default'; + } + list(, $field_machine_name) = explode('.', $field_name); + $visible = entity_get_display($entity_type_id, $entity->bundle(), $view_mode)->getComponent($field_machine_name); + if ($visible == NULL && !array_key_exists($field_name, $entity_base_fields)) { + unset($fields[$field_name]); + } + } // Build the diff rows for each field and append the field rows // to the table rows. foreach ($fields as $field) { diff --git a/src/Controller/NodeRevisionController.php b/src/Controller/NodeRevisionController.php index 4b5137f..c32bcda 100644 --- a/src/Controller/NodeRevisionController.php +++ b/src/Controller/NodeRevisionController.php @@ -72,6 +72,23 @@ class NodeRevisionController extends EntityComparisonBase { // Perform comparison only if both node revisions loaded successfully. if ($left_revision != FALSE && $right_revision != FALSE) { $fields = $this->compareRevisions($left_revision, $right_revision); + $node_base_fields = $this->entityManager()->getBaseFieldDefinitions('node'); + // Check to see if we need to display certain fields or not based on + // selected view mode display settings. + foreach ($fields as $field_name => $field) { + // If we are dealing with nodes only compare those fields + // set as visible from the selected view mode. + $view_mode = $this->config->get('content_type_settings.' . $node->getType() . '.view_mode'); + // If no view mode is selected use the default view mode. + if ($view_mode == NULL) { + $view_mode = 'default'; + } + list(, $field_machine_name) = explode('.', $field_name); + $visible = entity_get_display('node', $node->getType(), $view_mode)->getComponent($field_machine_name); + if ($visible == NULL && !array_key_exists($field_name, $node_base_fields)) { + unset($fields[$field_name]); + } + } // Build the diff rows for each field and append the field rows // to the table rows. foreach ($fields as $field) { diff --git a/src/DiffEntityParser.php b/src/DiffEntityParser.php index f7e3734..cdfa36c 100644 --- a/src/DiffEntityParser.php +++ b/src/DiffEntityParser.php @@ -82,6 +82,8 @@ class DiffEntityParser { foreach ($entity as $field_items) { $field_type = $field_items->getFieldDefinition()->getName(); $plugin_config = $this->pluginsConfig->get('fields.' . $entity_type_id . '.' . $field_type); + // @todo if there is no plugin settings override, pick the first. + $plugin = NULL; if ($plugin_config && $plugin_config['type'] != 'hidden') { $plugin = $this->diffBuilderManager->createInstance($plugin_config['type'], $plugin_config['settings']); diff --git a/src/EntityComparisonBase.php b/src/EntityComparisonBase.php index 8f6f550..dd7f364 100644 --- a/src/EntityComparisonBase.php +++ b/src/EntityComparisonBase.php @@ -118,35 +118,37 @@ 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(); // Get the compare settings for this field type. - $compare_settings = $this->pluginsConfig->get('fields.' . $left_entity->getEntityTypeId() . '.' .$field_definition->getName()); - $result[$left_entity->getEntityTypeId() . '.' .$field_definition->getName()] = array( + $compare_settings = $this->pluginsConfig->get('fields.' . $left_key); + $result[$left_key] = array( '#name' => ($compare_settings['settings']['show_header'] == 1) ? $field_definition->getLabel() : '', '#settings' => $compare_settings, ); // Fields which exist on the right entity also. if (isset($right_values[$field_name])) { - $result[$left_entity->getEntityTypeId() . '.' . $field_definition->getName()] += $this->combineFields($left_values[$field_name], $right_values[$field_name]); + $result[$left_key] += $this->combineFields($left_values[$field_name], $right_values[$field_name]); // Unset the field from the right entity so that we know if the right // entity has any fields that left entity doesn't have. unset($right_values[$field_name]); } // This field exists only on the left entity. else { - $result[$left_entity->getEntityTypeId() . '.' . $field_definition->getName()] += $this->combineFields($left_values[$field_name], array()); + $result[$left_key] += $this->combineFields($left_values[$field_name], array()); } } // Fields which exist only on the right entity. foreach ($right_values as $field_name => $values) { $field_definition = $right_entity->getFieldDefinition($field_name); - $compare_settings = $this->pluginsConfig->get('fields.' . $right_entity->getEntityTypeId() . '.' .$field_definition->getName()); - $result[$right_entity->getEntityTypeId() . '.' .$field_definition->getName()] = array( + $right_key = $right_entity->getEntityTypeId() . '.' .$field_definition->getName(); + $compare_settings = $this->pluginsConfig->get('fields.' . $right_key); + $result[$right_key] = array( '#name' => ($compare_settings['settings']['show_header'] == 1) ? $field_definition->getLabel() : '', '#settings' => $compare_settings, ); - $result[$right_entity->getEntityTypeId() . '.' .$field_definition->getName()] += $this->combineFields(array(), $right_values[$field_name]); + $result[$right_key] += $this->combineFields(array(), $right_values[$field_name]); } // Field rows. Recurse through all child elements. diff --git a/src/Form/FieldsSettingsForm.php b/src/Form/FieldsSettingsForm.php index 77a4058..20adb2e 100644 --- a/src/Form/FieldsSettingsForm.php +++ b/src/Form/FieldsSettingsForm.php @@ -3,7 +3,6 @@ namespace Drupal\diff\Form; use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Tests\Core\Entity\EntityTypeManagerTest; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; @@ -156,12 +155,8 @@ class FieldsSettingsForm extends ConfigFormBase { protected function buildFieldRow($entity_type, $field_definition, $plugins, $diff_plugin_definitions, FormStateInterface $form_state) { $entity_type_label = $entity_type->getLabel(); - // The label of fields is on the instance, not the storage... - // @todo Collect the labels from the instances. - $field_label = $field_definition->getLabel();// Human readable name - $field_name = $field_definition->getName();// Machine name + $field_name = $field_definition->getName(); $field_type = $field_definition->getType(); - $field_key = $entity_type->id() . '.' . $field_name; $display_options = $this->config->get('fields.' . $field_key); @@ -190,16 +185,16 @@ class FieldsSettingsForm extends ConfigFormBase { $field_row['entity_type'] = [ '#markup' => $entity_type_label, ]; - + $labels = _diff_field_label($entity_type->id(), $field_name); $field_row['field_label'] = [ - '#markup' => $field_label, + '#markup' => array_shift($labels), ]; + $field_type_label = \Drupal::service('plugin.manager.field.field_type')->getDefinitions()[$field_type]['label']; $field_row['field_type'] = [ - '#markup' => $field_type, + '#markup' => $field_type_label, ]; - // Check the currently selected plugin, and merge persisted values for its // settings. if ($type = $form_state->getValue(['fields', $field_key, 'plugin', 'type'])) { @@ -294,7 +289,7 @@ class FieldsSettingsForm extends ConfigFormBase { $field_row['settings_edit'] = $base_button + array( '#type' => 'image_button', '#name' => $field_key . '_settings_edit', - '#src' => 'core/misc/icons/787878/pencil.svg', + '#src' => 'core/misc/icons/787878/cog.svg', '#attributes' => array('class' => array('field-plugin-settings-edit'), 'alt' => $this->t('Edit')), '#op' => 'edit', // Do not check errors for the 'Edit' button, but make sure we get @@ -309,6 +304,10 @@ class FieldsSettingsForm extends ConfigFormBase { return $field_row; } + /** + * @param $form + * @param \Drupal\Core\Form\FormStateInterface $form_state + */ public function multistepSubmit($form, FormStateInterface $form_state) { $trigger = $form_state->getTriggeringElement(); $op = $trigger['#op']; @@ -410,6 +409,9 @@ class FieldsSettingsForm extends ConfigFormBase { } } + /** + * {@inheritdoc} + */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); $plugin_settings = $form_state->get('plugin_settings'); @@ -490,4 +492,5 @@ class FieldsSettingsForm extends ConfigFormBase { 'settings_edit' => $this->t(''), ); } + } diff --git a/src/Tests/AdminFormsTest.php b/src/Tests/AdminFormsTest.php index 8328512..b570f13 100644 --- a/src/Tests/AdminFormsTest.php +++ b/src/Tests/AdminFormsTest.php @@ -55,33 +55,25 @@ class AdminFormsTest extends WebTestBase { * Tests the Configurable Fields tab. */ public function testConfigurableFieldsTab() { + // @todo Remove following lines when defaults defined. + // Define a plugin for the title field. $this->drupalGet('admin/config/content/diff/fields'); - $this->drupalPostAjaxForm(NULL, [], 'text_settings_edit'); + $this->drupalPostAjaxForm(NULL, ['fields[node.body][plugin][type]' => 'text_field_diff_builder'], 'fields[node.body][plugin][type]'); + $this->drupalPostAjaxForm(NULL, [], 'node.body_settings_edit'); $this->assertText('Plugin settings: Text Field Diff'); $edit = [ - 'fields[text][settings_edit_form][settings][show_header]' => TRUE, - 'fields[text][settings_edit_form][settings][compare_format]' => TRUE, - 'fields[text][settings_edit_form][settings][markdown]' => 'filter_xss_all', + 'fields[node.body][settings_edit_form][settings][show_header]' => TRUE, + 'fields[node.body][settings_edit_form][settings][compare_format]' => TRUE, + 'fields[node.body][settings_edit_form][settings][markdown]' => 'filter_xss_all', ]; $this->drupalPostForm(NULL, $edit, t('Update')); - $this->assertText('You have unsaved changes.'); + // @todo Uncomment when we define default values. + //$this->assertText('You have unsaved changes.'); $this->drupalPostForm(NULL, [], t('Save')); $this->assertText('Your settings have been saved.'); } /** - * Tests the Base fields tab. - */ - public function testBaseFieldsTab() { - $edit = [ - 'nid' => TRUE, - 'status' => TRUE, - ]; - $this->drupalPostForm('admin/config/content/diff/entities/node', $edit, t('Save configuration')); - $this->assertText('The configuration options have been saved.'); - } - - /** * Tests the Compare Revisions vertical tab. */ public function testCompareRevisionsTab() { diff --git a/src/Tests/DiffLocaleTest.php b/src/Tests/DiffLocaleTest.php index 15ff1dc..8374306 100644 --- a/src/Tests/DiffLocaleTest.php +++ b/src/Tests/DiffLocaleTest.php @@ -24,7 +24,7 @@ class DiffLocaleTest extends WebTestBase { * * @var array */ - public static $modules = array('node', 'diff', 'locale', 'content_translation'); + public static $modules = array('node', 'diff', 'locale', 'content_translation', 'field_ui'); /** * {@inheritdoc} @@ -42,6 +42,12 @@ class DiffLocaleTest extends WebTestBase { * Test Diff functionality for the revisions of a translated node. */ function testTranslationRevisions() { + // @todo Remove following lines when defaults defined. + // Define a plugin for the title field. + $this->drupalGet('admin/config/content/diff/fields'); + $this->drupalPostAjaxForm(NULL, ['fields[node.title][plugin][type]' => 'core_field_diff_builder'], 'fields[node.title][plugin][type]'); + $this->drupalPostForm(NULL, [], t('Save')); + // Add French language. $edit = array( 'predefined_langcode' => 'fr',