3 namespace Drupal\migrate\Plugin\migrate\destination;
5 use Drupal\Component\Plugin\DependentPluginInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Entity\DependencyTrait;
8 use Drupal\Core\Language\LanguageManagerInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\migrate\Plugin\MigrationInterface;
11 use Drupal\migrate\Row;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Provides Configuration Management destination plugin.
17 * Persists data to the config system.
19 * Available configuration keys:
20 * - store null: (optional) Boolean, if TRUE, when a property is NULL, NULL is
21 * stored, otherwise the default is used. Defaults to FALSE.
22 * - translations: (optional) Boolean, if TRUE, the destination will be
23 * associated with the langcode provided by the source plugin. Defaults to
26 * Destination properties expected in the imported row:
27 * - config_name: The machine name of the config.
28 * - langcode: (optional) The language code of the config.
38 * use_admin_theme: node_admin_theme
41 * config_name: node.settings
44 * This will add the value of the variable "node_admin_theme" to the config with
45 * the machine name "node.settings" as "node.settings.use_admin_theme".
49 * plugin: i18n_variable
51 * - site_offline_message
54 * message: site_offline_message
57 * config_name: system.maintenance
61 * This will add the value of the variable "site_offline_message" to the config
62 * with the machine name "system.maintenance" as "system.maintenance.message",
63 * coupled with the relevant langcode as obtained from the "i18n_variable"
66 * @see \Drupal\migrate_drupal\Plugin\migrate\source\d6\i18nVariable
68 * @MigrateDestination(
72 class Config extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
79 * @var \Drupal\Core\Config\Config
84 * The language manager.
86 * @var \Drupal\Core\Language\LanguageManagerInterface
88 protected $language_manager;
91 * Constructs a Config destination object.
93 * @param array $configuration
94 * A configuration array containing information about the plugin instance.
95 * @param string $plugin_id
96 * The plugin ID for the plugin instance.
97 * @param mixed $plugin_definition
98 * The plugin implementation definition.
99 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
100 * The migration entity.
101 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
102 * The configuration factory.
103 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
104 * The language manager.
106 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
107 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
108 $this->config = $config_factory->getEditable($configuration['config_name']);
109 $this->language_manager = $language_manager;
110 if ($this->isTranslationDestination()) {
111 $this->supportsRollback = TRUE;
118 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
124 $container->get('config.factory'),
125 $container->get('language_manager')
132 public function import(Row $row, array $old_destination_id_values = []) {
133 if ($this->isTranslationDestination()) {
134 $this->config = $this->language_manager->getLanguageConfigOverride($row->getDestinationProperty('langcode'), $this->config->getName());
137 foreach ($row->getRawDestination() as $key => $value) {
138 if (isset($value) || !empty($this->configuration['store null'])) {
139 $this->config->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $key), $value);
142 $this->config->save();
143 $ids[] = $this->config->getName();
144 if ($this->isTranslationDestination()) {
145 $ids[] = $row->getDestinationProperty('langcode');
153 public function fields(MigrationInterface $migration = NULL) {
154 // @todo Dynamically fetch fields using Config Schema API.
160 public function getIds() {
161 $ids['config_name']['type'] = 'string';
162 if ($this->isTranslationDestination()) {
163 $ids['langcode']['type'] = 'string';
171 public function calculateDependencies() {
172 $provider = explode('.', $this->config->getName(), 2)[0];
173 $this->addDependency('module', $provider);
174 return $this->dependencies;
178 * Get whether this destination is for translations.
181 * Whether this destination is for translations.
183 protected function isTranslationDestination() {
184 return !empty($this->configuration['translations']);
190 public function rollback(array $destination_identifier) {
191 if ($this->isTranslationDestination()) {
192 $language = $destination_identifier['langcode'];
193 $config = $this->language_manager->getLanguageConfigOverride($language, $this->config->getName());