class EntityConfigBase in Drupal 8
Same name and namespace in other branches
- 9 core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php \Drupal\migrate\Plugin\migrate\destination\EntityConfigBase
Base destination class for importing configuration entities.
Available configuration keys:
- translations: (optional) Boolean, if TRUE, the destination will be associated with the langcode provided by the source plugin. Defaults to FALSE.
Examples:
source:
plugin: d7_block_custom
process:
id: bid
info: info
langcode: language
body: body
destination:
plugin: entity:block
This will save the migrated, processed row as a block config entity.
source:
plugin: d6_i18n_profile_field
constants:
entity_type: user
bundle: user
process:
langcode: language
entity_type: 'constants/entity_type'
bundle: 'constants/bundle'
field_name: name
...
translation: translation
destination:
plugin: entity:field_config
translations: true
Because the translations configuration is set to "true", this will save the migrated, processed row to a "field_config" entity associated with the designated langcode.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\Plugin\migrate\destination\DestinationBase implements MigrateDestinationInterface, RequirementsInterface
- class \Drupal\migrate\Plugin\migrate\destination\Entity implements DependentPluginInterface, ContainerFactoryPluginInterface uses DependencyTrait, EntityFieldDefinitionTrait
- class \Drupal\migrate\Plugin\migrate\destination\EntityConfigBase
- class \Drupal\migrate\Plugin\migrate\destination\Entity implements DependentPluginInterface, ContainerFactoryPluginInterface uses DependencyTrait, EntityFieldDefinitionTrait
- class \Drupal\migrate\Plugin\migrate\destination\DestinationBase implements MigrateDestinationInterface, RequirementsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EntityConfigBase
8 files declare their use of EntityConfigBase
- DestinationCategoryTest.php in core/
modules/ migrate_drupal/ tests/ src/ Kernel/ Plugin/ migrate/ DestinationCategoryTest.php - EntityBlock.php in core/
modules/ block/ src/ Plugin/ migrate/ destination/ EntityBlock.php - EntityCommentType.php in core/
modules/ comment/ src/ Plugin/ migrate/ destination/ EntityCommentType.php - EntityDateFormat.php in core/
modules/ system/ src/ Plugin/ migrate/ destination/ EntityDateFormat.php - EntityImageStyle.php in core/
modules/ image/ src/ Plugin/ migrate/ destination/ EntityImageStyle.php
File
- core/
modules/ migrate/ src/ Plugin/ migrate/ destination/ EntityConfigBase.php, line 63
Namespace
Drupal\migrate\Plugin\migrate\destinationView source
class EntityConfigBase extends Entity {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Construct a new entity.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* The migration.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The storage for this entity type.
* @param array $bundles
* The list of bundles this entity type has.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
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) {
// Ids is keyed by the key name so grab the keys.
$id_keys = array_keys($ids);
if (!$row
->getDestinationProperty($id_key)) {
// Set the ID into the destination in for form "val1.val2.val3".
$row
->setDestinationProperty($id_key, $this
->generateId($row, $id_keys));
}
}
$entity = $this
->getEntity($row, $old_destination_id_values);
// Translations are already saved in updateEntity by configuration override.
if (!$this
->isTranslationDestination()) {
$entity
->save();
}
if (count($ids) > 1) {
// This can only be a config entity, content entities have their ID key
// and that's it.
$return = [];
foreach ($id_keys as $id_key) {
if ($this
->isTranslationDestination() && $id_key == 'langcode') {
// Config entities do not have a language property, get the language
// code from the destination.
$return[] = $row
->getDestinationProperty($id_key);
}
else {
$return[] = $entity
->get($id_key);
}
}
return $return;
}
return [
$entity
->id(),
];
}
/**
* Get whether this destination is for translations.
*
* @return bool
* Whether this destination is for translations.
*/
protected function isTranslationDestination() {
return !empty($this->configuration['translations']);
}
/**
* {@inheritdoc}
*/
public function getIds() {
$id_key = $this
->getKey('id');
$ids[$id_key]['type'] = 'string';
if ($this
->isTranslationDestination()) {
$ids['langcode']['type'] = 'string';
}
return $ids;
}
/**
* Updates an entity with the contents of a row.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to update.
* @param \Drupal\migrate\Row $row
* The row object to update from.
*/
protected function updateEntity(EntityInterface $entity, Row $row) {
// This is a translation if the language in the active config does not
// match the language of this row.
$translation = FALSE;
if ($row
->hasDestinationProperty('langcode') && $this->languageManager instanceof ConfigurableLanguageManager) {
$config = $entity
->getConfigDependencyName();
$langcode = $this->configFactory
->get('langcode');
if ($langcode != $row
->getDestinationProperty('langcode')) {
$translation = TRUE;
}
}
if ($translation) {
$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());
}
}
/**
* Updates a (possible nested) entity property with a value.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The config entity.
* @param array $parents
* The array of parents.
* @param string|object $value
* The value to update to.
*/
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);
}
/**
* Generates an entity ID.
*
* @param \Drupal\migrate\Row $row
* The current row.
* @param array $ids
* The destination IDs.
*
* @return string
* The generated entity ID.
*/
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);
}
/**
* {@inheritdoc}
*/
public function rollback(array $destination_identifier) {
if ($this
->isTranslationDestination()) {
// The entity id does not include the langcode.
$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);
// Rollback the translation.
$config_override
->delete();
}
else {
$destination_identifier = implode('.', $destination_identifier);
parent::rollback([
$destination_identifier,
]);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
DestinationBase:: |
protected | property | The migration. | |
DestinationBase:: |
protected | property | The rollback action to be saved for the last imported item. | |
DestinationBase:: |
protected | property | Indicates whether the destination can be rolled back. | |
DestinationBase:: |
public | function |
Checks if requirements for this plugin are OK. Overrides RequirementsInterface:: |
|
DestinationBase:: |
public | function |
Gets the destination module handling the destination data. Overrides MigrateDestinationInterface:: |
1 |
DestinationBase:: |
public | function |
The rollback action for the last imported item. Overrides MigrateDestinationInterface:: |
|
DestinationBase:: |
protected | function | For a destination item being updated, set the appropriate rollback action. | |
DestinationBase:: |
public | function |
Whether the destination can be rolled back or not. Overrides MigrateDestinationInterface:: |
|
Entity:: |
protected | property | The list of the bundles of this entity type. | |
Entity:: |
protected | property | The entity storage. | |
Entity:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
Entity:: |
public | function |
Returns an array of destination fields. Overrides MigrateDestinationInterface:: |
|
Entity:: |
public | function | Gets the bundle for the row taking into account the default. | |
Entity:: |
protected | function | Creates or loads an entity. | 5 |
Entity:: |
protected | function | Gets the entity ID of the row. | 2 |
Entity:: |
protected | function | Returns a specific entity key. | |
EntityConfigBase:: |
protected | property | The configuration factory. | |
EntityConfigBase:: |
protected | property | The language manager. | |
EntityConfigBase:: |
public static | function |
Creates an instance of the plugin. Overrides Entity:: |
2 |
EntityConfigBase:: |
protected | function | Generates an entity ID. | |
EntityConfigBase:: |
public | function |
Gets the destination IDs. Overrides MigrateDestinationInterface:: |
3 |
EntityConfigBase:: |
public | function |
Import the row. Overrides MigrateDestinationInterface:: |
4 |
EntityConfigBase:: |
protected | function | Get whether this destination is for translations. | |
EntityConfigBase:: |
public | function |
Delete the specified destination object from the target Drupal. Overrides Entity:: |
2 |
EntityConfigBase:: |
protected | function | Updates an entity with the contents of a row. | 1 |
EntityConfigBase:: |
protected | function | Updates a (possible nested) entity property with a value. | 1 |
EntityConfigBase:: |
public | function |
Construct a new entity. Overrides Entity:: |
2 |
EntityFieldDefinitionTrait:: |
protected | function | Gets the field definition from a specific entity base field. | |
EntityFieldDefinitionTrait:: |
protected static | function | Finds the entity type from configuration or plugin ID. | 5 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |