You are here

public function StateInformation::getStatusInfo in Entity Share 8.2

Same name and namespace in other branches
  1. 8.3 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 52

Class

StateInformation
Class StateInformation.

Namespace

Drupal\entity_share_client\Service

Code

public function getStatusInfo(array $data) {
  $status_info = [
    'label' => $this
      ->t('Undefined'),
    'class' => 'entity-share-undefined',
    'info_id' => StateInformationInterface::INFO_ID_UNDEFINED,
    'local_entity_link' => NULL,
    'local_revision_id' => NULL,
  ];

  // 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 = [
      'label' => $this
        ->t('Unknown entity type'),
      'class' => 'entity-share-undefined',
      'info_id' => StateInformationInterface::INFO_ID_UNKNOWN,
      'local_entity_link' => NULL,
      'local_revision_id' => NULL,
    ];
    return $status_info;
  }

  // Check if an entity already exists.
  $existing_entities = $entity_storage
    ->loadByProperties([
    'uuid' => $data['id'],
  ]);
  if (empty($existing_entities)) {
    $status_info = [
      'label' => $this
        ->t('New entity'),
      'class' => 'entity-share-new',
      'info_id' => StateInformationInterface::INFO_ID_NEW,
      'local_entity_link' => NULL,
      'local_revision_id' => NULL,
    ];
  }
  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 = 0;

      // If the website is using backward compatible timestamps output.
      // @see https://www.drupal.org/node/2859657.
      // The value is casted in integer for
      // https://www.drupal.org/node/2837696.
      if (is_numeric($data['attributes'][$changed_public_name])) {
        $entity_changed_time = (int) $data['attributes'][$changed_public_name];
      }
      elseif ($changed_datetime = \DateTime::createFromFormat(\DateTime::RFC3339, $data['attributes'][$changed_public_name])) {
        $entity_changed_time = $changed_datetime
          ->getTimestamp();
      }
      $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_changed_time = $existing_translation
            ->getChangedTime();

          // Existing entity.
          if ($entity_changed_time != $existing_entity_changed_time) {
            $status_info = [
              'label' => $this
                ->t('Entities not synchronized'),
              'class' => 'entity-share-changed',
              'info_id' => StateInformationInterface::INFO_ID_CHANGED,
              'local_entity_link' => $existing_entity
                ->toUrl(),
              'local_revision_id' => $existing_entity
                ->getRevisionId(),
            ];
          }
          else {
            $status_info = [
              'label' => $this
                ->t('Entities synchronized'),
              'class' => 'entity-share-up-to-date',
              'info_id' => StateInformationInterface::INFO_ID_SYNCHRONIZED,
              'local_entity_link' => $existing_entity
                ->toUrl(),
              'local_revision_id' => $existing_entity
                ->getRevisionId(),
            ];
          }
        }
        else {
          $status_info = [
            'label' => $this
              ->t('New translation'),
            'class' => 'entity-share-new',
            'info_id' => StateInformationInterface::INFO_ID_NEW_TRANSLATION,
            'local_entity_link' => $existing_entity
              ->toUrl(),
            'local_revision_id' => $existing_entity
              ->getRevisionId(),
          ];
        }
      }
      else {
        $existing_entity_changed_time = $existing_entity
          ->getChangedTime();

        // Existing entity.
        if ($entity_changed_time != $existing_entity_changed_time) {
          $status_info = [
            'label' => $this
              ->t('Entities not synchronized'),
            'class' => 'entity-share-changed',
            'info_id' => StateInformationInterface::INFO_ID_CHANGED,
            'local_entity_link' => $existing_entity
              ->toUrl(),
            'local_revision_id' => $existing_entity
              ->getRevisionId(),
          ];
        }
        else {
          $status_info = [
            'label' => $this
              ->t('Entities synchronized'),
            'class' => 'entity-share-up-to-date',
            'info_id' => StateInformationInterface::INFO_ID_SYNCHRONIZED,
            'local_entity_link' => $existing_entity
              ->toUrl(),
            'local_revision_id' => $existing_entity
              ->getRevisionId(),
          ];
        }
      }
    }
  }
  return $status_info;
}