You are here

public function DiffEntityComparison::compareRevisions in Diff 8

This method should return an array of items ready to be compared.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $left_entity: The left entity.

\Drupal\Core\Entity\ContentEntityInterface $right_entity: The right entity.

Return value

array Items ready to be compared by the Diff component.

File

src/DiffEntityComparison.php, line 101

Class

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

Namespace

Drupal\diff

Code

public function compareRevisions(ContentEntityInterface $left_entity, ContentEntityInterface $right_entity) {
  $result = array();
  $left_values = $this->entityParser
    ->parseEntity($left_entity);
  $right_values = $this->entityParser
    ->parseEntity($right_entity);
  foreach ($left_values as $left_key => $values) {
    list(, $field_key) = explode(':', $left_key);

    // Get the compare settings for this field type.
    $compare_settings = $this->pluginsConfig
      ->get('fields.' . $field_key);
    $result[$left_key] = [
      '#name' => isset($compare_settings['settings']['show_header']) && $compare_settings['settings']['show_header'] == 0 ? '' : $values['label'],
      '#settings' => $compare_settings,
      '#data' => [],
    ];

    // Fields which exist on the right entity also.
    if (isset($right_values[$left_key])) {
      $result[$left_key]['#data'] += $this
        ->combineFields($left_values[$left_key], $right_values[$left_key]);

      // 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[$left_key]);
    }
    else {
      $result[$left_key]['#data'] += $this
        ->combineFields($left_values[$left_key], []);
    }
  }

  // Fields which exist only on the right entity.
  foreach ($right_values as $right_key => $values) {
    list(, $field_key) = explode(':', $right_key);
    $compare_settings = $this->pluginsConfig
      ->get('fields.' . $field_key);
    $result[$right_key] = [
      '#name' => isset($compare_settings['settings']['show_header']) && $compare_settings['settings']['show_header'] == 0 ? '' : $values['label'],
      '#settings' => $compare_settings,
      '#data' => [],
    ];
    $result[$right_key]['#data'] += $this
      ->combineFields([], $right_values[$right_key]);
  }
  return $result;
}