annotate core/modules/migrate/src/Plugin/migrate/destination/Config.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\migrate\Plugin\migrate\destination;
Chris@0 4
Chris@0 5 use Drupal\Component\Plugin\DependentPluginInterface;
Chris@0 6 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 7 use Drupal\Core\Entity\DependencyTrait;
Chris@0 8 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Chris@0 10 use Drupal\migrate\Plugin\MigrationInterface;
Chris@0 11 use Drupal\migrate\Row;
Chris@0 12 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Provides Configuration Management destination plugin.
Chris@0 16 *
Chris@0 17 * Persists data to the config system.
Chris@0 18 *
Chris@0 19 * Available configuration keys:
Chris@0 20 * - store null: (optional) Boolean, if TRUE, when a property is NULL, NULL is
Chris@0 21 * stored, otherwise the default is used. Defaults to FALSE.
Chris@0 22 * - translations: (optional) Boolean, if TRUE, the destination will be
Chris@0 23 * associated with the langcode provided by the source plugin. Defaults to
Chris@0 24 * FALSE.
Chris@0 25 *
Chris@0 26 * Destination properties expected in the imported row:
Chris@0 27 * - config_name: The machine name of the config.
Chris@0 28 * - langcode: (optional) The language code of the config.
Chris@0 29 *
Chris@0 30 * Examples:
Chris@0 31 *
Chris@0 32 * @code
Chris@0 33 * source:
Chris@0 34 * plugin: variable
Chris@0 35 * variables:
Chris@0 36 * - node_admin_theme
Chris@0 37 * process:
Chris@0 38 * use_admin_theme: node_admin_theme
Chris@0 39 * destination:
Chris@0 40 * plugin: config
Chris@0 41 * config_name: node.settings
Chris@0 42 * @endcode
Chris@0 43 *
Chris@0 44 * This will add the value of the variable "node_admin_theme" to the config with
Chris@0 45 * the machine name "node.settings" as "node.settings.use_admin_theme".
Chris@0 46 *
Chris@0 47 * @code
Chris@0 48 * source:
Chris@0 49 * plugin: i18n_variable
Chris@0 50 * variables:
Chris@0 51 * - site_offline_message
Chris@0 52 * process:
Chris@0 53 * langcode: language
Chris@0 54 * message: site_offline_message
Chris@0 55 * destination:
Chris@0 56 * plugin: config
Chris@0 57 * config_name: system.maintenance
Chris@0 58 * translations: true
Chris@0 59 * @endcode
Chris@0 60 *
Chris@0 61 * This will add the value of the variable "site_offline_message" to the config
Chris@0 62 * with the machine name "system.maintenance" as "system.maintenance.message",
Chris@0 63 * coupled with the relevant langcode as obtained from the "i18n_variable"
Chris@0 64 * source plugin.
Chris@0 65 *
Chris@0 66 * @see \Drupal\migrate_drupal\Plugin\migrate\source\d6\i18nVariable
Chris@0 67 *
Chris@0 68 * @MigrateDestination(
Chris@0 69 * id = "config"
Chris@0 70 * )
Chris@0 71 */
Chris@0 72 class Config extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
Chris@0 73
Chris@0 74 use DependencyTrait;
Chris@0 75
Chris@0 76 /**
Chris@0 77 * The config object.
Chris@0 78 *
Chris@0 79 * @var \Drupal\Core\Config\Config
Chris@0 80 */
Chris@0 81 protected $config;
Chris@0 82
Chris@0 83 /**
Chris@0 84 * The language manager.
Chris@0 85 *
Chris@0 86 * @var \Drupal\Core\Language\LanguageManagerInterface
Chris@0 87 */
Chris@0 88 protected $language_manager;
Chris@0 89
Chris@0 90 /**
Chris@0 91 * Constructs a Config destination object.
Chris@0 92 *
Chris@0 93 * @param array $configuration
Chris@0 94 * A configuration array containing information about the plugin instance.
Chris@0 95 * @param string $plugin_id
Chris@0 96 * The plugin ID for the plugin instance.
Chris@0 97 * @param mixed $plugin_definition
Chris@0 98 * The plugin implementation definition.
Chris@0 99 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
Chris@0 100 * The migration entity.
Chris@0 101 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 102 * The configuration factory.
Chris@0 103 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 104 * The language manager.
Chris@0 105 */
Chris@0 106 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
Chris@0 107 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
Chris@0 108 $this->config = $config_factory->getEditable($configuration['config_name']);
Chris@0 109 $this->language_manager = $language_manager;
Chris@0 110 if ($this->isTranslationDestination()) {
Chris@0 111 $this->supportsRollback = TRUE;
Chris@0 112 }
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * {@inheritdoc}
Chris@0 117 */
Chris@0 118 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
Chris@0 119 return new static(
Chris@0 120 $configuration,
Chris@0 121 $plugin_id,
Chris@0 122 $plugin_definition,
Chris@0 123 $migration,
Chris@0 124 $container->get('config.factory'),
Chris@0 125 $container->get('language_manager')
Chris@0 126 );
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * {@inheritdoc}
Chris@0 131 */
Chris@0 132 public function import(Row $row, array $old_destination_id_values = []) {
Chris@0 133 if ($this->isTranslationDestination()) {
Chris@0 134 $this->config = $this->language_manager->getLanguageConfigOverride($row->getDestinationProperty('langcode'), $this->config->getName());
Chris@0 135 }
Chris@0 136
Chris@0 137 foreach ($row->getRawDestination() as $key => $value) {
Chris@0 138 if (isset($value) || !empty($this->configuration['store null'])) {
Chris@0 139 $this->config->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $key), $value);
Chris@0 140 }
Chris@0 141 }
Chris@0 142 $this->config->save();
Chris@0 143 $ids[] = $this->config->getName();
Chris@0 144 if ($this->isTranslationDestination()) {
Chris@0 145 $ids[] = $row->getDestinationProperty('langcode');
Chris@0 146 }
Chris@0 147 return $ids;
Chris@0 148 }
Chris@0 149
Chris@0 150 /**
Chris@0 151 * {@inheritdoc}
Chris@0 152 */
Chris@0 153 public function fields(MigrationInterface $migration = NULL) {
Chris@0 154 // @todo Dynamically fetch fields using Config Schema API.
Chris@0 155 }
Chris@0 156
Chris@0 157 /**
Chris@0 158 * {@inheritdoc}
Chris@0 159 */
Chris@0 160 public function getIds() {
Chris@0 161 $ids['config_name']['type'] = 'string';
Chris@0 162 if ($this->isTranslationDestination()) {
Chris@0 163 $ids['langcode']['type'] = 'string';
Chris@0 164 }
Chris@0 165 return $ids;
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * {@inheritdoc}
Chris@0 170 */
Chris@0 171 public function calculateDependencies() {
Chris@0 172 $provider = explode('.', $this->config->getName(), 2)[0];
Chris@0 173 $this->addDependency('module', $provider);
Chris@0 174 return $this->dependencies;
Chris@0 175 }
Chris@0 176
Chris@0 177 /**
Chris@0 178 * Get whether this destination is for translations.
Chris@0 179 *
Chris@0 180 * @return bool
Chris@0 181 * Whether this destination is for translations.
Chris@0 182 */
Chris@0 183 protected function isTranslationDestination() {
Chris@0 184 return !empty($this->configuration['translations']);
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * {@inheritdoc}
Chris@0 189 */
Chris@0 190 public function rollback(array $destination_identifier) {
Chris@0 191 if ($this->isTranslationDestination()) {
Chris@0 192 $language = $destination_identifier['langcode'];
Chris@0 193 $config = $this->language_manager->getLanguageConfigOverride($language, $this->config->getName());
Chris@0 194 $config->delete();
Chris@0 195 }
Chris@0 196 }
Chris@0 197
Chris@0 198 /**
Chris@0 199 * {@inheritdoc}
Chris@0 200 */
Chris@0 201 public function getDestinationModule() {
Chris@0 202 if (!empty($this->configuration['destination_module'])) {
Chris@0 203 return $this->configuration['destination_module'];
Chris@0 204 }
Chris@0 205 if (!empty($this->pluginDefinition['destination_module'])) {
Chris@0 206 return $this->pluginDefinition['destination_module'];
Chris@0 207 }
Chris@0 208 // Config translations require the config_translation module so set the
Chris@0 209 // migration provider to 'config_translation'. The corresponding non
Chris@0 210 // translated configuration is expected to be handled in a separate
Chris@0 211 // migration.
Chris@0 212 if (isset($this->configuration['translations'])) {
Chris@0 213 return 'config_translation';
Chris@0 214 }
Chris@0 215 // Get the module handling this configuration object from the config_name,
Chris@0 216 // which is of the form <module_name>.<configuration object name>
Chris@0 217 return !empty($this->configuration['config_name']) ? explode('.', $this->configuration['config_name'], 2)[0] : NULL;
Chris@0 218 }
Chris@0 219
Chris@0 220 }