You are here

public function DefaultDataProcessor::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/DefaultDataProcessor.php, line 81

Class

DefaultDataProcessor
General default data processor.

Namespace

Drupal\entity_share_client\Plugin\EntityShareClient\Processor

Code

public function isEntityImportable(RuntimeImportContext $runtime_import_context, array $entity_json_data) {
  $field_mappings = $runtime_import_context
    ->getFieldMappings();
  $parsed_type = explode('--', $entity_json_data['type']);
  $entity_type_id = $parsed_type[0];
  $entity_bundle = $parsed_type[1];

  // @todo Refactor in attributes to avoid getting entity keys each time.
  $entity_storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  $entity_keys = $entity_storage
    ->getEntityType()
    ->getKeys();
  $langcode_public_name = FALSE;
  if (!empty($entity_keys['langcode']) && isset($field_mappings[$entity_type_id][$entity_bundle][$entity_keys['langcode']])) {
    $langcode_public_name = $field_mappings[$entity_type_id][$entity_bundle][$entity_keys['langcode']];
  }
  $data_langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
  if ($langcode_public_name && !empty($entity_json_data['attributes'][$langcode_public_name])) {
    $data_langcode = $entity_json_data['attributes'][$langcode_public_name];
  }

  // Check if we try to import an entity with langcode in a disabled language.
  if (is_null($this->languageManager
    ->getLanguage($data_langcode))) {

    // Use the entity type if there is no label.
    $entity_label = $entity_type_id;

    // Prepare entity label.
    if (isset($entity_keys['label']) && isset($field_mappings[$entity_type_id][$entity_bundle][$entity_keys['label']])) {
      $label_public_name = $field_mappings[$entity_type_id][$entity_bundle][$entity_keys['label']];
      if (!empty($entity_json_data['attributes'][$label_public_name])) {
        $entity_label = $entity_json_data['attributes'][$label_public_name];
      }
    }
    $log_variables = [
      '%entity_label' => $entity_label,
    ];
    $this->logger
      ->error('Trying to import an entity (%entity_label) in a disabled language.', $log_variables);
    $this
      ->messenger()
      ->addError($this
      ->t('Trying to import an entity (%entity_label) in a disabled language.', $log_variables));
    return FALSE;
  }
  return TRUE;
}