You are here

public function StateInformation::getStatusInfo in Entity Share 8.3

Same name and namespace in other branches
  1. 8.2 modules/entity_share_client/src/Service/StateInformation.php \Drupal\entity_share_client\Service\StateInformation::getStatusInfo()

Check if an entity already exists or not and get status info.

Default implementation is to compare revision timestamp.

Parameters

array $data: The data of a single entity from the JSON:API payload.

Return value

array Returns an array of info:

  • label: the label to display.
  • class: to add a class on a row.
  • info_id: an identifier of the status info.
  • local_entity_link: the link of the local entity if it exists.
  • local_revision_id: the revision ID of the local entity if it exists.

Overrides StateInformationInterface::getStatusInfo

File

modules/entity_share_client/src/Service/StateInformation.php, line 68

Class

StateInformation
Service to handle presentation of import state.

Namespace

Drupal\entity_share_client\Service

Code

public function getStatusInfo(array $data) {
  $status_info = $this
    ->statusInfoArray(StateInformationInterface::INFO_ID_UNDEFINED);

  // Get the entity type and entity storage.
  $parsed_type = explode('--', $data['type']);
  $entity_type_id = $parsed_type[0];
  try {
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type_id);
  } catch (\Exception $exception) {
    $status_info = $this
      ->statusInfoArray(StateInformationInterface::INFO_ID_UNKNOWN);
    return $status_info;
  }

  // Check if an entity already exists.
  $existing_entities = $entity_storage
    ->loadByProperties([
    'uuid' => $data['id'],
  ]);
  if (empty($existing_entities)) {
    $status_info = $this
      ->statusInfoArray(StateInformationInterface::INFO_ID_NEW);
  }
  else {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $existing_entity */
    $existing_entity = array_shift($existing_entities);
    $resource_type = $this->resourceTypeRepository
      ->get($parsed_type[0], $parsed_type[1]);
    $changed_public_name = FALSE;
    if ($resource_type
      ->hasField('changed')) {
      $changed_public_name = $resource_type
        ->getPublicName('changed');
    }
    if (!empty($data['attributes'][$changed_public_name]) && method_exists($existing_entity, 'getChangedTime')) {
      $entity_changed_time = EntityShareUtility::convertChangedTime($data['attributes'][$changed_public_name]);
      $entity_keys = $entity_storage
        ->getEntityType()
        ->getKeys();

      // Case of translatable entity.
      if (isset($entity_keys['langcode']) && !empty($entity_keys['langcode'])) {
        $entity_language_id = $data['attributes'][$resource_type
          ->getPublicName($entity_keys['langcode'])];

        // Entity has the translation.
        if ($existing_entity
          ->hasTranslation($entity_language_id)) {
          $existing_translation = $existing_entity
            ->getTranslation($entity_language_id);

          // Existing entity.
          if ($this
            ->entityHasChanged($existing_translation, $entity_changed_time)) {
            $status_info = $this
              ->statusInfoArray(StateInformationInterface::INFO_ID_CHANGED, $existing_entity);
          }
          else {
            $status_info = $this
              ->statusInfoArray(StateInformationInterface::INFO_ID_SYNCHRONIZED, $existing_entity);
          }
        }
        else {
          $status_info = $this
            ->statusInfoArray(StateInformationInterface::INFO_ID_NEW_TRANSLATION, $existing_entity);
        }
      }
      else {

        // Existing entity.
        if ($this
          ->entityHasChanged($existing_entity, $entity_changed_time)) {
          $status_info = $this
            ->statusInfoArray(StateInformationInterface::INFO_ID_CHANGED, $existing_entity);
        }
        else {
          $status_info = $this
            ->statusInfoArray(StateInformationInterface::INFO_ID_SYNCHRONIZED, $existing_entity);
        }
      }
    }
  }
  return $status_info;
}