3 namespace Drupal\config\Form;
5 use Drupal\Core\Config\Entity\ConfigEntityInterface;
6 use Drupal\Core\Config\StorageInterface;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Form\FormBase;
10 use Drupal\Core\Form\FormState;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Serialization\Yaml;
13 use Drupal\Core\StringTranslation\TranslatableMarkup;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Provides a form for exporting a single configuration file.
19 class ConfigSingleExportForm extends FormBase {
24 * @var \Drupal\Core\Entity\EntityManagerInterface
26 protected $entityManager;
31 * @var \Drupal\Core\Config\StorageInterface
33 protected $configStorage;
36 * Tracks the valid config entity type definitions.
38 * @var \Drupal\Core\Entity\EntityTypeInterface[]
40 protected $definitions = [];
43 * Constructs a new ConfigSingleImportForm.
45 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
47 * @param \Drupal\Core\Config\StorageInterface $config_storage
50 public function __construct(EntityManagerInterface $entity_manager, StorageInterface $config_storage) {
51 $this->entityManager = $entity_manager;
52 $this->configStorage = $config_storage;
58 public static function create(ContainerInterface $container) {
60 $container->get('entity.manager'),
61 $container->get('config.storage')
68 public function getFormId() {
69 return 'config_single_export_form';
75 public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
76 foreach ($this->entityManager->getDefinitions() as $entity_type => $definition) {
77 if ($definition->entityClassImplements(ConfigEntityInterface::class)) {
78 $this->definitions[$entity_type] = $definition;
81 $entity_types = array_map(function (EntityTypeInterface $definition) {
82 return $definition->getLabel();
83 }, $this->definitions);
84 // Sort the entity types by label, then add the simple config to the top.
85 uasort($entity_types, 'strnatcasecmp');
87 'system.simple' => $this->t('Simple configuration'),
89 $form['config_type'] = [
90 '#title' => $this->t('Configuration type'),
92 '#options' => $config_types,
93 '#default_value' => $config_type,
95 'callback' => '::updateConfigurationType',
96 'wrapper' => 'edit-config-type-wrapper',
99 $default_type = $form_state->getValue('config_type', $config_type);
100 $form['config_name'] = [
101 '#title' => $this->t('Configuration name'),
103 '#options' => $this->findConfiguration($default_type),
104 '#default_value' => $config_name,
105 '#prefix' => '<div id="edit-config-type-wrapper">',
106 '#suffix' => '</div>',
108 'callback' => '::updateExport',
109 'wrapper' => 'edit-export-wrapper',
114 '#title' => $this->t('Here is your configuration:'),
115 '#type' => 'textarea',
117 '#prefix' => '<div id="edit-export-wrapper">',
118 '#suffix' => '</div>',
120 if ($config_type && $config_name) {
121 $fake_form_state = (new FormState())->setValues([
122 'config_type' => $config_type,
123 'config_name' => $config_name,
125 $form['export'] = $this->updateExport($form, $fake_form_state);
131 * Handles switching the configuration type selector.
133 public function updateConfigurationType($form, FormStateInterface $form_state) {
134 $form['config_name']['#options'] = $this->findConfiguration($form_state->getValue('config_type'));
135 return $form['config_name'];
139 * Handles switching the export textarea.
141 public function updateExport($form, FormStateInterface $form_state) {
142 // Determine the full config name for the selected config entity.
143 if ($form_state->getValue('config_type') !== 'system.simple') {
144 $definition = $this->entityManager->getDefinition($form_state->getValue('config_type'));
145 $name = $definition->getConfigPrefix() . '.' . $form_state->getValue('config_name');
147 // The config name is used directly for simple configuration.
149 $name = $form_state->getValue('config_name');
151 // Read the raw data for this config name, encode it, and display it.
152 $form['export']['#value'] = Yaml::encode($this->configStorage->read($name));
153 $form['export']['#description'] = $this->t('Filename: %name', ['%name' => $name . '.yml']);
154 return $form['export'];
158 * Handles switching the configuration type selector.
160 protected function findConfiguration($config_type) {
162 '' => $this->t('- Select -'),
164 // For a given entity type, load all entities.
165 if ($config_type && $config_type !== 'system.simple') {
166 $entity_storage = $this->entityManager->getStorage($config_type);
167 foreach ($entity_storage->loadMultiple() as $entity) {
168 $entity_id = $entity->id();
169 if ($label = $entity->label()) {
170 $names[$entity_id] = new TranslatableMarkup('@label (@id)', ['@label' => $label, '@id' => $entity_id]);
173 $names[$entity_id] = $entity_id;
177 // Handle simple configuration.
179 // Gather the config entity prefixes.
180 $config_prefixes = array_map(function (EntityTypeInterface $definition) {
181 return $definition->getConfigPrefix() . '.';
182 }, $this->definitions);
184 // Find all config, and then filter our anything matching a config prefix.
185 $names = $this->configStorage->listAll();
186 $names = array_combine($names, $names);
187 foreach ($names as $config_name) {
188 foreach ($config_prefixes as $config_prefix) {
189 if (strpos($config_name, $config_prefix) === 0) {
190 unset($names[$config_name]);
201 public function submitForm(array &$form, FormStateInterface $form_state) {
202 // Nothing to submit.