Version 1
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / destination / Config.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\destination;
4
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;
13
14 /**
15  * Provides Configuration Management destination plugin.
16  *
17  * Persists data to the config system.
18  *
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
24  *   FALSE.
25  *
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.
29  *
30  * Examples:
31  *
32  * @code
33  * source:
34  *   plugin: variable
35  *   variables:
36  *     - node_admin_theme
37  * process:
38  *   use_admin_theme: node_admin_theme
39  * destination:
40  *   plugin: config
41  *   config_name: node.settings
42  * @endcode
43  *
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".
46  *
47  * @code
48  * source:
49  *   plugin: i18n_variable
50  *   variables:
51  *     - site_offline_message
52  * process:
53  *   langcode: language
54  *   message: site_offline_message
55  * destination:
56  *   plugin: config
57  *   config_name: system.maintenance
58  *   translations: true
59  * @endcode
60  *
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"
64  * source plugin.
65  *
66  * @see \Drupal\migrate_drupal\Plugin\migrate\source\d6\i18nVariable
67  *
68  * @MigrateDestination(
69  *   id = "config"
70  * )
71  */
72 class Config extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
73
74   use DependencyTrait;
75
76   /**
77    * The config object.
78    *
79    * @var \Drupal\Core\Config\Config
80    */
81   protected $config;
82
83   /**
84    * The language manager.
85    *
86    * @var \Drupal\Core\Language\LanguageManagerInterface
87    */
88   protected $language_manager;
89
90   /**
91    * Constructs a Config destination object.
92    *
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.
105    */
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;
112     }
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
119     return new static(
120       $configuration,
121       $plugin_id,
122       $plugin_definition,
123       $migration,
124       $container->get('config.factory'),
125       $container->get('language_manager')
126     );
127   }
128
129   /**
130    * {@inheritdoc}
131    */
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());
135     }
136
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);
140       }
141     }
142     $this->config->save();
143     $ids[] = $this->config->getName();
144     if ($this->isTranslationDestination()) {
145       $ids[] = $row->getDestinationProperty('langcode');
146     }
147     return $ids;
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public function fields(MigrationInterface $migration = NULL) {
154     // @todo Dynamically fetch fields using Config Schema API.
155   }
156
157   /**
158    * {@inheritdoc}
159    */
160   public function getIds() {
161     $ids['config_name']['type'] = 'string';
162     if ($this->isTranslationDestination()) {
163       $ids['langcode']['type'] = 'string';
164     }
165     return $ids;
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function calculateDependencies() {
172     $provider = explode('.', $this->config->getName(), 2)[0];
173     $this->addDependency('module', $provider);
174     return $this->dependencies;
175   }
176
177   /**
178    * Get whether this destination is for translations.
179    *
180    * @return bool
181    *   Whether this destination is for translations.
182    */
183   protected function isTranslationDestination() {
184     return !empty($this->configuration['translations']);
185   }
186
187   /**
188    * {@inheritdoc}
189    */
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());
194       $config->delete();
195     }
196   }
197
198 }