Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / config / src / Form / ConfigSingleExportForm.php
1 <?php
2
3 namespace Drupal\config\Form;
4
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;
15
16 /**
17  * Provides a form for exporting a single configuration file.
18  *
19  * @internal
20  */
21 class ConfigSingleExportForm extends FormBase {
22
23   /**
24    * The entity manager.
25    *
26    * @var \Drupal\Core\Entity\EntityManagerInterface
27    */
28   protected $entityManager;
29
30   /**
31    * The config storage.
32    *
33    * @var \Drupal\Core\Config\StorageInterface
34    */
35   protected $configStorage;
36
37   /**
38    * Tracks the valid config entity type definitions.
39    *
40    * @var \Drupal\Core\Entity\EntityTypeInterface[]
41    */
42   protected $definitions = [];
43
44   /**
45    * Constructs a new ConfigSingleImportForm.
46    *
47    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
48    *   The entity manager.
49    * @param \Drupal\Core\Config\StorageInterface $config_storage
50    *   The config storage.
51    */
52   public function __construct(EntityManagerInterface $entity_manager, StorageInterface $config_storage) {
53     $this->entityManager = $entity_manager;
54     $this->configStorage = $config_storage;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container) {
61     return new static(
62       $container->get('entity.manager'),
63       $container->get('config.storage')
64     );
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function getFormId() {
71     return 'config_single_export_form';
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
78     foreach ($this->entityManager->getDefinitions() as $entity_type => $definition) {
79       if ($definition->entityClassImplements(ConfigEntityInterface::class)) {
80         $this->definitions[$entity_type] = $definition;
81       }
82     }
83     $entity_types = array_map(function (EntityTypeInterface $definition) {
84       return $definition->getLabel();
85     }, $this->definitions);
86     // Sort the entity types by label, then add the simple config to the top.
87     uasort($entity_types, 'strnatcasecmp');
88     $config_types = [
89       'system.simple' => $this->t('Simple configuration'),
90     ] + $entity_types;
91     $form['config_type'] = [
92       '#title' => $this->t('Configuration type'),
93       '#type' => 'select',
94       '#options' => $config_types,
95       '#default_value' => $config_type,
96       '#ajax' => [
97         'callback' => '::updateConfigurationType',
98         'wrapper' => 'edit-config-type-wrapper',
99       ],
100     ];
101     $default_type = $form_state->getValue('config_type', $config_type);
102     $form['config_name'] = [
103       '#title' => $this->t('Configuration name'),
104       '#type' => 'select',
105       '#options' => $this->findConfiguration($default_type),
106       '#default_value' => $config_name,
107       '#prefix' => '<div id="edit-config-type-wrapper">',
108       '#suffix' => '</div>',
109       '#ajax' => [
110         'callback' => '::updateExport',
111         'wrapper' => 'edit-export-wrapper',
112       ],
113     ];
114
115     $form['export'] = [
116       '#title' => $this->t('Here is your configuration:'),
117       '#type' => 'textarea',
118       '#rows' => 24,
119       '#prefix' => '<div id="edit-export-wrapper">',
120       '#suffix' => '</div>',
121     ];
122     if ($config_type && $config_name) {
123       $fake_form_state = (new FormState())->setValues([
124         'config_type' => $config_type,
125         'config_name' => $config_name,
126       ]);
127       $form['export'] = $this->updateExport($form, $fake_form_state);
128     }
129     return $form;
130   }
131
132   /**
133    * Handles switching the configuration type selector.
134    */
135   public function updateConfigurationType($form, FormStateInterface $form_state) {
136     $form['config_name']['#options'] = $this->findConfiguration($form_state->getValue('config_type'));
137     return $form['config_name'];
138   }
139
140   /**
141    * Handles switching the export textarea.
142    */
143   public function updateExport($form, FormStateInterface $form_state) {
144     // Determine the full config name for the selected config entity.
145     if ($form_state->getValue('config_type') !== 'system.simple') {
146       $definition = $this->entityManager->getDefinition($form_state->getValue('config_type'));
147       $name = $definition->getConfigPrefix() . '.' . $form_state->getValue('config_name');
148     }
149     // The config name is used directly for simple configuration.
150     else {
151       $name = $form_state->getValue('config_name');
152     }
153     // Read the raw data for this config name, encode it, and display it.
154     $form['export']['#value'] = Yaml::encode($this->configStorage->read($name));
155     $form['export']['#description'] = $this->t('Filename: %name', ['%name' => $name . '.yml']);
156     return $form['export'];
157   }
158
159   /**
160    * Handles switching the configuration type selector.
161    */
162   protected function findConfiguration($config_type) {
163     $names = [
164       '' => $this->t('- Select -'),
165     ];
166     // For a given entity type, load all entities.
167     if ($config_type && $config_type !== 'system.simple') {
168       $entity_storage = $this->entityManager->getStorage($config_type);
169       foreach ($entity_storage->loadMultiple() as $entity) {
170         $entity_id = $entity->id();
171         if ($label = $entity->label()) {
172           $names[$entity_id] = new TranslatableMarkup('@label (@id)', ['@label' => $label, '@id' => $entity_id]);
173         }
174         else {
175           $names[$entity_id] = $entity_id;
176         }
177       }
178     }
179     // Handle simple configuration.
180     else {
181       // Gather the config entity prefixes.
182       $config_prefixes = array_map(function (EntityTypeInterface $definition) {
183         return $definition->getConfigPrefix() . '.';
184       }, $this->definitions);
185
186       // Find all config, and then filter our anything matching a config prefix.
187       $names = $this->configStorage->listAll();
188       $names = array_combine($names, $names);
189       foreach ($names as $config_name) {
190         foreach ($config_prefixes as $config_prefix) {
191           if (strpos($config_name, $config_prefix) === 0) {
192             unset($names[$config_name]);
193           }
194         }
195       }
196     }
197     return $names;
198   }
199
200   /**
201    * {@inheritdoc}
202    */
203   public function submitForm(array &$form, FormStateInterface $form_state) {
204     // Nothing to submit.
205   }
206
207 }