You are here

protected function DiffEntityComparison::combineFields in Diff 8

Combine two fields into an array with keys '#left' and '#right'.

Parameters

array $left_values: Entity field formatted into an array of strings.

array $right_values: Entity field formatted into an array of strings.

Return value

array Array resulted after combining the left and right values.

1 call to DiffEntityComparison::combineFields()
DiffEntityComparison::compareRevisions in src/DiffEntityComparison.php
This method should return an array of items ready to be compared.

File

src/DiffEntityComparison.php, line 156

Class

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

Namespace

Drupal\diff

Code

protected function combineFields(array $left_values, array $right_values) {
  $result = array(
    '#left' => array(),
    '#right' => array(),
  );
  $max = max(array(
    count($left_values),
    count($right_values),
  ));
  for ($delta = 0; $delta < $max; $delta++) {

    // EXPERIMENTAL: Transform thumbnail from ImageFieldBuilder.
    // @todo Make thumbnail / rich diff data pluggable.
    // @see https://www.drupal.org/node/2840566
    if (isset($left_values[$delta])) {
      $value = $left_values[$delta];
      if (isset($value['#thumbnail'])) {
        $result['#left_thumbnail'][] = $value['#thumbnail'];
      }
      else {
        $result['#left'][] = is_array($value) ? implode("\n", $value) : $value;
      }
    }
    if (isset($right_values[$delta])) {
      $value = $right_values[$delta];
      if (isset($value['#thumbnail'])) {
        $result['#right_thumbnail'][] = $value['#thumbnail'];
      }
      else {
        $result['#right'][] = is_array($value) ? implode("\n", $value) : $value;
      }
    }
  }

  // If a field has multiple values combine them into one single string.
  $result['#left'] = implode("\n", $result['#left']);
  $result['#right'] = implode("\n", $result['#right']);
  return $result;
}