Same filename and directory in other branches
- 8.9.x core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php
- 9 core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php
Namespace
Drupal\migrate\Plugin\migrate\destination
File
core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php
View source
<?php
namespace Drupal\migrate\Plugin\migrate\destination;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\language\ConfigurableLanguageManager;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityConfigBase extends Entity {
protected $languageManager;
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles);
$this->languageManager = $language_manager;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$entity_type_id = static::getEntityTypeId($plugin_id);
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity_type.manager')
->getStorage($entity_type_id), array_keys($container
->get('entity_type.bundle.info')
->getBundleInfo($entity_type_id)), $container
->get('language_manager'), $container
->get('config.factory'));
}
public function import(Row $row, array $old_destination_id_values = []) {
if ($row
->isStub()) {
throw new MigrateException('Config entities can not be stubbed.');
}
$this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
$ids = $this
->getIds();
$id_key = $this
->getKey('id');
if (count($ids) > 1) {
$id_keys = array_keys($ids);
if (!$row
->getDestinationProperty($id_key)) {
$row
->setDestinationProperty($id_key, $this
->generateId($row, $id_keys));
}
}
$entity = $this
->getEntity($row, $old_destination_id_values);
if (!$this
->isTranslationDestination()) {
$entity
->save();
}
if (count($ids) > 1) {
$return = [];
foreach ($id_keys as $id_key) {
if ($this
->isTranslationDestination() && $id_key == 'langcode') {
$return[] = $row
->getDestinationProperty($id_key);
}
else {
$return[] = $entity
->get($id_key);
}
}
return $return;
}
return [
$entity
->id(),
];
}
protected function isTranslationDestination() {
return !empty($this->configuration['translations']);
}
public function getIds() {
$id_key = $this
->getKey('id');
$ids[$id_key]['type'] = 'string';
if ($this
->isTranslationDestination()) {
$ids['langcode']['type'] = 'string';
}
return $ids;
}
protected function updateEntity(EntityInterface $entity, Row $row) {
$translation = FALSE;
if ($this
->isTranslationDestination() && $row
->hasDestinationProperty('langcode') && $this->languageManager instanceof ConfigurableLanguageManager) {
$config = $entity
->getConfigDependencyName();
$langcode = $this->configFactory
->get('langcode');
if ($langcode != $row
->getDestinationProperty('langcode')) {
$translation = TRUE;
}
}
if ($translation) {
if (!$row
->hasDestinationProperty('property')) {
throw new \LogicException('The "property" property is required');
}
if (!$row
->hasDestinationProperty('translation')) {
throw new \LogicException('The "translation" property is required');
}
$config_override = $this->languageManager
->getLanguageConfigOverride($row
->getDestinationProperty('langcode'), $config);
$config_override
->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $row
->getDestinationProperty('property')), $row
->getDestinationProperty('translation'));
$config_override
->save();
}
else {
foreach ($row
->getRawDestination() as $property => $value) {
$this
->updateEntityProperty($entity, explode(Row::PROPERTY_SEPARATOR, $property), $value);
}
$this
->setRollbackAction($row
->getIdMap());
}
return $entity;
}
protected function updateEntityProperty(EntityInterface $entity, array $parents, $value) {
$top_key = array_shift($parents);
$entity_value = $entity
->get($top_key);
if (is_array($entity_value)) {
NestedArray::setValue($entity_value, $parents, $value);
}
else {
$entity_value = $value;
}
$entity
->set($top_key, $entity_value);
}
protected function generateId(Row $row, array $ids) {
$id_values = [];
foreach ($ids as $id) {
if ($this
->isTranslationDestination() && $id == 'langcode') {
continue;
}
$id_values[] = $row
->getDestinationProperty($id);
}
return implode('.', $id_values);
}
public function rollback(array $destination_identifier) {
if ($this
->isTranslationDestination()) {
$id_values = [];
foreach ($destination_identifier as $key => $value) {
if ($this
->isTranslationDestination() && $key === 'langcode') {
continue;
}
$id_values[] = $value;
}
$entity_id = implode('.', $id_values);
$language = $destination_identifier['langcode'];
$config = $this->storage
->load($entity_id)
->getConfigDependencyName();
$config_override = $this->languageManager
->getLanguageConfigOverride($language, $config);
$config_override
->delete();
}
else {
$destination_identifier = implode('.', $destination_identifier);
parent::rollback([
$destination_identifier,
]);
}
}
}
Classes