3 namespace Drupal\migrate\Plugin\migrate\destination;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\language\ConfigurableLanguageManager;
11 use Drupal\migrate\Plugin\MigrationInterface;
12 use Drupal\migrate\MigrateException;
13 use Drupal\migrate\Plugin\MigrateIdMapInterface;
14 use Drupal\migrate\Row;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Base destination class for importing configuration entities.
20 * Available configuration keys:
21 * - translations: (optional) Boolean, if TRUE, the destination will be
22 * associated with the langcode provided by the source plugin. Defaults to
29 * plugin: d7_block_custom
36 * plugin: entity:block
39 * This will save the migrated, processed row as a block config entity.
43 * plugin: d6_i18n_profile_field
49 * entity_type: 'constants/entity_type'
50 * bundle: 'constants/bundle'
53 * translation: translation
55 * plugin: entity:field_config
59 * Because the translations configuration is set to "true", this will save the
60 * migrated, processed row to a "field_config" entity associated with the
61 * designated langcode.
63 class EntityConfigBase extends Entity {
66 * The language manager.
68 * @var \Drupal\Core\Language\LanguageManagerInterface
70 protected $languageManager;
73 * The configuration factory.
75 * @var \Drupal\Core\Config\ConfigFactoryInterface
77 protected $configFactory;
80 * Construct a new entity.
82 * @param array $configuration
83 * A configuration array containing information about the plugin instance.
84 * @param string $plugin_id
85 * The plugin_id for the plugin instance.
86 * @param mixed $plugin_definition
87 * The plugin implementation definition.
88 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
90 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
91 * The storage for this entity type.
92 * @param array $bundles
93 * The list of bundles this entity type has.
94 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
95 * The language manager.
96 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
97 * The configuration factory.
99 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory) {
100 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles);
101 $this->languageManager = $language_manager;
102 $this->configFactory = $config_factory;
108 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
109 $entity_type_id = static::getEntityTypeId($plugin_id);
115 $container->get('entity.manager')->getStorage($entity_type_id),
116 array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id)),
117 $container->get('language_manager'),
118 $container->get('config.factory')
125 public function import(Row $row, array $old_destination_id_values = []) {
126 if ($row->isStub()) {
127 throw new MigrateException('Config entities can not be stubbed.');
129 $this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
130 $ids = $this->getIds();
131 $id_key = $this->getKey('id');
132 if (count($ids) > 1) {
133 // Ids is keyed by the key name so grab the keys.
134 $id_keys = array_keys($ids);
135 if (!$row->getDestinationProperty($id_key)) {
136 // Set the ID into the destination in for form "val1.val2.val3".
137 $row->setDestinationProperty($id_key, $this->generateId($row, $id_keys));
140 $entity = $this->getEntity($row, $old_destination_id_values);
141 // Translations are already saved in updateEntity by configuration override.
142 if (!$this->isTranslationDestination()) {
145 if (count($ids) > 1) {
146 // This can only be a config entity, content entities have their ID key
149 foreach ($id_keys as $id_key) {
150 if (($this->isTranslationDestination()) && ($id_key == 'langcode')) {
151 // Config entities do not have a language property, get the language
152 // code from the destination.
153 $return[] = $row->getDestinationProperty($id_key);
156 $return[] = $entity->get($id_key);
161 return [$entity->id()];
165 * Get whether this destination is for translations.
168 * Whether this destination is for translations.
170 protected function isTranslationDestination() {
171 return !empty($this->configuration['translations']);
177 public function getIds() {
178 $id_key = $this->getKey('id');
179 $ids[$id_key]['type'] = 'string';
180 if ($this->isTranslationDestination()) {
181 $ids['langcode']['type'] = 'string';
187 * Updates an entity with the contents of a row.
189 * @param \Drupal\Core\Entity\EntityInterface $entity
190 * The entity to update.
191 * @param \Drupal\migrate\Row $row
192 * The row object to update from.
194 protected function updateEntity(EntityInterface $entity, Row $row) {
195 // This is a translation if the language in the active config does not
196 // match the language of this row.
197 $translation = FALSE;
198 if ($row->hasDestinationProperty('langcode') && $this->languageManager instanceof ConfigurableLanguageManager) {
199 $config = $entity->getConfigDependencyName();
200 $langcode = $this->configFactory->get('langcode');
201 if ($langcode != $row->getDestinationProperty('langcode')) {
207 $config_override = $this->languageManager->getLanguageConfigOverride($row->getDestinationProperty('langcode'), $config);
208 $config_override->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $row->getDestinationProperty('property')), $row->getDestinationProperty('translation'));
209 $config_override->save();
212 foreach ($row->getRawDestination() as $property => $value) {
213 $this->updateEntityProperty($entity, explode(Row::PROPERTY_SEPARATOR, $property), $value);
215 $this->setRollbackAction($row->getIdMap());
220 * Updates a (possible nested) entity property with a value.
222 * @param \Drupal\Core\Entity\EntityInterface $entity
224 * @param array $parents
225 * The array of parents.
226 * @param string|object $value
227 * The value to update to.
229 protected function updateEntityProperty(EntityInterface $entity, array $parents, $value) {
230 $top_key = array_shift($parents);
231 $entity_value = $entity->get($top_key);
232 if (is_array($entity_value)) {
233 NestedArray::setValue($entity_value, $parents, $value);
236 $entity_value = $value;
238 $entity->set($top_key, $entity_value);
242 * Generates an entity ID.
244 * @param \Drupal\migrate\Row $row
247 * The destination IDs.
250 * The generated entity ID.
252 protected function generateId(Row $row, array $ids) {
254 foreach ($ids as $id) {
255 if ($this->isTranslationDestination() && $id == 'langcode') {
258 $id_values[] = $row->getDestinationProperty($id);
260 return implode('.', $id_values);
266 public function rollback(array $destination_identifier) {
267 if ($this->isTranslationDestination()) {
268 // The entity id does not include the langcode.
270 foreach ($destination_identifier as $key => $value) {
271 if ($this->isTranslationDestination() && $key === 'langcode') {
274 $id_values[] = $value;
276 $entity_id = implode('.', $id_values);
277 $language = $destination_identifier['langcode'];
279 $config = $this->storage->load($entity_id)->getConfigDependencyName();
280 $config_override = $this->languageManager->getLanguageConfigOverride($language, $config);
281 // Rollback the translation.
282 $config_override->delete();
285 $destination_identifier = implode('.', $destination_identifier);
286 parent::rollback([$destination_identifier]);