You are here

protected function JsonapiHelper::getStatusInfo in Entity Share 8

Check if an entity already exists or not and compare revision timestamp.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The distant entity to check.

string $entity_type_id: The entity type id.

Return value

array Returns an array of info:

  • class: to add a class on a row.
  • label: the label to display.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\InvalidArgumentException

1 call to JsonapiHelper::getStatusInfo()
JsonapiHelper::addOption in modules/entity_share_client/src/Service/JsonapiHelper.php
Helper function to add an option.

File

modules/entity_share_client/src/Service/JsonapiHelper.php, line 688

Class

JsonapiHelper
Class JsonapiHelper.

Namespace

Drupal\entity_share_client\Service

Code

protected function getStatusInfo(ContentEntityInterface $entity, $entity_type_id) {
  $status_info = [
    'label' => $this
      ->t('Undefined'),
    'class' => 'entity-share-undefined',
  ];

  // Check if an entity already exists.
  $existing_entities = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->loadByProperties([
    'uuid' => $entity
      ->uuid(),
  ]);
  if (empty($existing_entities)) {
    $status_info = [
      'label' => $this
        ->t('New entity'),
      'class' => 'entity-share-new',
    ];
  }
  elseif (method_exists($entity, 'getChangedTime')) {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $existing_entity */
    $existing_entity = array_shift($existing_entities);
    $entity_language_id = $entity
      ->language()
      ->getId();

    // Entity has the translation.
    if ($existing_entity
      ->hasTranslation($entity_language_id)) {
      $existing_translation = $existing_entity
        ->getTranslation($entity_language_id);
      $entity_changed_time = $entity
        ->getChangedTime();
      $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',
        ];
      }
      else {
        $status_info = [
          'label' => $this
            ->t('Entities synchronized'),
          'class' => 'entity-share-up-to-date',
        ];
      }
    }
    else {
      $status_info = [
        'label' => $this
          ->t('New translation'),
        'class' => 'entity-share-new',
      ];
    }
  }
  return $status_info;
}