/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $configName = $input->getArgument('name'); $fileName = $input->getArgument('file'); $ymlFile = new Parser(); if (!empty($fileName) && file_exists($fileName)) { $value = $ymlFile->parse(file_get_contents($fileName)); } else { $value = $ymlFile->parse(stream_get_contents(fopen("php://stdin", "r"))); } if (empty($value)) { $io->error($this->trans('commands.config.import.single.messages.empty-value')); return; } try { $source_storage = new StorageReplaceDataWrapper($this->configStorage); $source_storage->replaceData($configName, $value); $storage_comparer = new StorageComparer($source_storage, $this->configStorage, $this->configManager); if ($this->configImport($io, $storage_comparer)) { $io->success(sprintf($this->trans('commands.config.import.single.messages.success'), $configName)); } } catch (\Exception $e) { $io->error($e->getMessage()); return 1; } }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { // The confirmation step needs no additional validation. if ($this->data) { return; } // Decode the submitted import. $data = Yaml::decode($form_state->getValue('import')); // Validate for config entities. if ($form_state->getValue('config_type') !== 'system.simple') { $definition = $this->entityManager->getDefinition($form_state->getValue('config_type')); $id_key = $definition->getKey('id'); // If a custom entity ID is specified, override the value in the // configuration data being imported. if (!$form_state->isValueEmpty('custom_entity_id')) { $data[$id_key] = $form_state->getValue('custom_entity_id'); } $entity_storage = $this->entityManager->getStorage($form_state->getValue('config_type')); // If an entity ID was not specified, set an error. if (!isset($data[$id_key])) { $form_state->setErrorByName('import', $this->t('Missing ID key "@id_key" for this @entity_type import.', array('@id_key' => $id_key, '@entity_type' => $definition->getLabel()))); return; } $config_name = $definition->getConfigPrefix() . '.' . $data[$id_key]; // If there is an existing entity, ensure matching ID and UUID. if ($entity = $entity_storage->load($data[$id_key])) { $this->configExists = $entity; if (!isset($data['uuid'])) { $form_state->setErrorByName('import', $this->t('An entity with this machine name already exists but the import did not specify a UUID.')); return; } if ($data['uuid'] !== $entity->uuid()) { $form_state->setErrorByName('import', $this->t('An entity with this machine name already exists but the UUID does not match.')); return; } } elseif (isset($data['uuid']) && $entity_storage->loadByProperties(array('uuid' => $data['uuid']))) { $form_state->setErrorByName('import', $this->t('An entity with this UUID already exists but the machine name does not match.')); } } else { $config_name = $form_state->getValue('config_name'); $config = $this->config($config_name); $this->configExists = !$config->isNew() ? $config : FALSE; } // Use ConfigImporter validation. if (!$form_state->getErrors()) { $source_storage = new StorageReplaceDataWrapper($this->configStorage); $source_storage->replaceData($config_name, $data); $storage_comparer = new StorageComparer($source_storage, $this->configStorage, $this->configManager); if (!$storage_comparer->createChangelist()->hasChanges()) { $form_state->setErrorByName('import', $this->t('There are no changes to import.')); } else { $config_importer = new ConfigImporter($storage_comparer, $this->eventDispatcher, $this->configManager, $this->lock, $this->typedConfigManager, $this->moduleHandler, $this->moduleInstaller, $this->themeHandler, $this->getStringTranslation()); try { $config_importer->validate(); $form_state->set('config_importer', $config_importer); } catch (ConfigImporterException $e) { // There are validation errors. $item_list = ['#theme' => 'item_list', '#items' => $config_importer->getErrors(), '#title' => $this->t('The configuration cannot be imported because it failed validation for the following reasons:')]; $form_state->setErrorByName('import', $this->renderer->render($item_list)); } } } // Store the decoded version of the submitted import. $form_state->setValueForElement($form['import'], $data); }