DefaultDataProcessor.php in Entity Share 8.3
File
modules/entity_share_client/src/Plugin/EntityShareClient/Processor/DefaultDataProcessor.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Plugin\EntityShareClient\Processor;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_share_client\ImportProcessor\ImportProcessorPluginBase;
use Drupal\entity_share_client\RuntimeImportContext;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DefaultDataProcessor extends ImportProcessorPluginBase {
protected $entityTypeManager;
protected $logger;
protected $languageManager;
protected $stateInformation;
protected $time;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->entityTypeManager = $container
->get('entity_type.manager');
$instance->logger = $container
->get('logger.channel.entity_share_client');
$instance->languageManager = $container
->get('language_manager');
$instance->stateInformation = $container
->get('entity_share_client.state_information');
$instance->time = $container
->get('datetime.time');
return $instance;
}
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];
$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];
}
if (is_null($this->languageManager
->getLanguage($data_langcode))) {
$entity_label = $entity_type_id;
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;
}
public function prepareImportableEntityData(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];
$entity_storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entity_keys = $entity_storage
->getEntityType()
->getKeys();
$entity_keys_to_remove = [
'id',
'revision',
'default_langcode',
];
foreach ($entity_keys_to_remove as $entity_key_to_remove) {
if (!isset($entity_keys[$entity_key_to_remove])) {
continue;
}
if (!isset($field_mappings[$entity_type_id][$entity_bundle][$entity_keys[$entity_key_to_remove]])) {
continue;
}
$public_name_to_remove = $field_mappings[$entity_type_id][$entity_bundle][$entity_keys[$entity_key_to_remove]];
if (isset($entity_json_data['attributes'][$public_name_to_remove])) {
unset($entity_json_data['attributes'][$public_name_to_remove]);
}
}
$uuid_public_name = 'uuid';
if (!empty($entity_keys['uuid']) && isset($field_mappings[$entity_type_id][$entity_bundle][$entity_keys['uuid']])) {
$uuid_public_name = $field_mappings[$entity_type_id][$entity_bundle][$entity_keys['uuid']];
}
$entity_json_data['attributes'][$uuid_public_name] = $entity_json_data['id'];
}
public function postEntitySave(RuntimeImportContext $runtime_import_context, ContentEntityInterface $processed_entity) {
$import_status_entity = $this->stateInformation
->getImportStatusOfEntity($processed_entity);
if (!$import_status_entity) {
$parameters = [
'remote_website' => $runtime_import_context
->getRemote()
->id(),
'channel_id' => $runtime_import_context
->getChannelId(),
];
$this->stateInformation
->createImportStatusOfEntity($processed_entity, $parameters);
}
else {
$import_status_entity
->setLastImport($this->time
->getRequestTime())
->save();
}
}
}