You are here

public function SkipImported::isEntityImportable in Entity Share 8.3

Method called on STAGE_IS_ENTITY_IMPORTABLE.

If the plugin reacts to this stage.

Parameters

\Drupal\entity_share_client\RuntimeImportContext $runtime_import_context: The import context.

array $entity_json_data: The entity JSON data.

Return value

bool TRUE if the entity is importable. FALSE otherwise.

Overrides ImportProcessorPluginBase::isEntityImportable

File

modules/entity_share_client/src/Plugin/EntityShareClient/Processor/SkipImported.php, line 53

Class

SkipImported
Skip already imported entities.

Namespace

Drupal\entity_share_client\Plugin\EntityShareClient\Processor

Code

public function isEntityImportable(RuntimeImportContext $runtime_import_context, array $entity_data) {
  $entity_uuid = $entity_data['id'];
  $parsed_type = explode('--', $entity_data['type']);
  $entity_type_id = $parsed_type[0];
  $resource_type = $this->resourceTypeRepository
    ->get($parsed_type[0], $parsed_type[1]);

  // Determine public name of 'langcode'.
  $langcode_public_name = FALSE;
  if ($resource_type
    ->hasField('langcode')) {
    $langcode_public_name = $resource_type
      ->getPublicName('langcode');
  }
  $langcode = $entity_data['attributes'][$langcode_public_name] ?? NULL;

  // Get the information of the last import of this entity.
  $import_status_entity = $this->stateInformation
    ->getImportStatusByParameters($entity_uuid, $entity_type_id, $langcode);

  // If there is no information on when this entity was last imported, it
  // means that probably it hasn't been - so it should be importable.
  if (!$import_status_entity) {
    return TRUE;
  }

  // Determine public name of 'changed' property.
  $changed_public_name = FALSE;
  if ($resource_type
    ->hasField('changed')) {
    $changed_public_name = $resource_type
      ->getPublicName('changed');
  }

  // Get the remote 'changed' value, if exists:
  $remote_changed_time = $entity_data['attributes'][$changed_public_name] ?? FALSE;

  // If there is no information on when the remote entity has been modified,
  // we shouldn't skip it - so it should be importable.
  if ($remote_changed_time === FALSE) {
    return TRUE;
  }
  $remote_changed_timestamp = EntityShareUtility::convertChangedTime($remote_changed_time);

  // If the remote entity was modified after our last import, it should
  // be importable.
  return $import_status_entity
    ->getLastImport() < $remote_changed_timestamp;
}