8710655d7f949493befa4353d2cd38fa3763f58f
[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 class ConfigSingleExportForm extends FormBase {
20
21   /**
22    * The entity manager.
23    *
24    * @var \Drupal\Core\Entity\EntityManagerInterface
25    */
26   protected $entityManager;
27
28   /**
29    * The config storage.
30    *
31    * @var \Drupal\Core\Config\StorageInterface
32    */
33   protected $configStorage;
34
35   /**
36    * Tracks the valid config entity type definitions.
37    *
38    * @var \Drupal\Core\Entity\EntityTypeInterface[]
39    */
40   protected $definitions = [];
41
42   /**
43    * Constructs a new ConfigSingleImportForm.
44    *
45    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
46    *   The entity manager.
47    * @param \Drupal\Core\Config\StorageInterface $config_storage
48    *   The config storage.
49    */
50   public function __construct(EntityManagerInterface $entity_manager, StorageInterface $config_storage) {
51     $this->entityManager = $entity_manager;
52     $this->configStorage = $config_storage;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container) {
59     return new static(
60       $container->get('entity.manager'),
61       $container->get('config.storage')
62     );
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function getFormId() {
69     return 'config_single_export_form';
70   }
71
72   /**
73    * {@inheritdoc}
74    */
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;
79       }
80     }
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');
86     $config_types = [
87       'system.simple' => $this->t('Simple configuration'),
88     ] + $entity_types;
89     $form['config_type'] = [
90       '#title' => $this->t('Configuration type'),
91       '#type' => 'select',
92       '#options' => $config_types,
93       '#default_value' => $config_type,
94       '#ajax' => [
95         'callback' => '::updateConfigurationType',
96         'wrapper' => 'edit-config-type-wrapper',
97       ],
98     ];
99     $default_type = $form_state->getValue('config_type', $config_type);
100     $form['config_name'] = [
101       '#title' => $this->t('Configuration name'),
102       '#type' => 'select',
103       '#options' => $this->findConfiguration($default_type),
104       '#default_value' => $config_name,
105       '#prefix' => '<div id="edit-config-type-wrapper">',
106       '#suffix' => '</div>',
107       '#ajax' => [
108         'callback' => '::updateExport',
109         'wrapper' => 'edit-export-wrapper',
110       ],
111     ];
112
113     $form['export'] = [
114       '#title' => $this->t('Here is your configuration:'),
115       '#type' => 'textarea',
116       '#rows' => 24,
117       '#prefix' => '<div id="edit-export-wrapper">',
118       '#suffix' => '</div>',
119     ];
120     if ($config_type && $config_name) {
121       $fake_form_state = (new FormState())->setValues([
122         'config_type' => $config_type,
123         'config_name' => $config_name,
124       ]);
125       $form['export'] = $this->updateExport($form, $fake_form_state);
126     }
127     return $form;
128   }
129
130   /**
131    * Handles switching the configuration type selector.
132    */
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'];
136   }
137
138   /**
139    * Handles switching the export textarea.
140    */
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');
146     }
147     // The config name is used directly for simple configuration.
148     else {
149       $name = $form_state->getValue('config_name');
150     }
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'];
155   }
156
157   /**
158    * Handles switching the configuration type selector.
159    */
160   protected function findConfiguration($config_type) {
161     $names = [
162       '' => $this->t('- Select -'),
163     ];
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]);
171         }
172         else {
173           $names[$entity_id] = $entity_id;
174         }
175       }
176     }
177     // Handle simple configuration.
178     else {
179       // Gather the config entity prefixes.
180       $config_prefixes = array_map(function (EntityTypeInterface $definition) {
181         return $definition->getConfigPrefix() . '.';
182       }, $this->definitions);
183
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]);
191           }
192         }
193       }
194     }
195     return $names;
196   }
197
198   /**
199    * {@inheritdoc}
200    */
201   public function submitForm(array &$form, FormStateInterface $form_state) {
202     // Nothing to submit.
203   }
204
205 }