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.
21 class ConfigSingleExportForm extends FormBase {
26 * @var \Drupal\Core\Entity\EntityManagerInterface
28 protected $entityManager;
33 * @var \Drupal\Core\Config\StorageInterface
35 protected $configStorage;
38 * Tracks the valid config entity type definitions.
40 * @var \Drupal\Core\Entity\EntityTypeInterface[]
42 protected $definitions = [];
45 * Constructs a new ConfigSingleImportForm.
47 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
49 * @param \Drupal\Core\Config\StorageInterface $config_storage
52 public function __construct(EntityManagerInterface $entity_manager, StorageInterface $config_storage) {
53 $this->entityManager = $entity_manager;
54 $this->configStorage = $config_storage;
60 public static function create(ContainerInterface $container) {
62 $container->get('entity.manager'),
63 $container->get('config.storage')
70 public function getFormId() {
71 return 'config_single_export_form';
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;
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');
89 'system.simple' => $this->t('Simple configuration'),
91 $form['config_type'] = [
92 '#title' => $this->t('Configuration type'),
94 '#options' => $config_types,
95 '#default_value' => $config_type,
97 'callback' => '::updateConfigurationType',
98 'wrapper' => 'edit-config-type-wrapper',
101 $default_type = $form_state->getValue('config_type', $config_type);
102 $form['config_name'] = [
103 '#title' => $this->t('Configuration name'),
105 '#options' => $this->findConfiguration($default_type),
106 '#default_value' => $config_name,
107 '#prefix' => '<div id="edit-config-type-wrapper">',
108 '#suffix' => '</div>',
110 'callback' => '::updateExport',
111 'wrapper' => 'edit-export-wrapper',
116 '#title' => $this->t('Here is your configuration:'),
117 '#type' => 'textarea',
119 '#prefix' => '<div id="edit-export-wrapper">',
120 '#suffix' => '</div>',
122 if ($config_type && $config_name) {
123 $fake_form_state = (new FormState())->setValues([
124 'config_type' => $config_type,
125 'config_name' => $config_name,
127 $form['export'] = $this->updateExport($form, $fake_form_state);
133 * Handles switching the configuration type selector.
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'];
141 * Handles switching the export textarea.
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');
149 // The config name is used directly for simple configuration.
151 $name = $form_state->getValue('config_name');
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'];
160 * Handles switching the configuration type selector.
162 protected function findConfiguration($config_type) {
164 '' => $this->t('- Select -'),
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]);
175 $names[$entity_id] = $entity_id;
179 // Handle simple configuration.
181 // Gather the config entity prefixes.
182 $config_prefixes = array_map(function (EntityTypeInterface $definition) {
183 return $definition->getConfigPrefix() . '.';
184 }, $this->definitions);
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]);
203 public function submitForm(array &$form, FormStateInterface $form_state) {
204 // Nothing to submit.