You are here

protected function EntityParser::parseEntity in Entity Share 8.3

Parses an entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The Drupal entity (local or remote).

array $remote_data: Used for remote entity: entity data coming from JSON:API.

Return value

array Parsed data of a field, suitable for YAML parsing. Associative array, keyed by labels. Values are strings, numbers or arrays.

2 calls to EntityParser::parseEntity()
EntityParser::prepareLocalEntity in modules/entity_share_diff/src/Service/EntityParser.php
Prepares entity loaded from local database.
EntityParser::prepareRemoteEntity in modules/entity_share_diff/src/Service/EntityParser.php
Prepares entity loaded from remote by JSON:API.

File

modules/entity_share_diff/src/Service/EntityParser.php, line 222

Class

EntityParser
Entity parser.

Namespace

Drupal\entity_share_diff\Service

Code

protected function parseEntity(ContentEntityInterface $entity, array $remote_data = NULL) {
  $result = [];
  $langcode = $this->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);
  }
  $irrelevant_fields = $this
    ->getFieldsIrrelevantForDiff($entity);

  // 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 $item_key => $field_items) {

    // Prepare remote information for reference fields, if exists.
    if ($this
      ->getRemote()) {
      $public_key = $this
        ->getPublicFieldName($item_key, $remote_data);
    }
    else {
      $public_key = $item_key;
    }
    $remote_field_data = [];

    // Determine if a field should be parsed or skipped.
    // If a field is an entity reference, then eliminate it in
    // case the relationship is not handleable.
    switch ($this->entityReferenceHelper
      ->relationshipHandleable($field_items)) {
      case EntityReferenceHelper::RELATIONSHIP_HANDLEABLE:
        $should_parse = TRUE;
        if (isset($remote_data['relationships'][$public_key])) {
          $remote_field_data = $remote_data['relationships'][$public_key];
          if (isset($remote_field_data['data'])) {
            $remote_field_data['data'] = EntityShareUtility::prepareData($remote_field_data['data']);
          }
        }
        break;
      case EntityReferenceHelper::RELATIONSHIP_NOT_HANDLEABLE:
        $should_parse = FALSE;
        break;
      case EntityReferenceHelper::RELATIONSHIP_NOT_ENTITY_REFERENCE:
        $should_parse = !in_array($item_key, $irrelevant_fields);
        break;
    }
    if (!$should_parse) {
      continue;
    }
    $parsed_field = $this
      ->parseField($item_key, $field_items, $remote_field_data);
    if ($parsed_field != NULL) {
      $field_label = (string) $field_items
        ->getFieldDefinition()
        ->getLabel();
      $result[$field_label] = $parsed_field;
    }
  }
  return $result;
}