Same filename and directory in other branches
- 8.9.x core/modules/config/src/Form/ConfigSingleExportForm.php
- 9 core/modules/config/src/Form/ConfigSingleExportForm.php
Namespace
Drupal\config\Form
File
core/modules/config/src/Form/ConfigSingleExportForm.php
View source
<?php
namespace Drupal\config\Form;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfigSingleExportForm extends FormBase {
protected $entityTypeManager;
protected $configStorage;
protected $definitions = [];
public function __construct(EntityTypeManagerInterface $entity_type_manager, StorageInterface $config_storage) {
$this->entityTypeManager = $entity_type_manager;
$this->configStorage = $config_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('config.storage'));
}
public function getFormId() {
return 'config_single_export_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
$form['#prefix'] = '<div id="js-config-form-wrapper">';
$form['#suffix'] = '</div>';
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type => $definition) {
if ($definition
->entityClassImplements(ConfigEntityInterface::class)) {
$this->definitions[$entity_type] = $definition;
}
}
$entity_types = array_map(function (EntityTypeInterface $definition) {
return $definition
->getLabel();
}, $this->definitions);
uasort($entity_types, 'strnatcasecmp');
$config_types = [
'system.simple' => $this
->t('Simple configuration'),
] + $entity_types;
$form['config_type'] = [
'#title' => $this
->t('Configuration type'),
'#type' => 'select',
'#options' => $config_types,
'#default_value' => $config_type,
'#ajax' => [
'callback' => '::updateConfigurationType',
'wrapper' => 'js-config-form-wrapper',
],
];
$default_type = $form_state
->getValue('config_type', $config_type);
$form['config_name'] = [
'#title' => $this
->t('Configuration name'),
'#type' => 'select',
'#options' => $this
->findConfiguration($default_type),
'#default_value' => $config_name,
'#prefix' => '<div id="edit-config-type-wrapper">',
'#suffix' => '</div>',
'#ajax' => [
'callback' => '::updateExport',
'wrapper' => 'edit-export-wrapper',
],
];
$form['export'] = [
'#title' => $this
->t('Here is your configuration:'),
'#type' => 'textarea',
'#rows' => 24,
'#prefix' => '<div id="edit-export-wrapper">',
'#suffix' => '</div>',
];
if ($config_type && $config_name) {
$fake_form_state = (new FormState())
->setValues([
'config_type' => $config_type,
'config_name' => $config_name,
]);
$form['export'] = $this
->updateExport($form, $fake_form_state);
}
return $form;
}
public function updateConfigurationType($form, FormStateInterface $form_state) {
$form['config_name']['#options'] = $this
->findConfiguration($form_state
->getValue('config_type'));
$form['export']['#value'] = NULL;
return $form;
}
public function updateExport($form, FormStateInterface $form_state) {
if ($form_state
->getValue('config_type') !== 'system.simple') {
$definition = $this->entityTypeManager
->getDefinition($form_state
->getValue('config_type'));
$name = $definition
->getConfigPrefix() . '.' . $form_state
->getValue('config_name');
}
else {
$name = $form_state
->getValue('config_name');
}
$exists = $this->configStorage
->exists($name);
$form['export']['#value'] = !$exists ? NULL : Yaml::encode($this->configStorage
->read($name));
$form['export']['#description'] = !$exists ? NULL : $this
->t('Filename: %name', [
'%name' => $name . '.yml',
]);
return $form['export'];
}
protected function findConfiguration($config_type) {
$names = [
'' => $this
->t('- Select -'),
];
if ($config_type && $config_type !== 'system.simple') {
$entity_storage = $this->entityTypeManager
->getStorage($config_type);
foreach ($entity_storage
->loadMultiple() as $entity) {
$entity_id = $entity
->id();
if ($label = $entity
->label()) {
$names[$entity_id] = new TranslatableMarkup('@id (@label)', [
'@label' => $label,
'@id' => $entity_id,
]);
}
else {
$names[$entity_id] = $entity_id;
}
}
}
else {
$config_prefixes = array_map(function (EntityTypeInterface $definition) {
return $definition
->getConfigPrefix() . '.';
}, $this->definitions);
$names += $this->configStorage
->listAll();
$names = array_combine($names, $names);
foreach ($names as $config_name) {
foreach ($config_prefixes as $config_prefix) {
if (str_starts_with($config_name, $config_prefix)) {
unset($names[$config_name]);
}
}
}
}
return $names;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
Classes