You are here

public function DiffEntityParser::parseEntity in Diff 8

Transforms an entity into an array of strings.

Parses an entity's fields and for every field it builds an array of string to be compared. Basically this function transforms an entity into an array of strings.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: An entity containing fields.

Return value

array Array of strings resulted by parsing the entity.

File

src/DiffEntityParser.php, line 62

Class

DiffEntityParser
Transforms an entity into an array of strings for diff.

Namespace

Drupal\diff

Code

public function parseEntity(ContentEntityInterface $entity) {
  $result = array();
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();

  // Load entity of current language, otherwise fields are always compared by
  // their default language.
  if ($entity
    ->hasTranslation($langcode)) {
    $entity = $entity
      ->getTranslation($langcode);
  }
  $entity_type_id = $entity
    ->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 ($entity as $field_items) {

    // Define if the current field should be displayed as a diff change.
    $show_diff = $this->diffBuilderManager
      ->showDiff($field_items
      ->getFieldDefinition()
      ->getFieldStorageDefinition());
    if (!$show_diff || !$entity
      ->get($field_items
      ->getFieldDefinition()
      ->getName())
      ->access('view')) {
      continue;
    }

    // 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
            ->parseEntity($reference_entity) as $key => $build) {
            $result[$key] = $build;
            $result[$key]['label'] = $field_items
              ->getFieldDefinition()
              ->getLabel() . ' > ' . $result[$key]['label'];
          }
        }
      }
      else {
        $build = $plugin
          ->build($field_items);
        if (!empty($build)) {
          $result[$entity
            ->id() . ':' . $entity_type_id . '.' . $field_items
            ->getName()] = $build;
          $result[$entity
            ->id() . ':' . $entity_type_id . '.' . $field_items
            ->getName()]['label'] = $field_items
            ->getFieldDefinition()
            ->getLabel();
        }
      }
    }
  }
  $this->diffBuilderManager
    ->clearCachedDefinitions();
  return $result;
}