You are here

protected function DiffEntityComparison::summary in Diff 8

Creates an log message based on the changes of entity fields.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $revision: The current revision.

Return value

array Array of the revision fields with their value and label.

File

src/DiffEntityComparison.php, line 294

Class

DiffEntityComparison
Entity comparison service that prepares a diff of a pair of entities.

Namespace

Drupal\diff

Code

protected function summary(ContentEntityInterface $revision) {
  $result = [];
  $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.

  /** @var \Drupal\Core\Field\FieldItemListInterface $field_items */
  foreach ($revision as $field_items) {
    $show_delta = FALSE;

    // Create a plugin instance for the field definition.
    $plugin = $this->diffBuilderManager
      ->createInstanceForFieldDefinition($field_items
      ->getFieldDefinition());
    if ($plugin && $this->diffBuilderManager
      ->showDiff($field_items
      ->getFieldDefinition()
      ->getFieldStorageDefinition())) {

      // 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) {
            if ($field_items
              ->getFieldDefinition()
              ->getFieldStorageDefinition()
              ->getCardinality() != 1) {
              $show_delta = TRUE;
            }
            $result[$key] = $build;
            $delta = $show_delta ? '<sub>' . ($entity_key + 1) . '</sub> ' : ' - ';
            $result[$key]['label'] = $field_items
              ->getFieldDefinition()
              ->getLabel() . $delta . $result[$key]['label'];
          }
        }
      }
      else {

        // Create a unique flat key.
        $key = $revision
          ->id() . ':' . $entity_type_id . '.' . $field_items
          ->getName();
        $result[$key]['value'] = $field_items
          ->getValue();
        $result[$key]['label'] = $field_items
          ->getFieldDefinition()
          ->getLabel();
      }
    }
  }
  return $result;
}