diff --git a/src/DiffEntityComparison.php b/src/DiffEntityComparison.php index 665f170..6f1b079 100644 --- a/src/DiffEntityComparison.php +++ b/src/DiffEntityComparison.php @@ -268,15 +268,34 @@ class DiffEntityComparison { * The revision log message. */ public function getRevisionDescription(ContentEntityInterface $revision, ContentEntityInterface $previous_revision = NULL) { + $auto_generated = FALSE; + $revision_summary = []; if ($revision instanceof RevisionLogInterface) { $revision_summary = Xss::filter($revision->getRevisionLogMessage()); - if ($revision_summary == '') { - $revision_summary = $this->summary($revision, $previous_revision); + if ($revision_summary == '' && $previous_revision) { + $auto_generated = TRUE; + $revision_summary = []; } } - else { - $revision_summary = $this->summary($revision, $previous_revision); + elseif ($previous_revision) { + $auto_generated = TRUE; } + if ($auto_generated) { + $left_values = $this->summary($previous_revision); + $right_values = $this->summary($revision); + foreach ($right_values as $key => $value) { + if (isset($left_values[$key]) && $value != $left_values[$key]) { + $revision_summary[] = $value['label']; + } + } + if (count($revision_summary) > 0) { + $revision_summary = 'Changes on: ' . implode(', ', $revision_summary); + } + else { + $revision_summary = 'No changes.'; + } + } + return $revision_summary; } @@ -285,31 +304,36 @@ class DiffEntityComparison { * * @param \Drupal\Core\Entity\ContentEntityInterface $revision * The current revision. - * @param \Drupal\Core\Entity\ContentEntityInterface $previous_revision - * (optional) The previous revision. Defaults to NULL. * * @return string * The revision log message. */ - protected function summary(ContentEntityInterface $revision, ContentEntityInterface $previous_revision = NULL) { - $summary = []; - if ($previous_revision) { - foreach ($previous_revision as $key => $value) { - if ($previous_revision->get($key) - ->getValue() != $revision->get($key)->getValue() - && $this->diffBuilderManager->getSelectedPluginForFieldDefinition($value->getFieldDefinition()) - ) { - $summary[] = $value->getFieldDefinition()->getLabel(); - } - } - } - if (count($summary) > 0) { - $summary = 'Changes on: ' . implode(', ', $summary); - } - else { - $summary = 'No changes.'; - } - return $summary; - } + protected function summary(ContentEntityInterface $revision) { + $result = array(); + $entity_type_id = $revision->getEntityTypeId(); + // Loop through entity fields and transform every FieldItemList object + // into an array of strings according to field type specific settings. + foreach ($revision as $field_items) { + // Create a plugin instance for the field definition. + $plugin = $this->diffBuilderManager->createInstanceForFieldDefinition($field_items->getFieldDefinition()); + if ($plugin) { + // Create the array with the fields of the entity. Recursive if the + // field contains entities. + if ($plugin instanceof FieldReferenceInterface) { + foreach ($plugin->getEntitiesToDiff($field_items) as $entity_key => $reference_entity) { + foreach($this->summary($reference_entity) as $key => $build) { + $result[$key] = $build; + $result[$key]['label'] = $field_items->getFieldDefinition()->getLabel() . ' > ' . $result[$key]['label']; + }; + } + } + else { + $result[$revision->id() . ':' . $entity_type_id . '.' . $field_items->getName()] = $field_items->getValue(); + $result[$revision->id() . ':' . $entity_type_id . '.' . $field_items->getName()]['label'] = $field_items->getFieldDefinition()->getLabel(); + } + } + } + return $result; + } }