View source
<?php
namespace Drupal\config\Form;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfigSingleExportForm extends FormBase {
protected $entityManager;
protected $configStorage;
protected $definitions = array();
public function __construct(EntityManagerInterface $entity_manager, StorageInterface $config_storage) {
$this->entityManager = $entity_manager;
$this->configStorage = $config_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.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) {
foreach ($this->entityManager
->getDefinitions() as $entity_type => $definition) {
if ($definition
->isSubclassOf('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface')) {
$this->definitions[$entity_type] = $definition;
}
}
$entity_types = array_map(function (EntityTypeInterface $definition) {
return $definition
->getLabel();
}, $this->definitions);
uasort($entity_types, 'strnatcasecmp');
$config_types = array(
'system.simple' => $this
->t('Simple configuration'),
) + $entity_types;
$form['config_type'] = array(
'#title' => $this
->t('Configuration type'),
'#type' => 'select',
'#options' => $config_types,
'#default_value' => $config_type,
'#ajax' => array(
'callback' => '::updateConfigurationType',
'wrapper' => 'edit-config-type-wrapper',
),
);
$default_type = $form_state
->getValue('config_type', $config_type);
$form['config_name'] = array(
'#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' => array(
'callback' => '::updateExport',
'wrapper' => 'edit-export-wrapper',
),
);
$form['export'] = array(
'#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'));
return $form['config_name'];
}
public function updateExport($form, FormStateInterface $form_state) {
if ($form_state
->getValue('config_type') !== 'system.simple') {
$definition = $this->entityManager
->getDefinition($form_state
->getValue('config_type'));
$name = $definition
->getConfigPrefix() . '.' . $form_state
->getValue('config_name');
}
else {
$name = $form_state
->getValue('config_name');
}
$form['export']['#value'] = Yaml::encode($this->configStorage
->read($name));
$form['export']['#description'] = $this
->t('Filename: %name', array(
'%name' => $name . '.yml',
));
return $form['export'];
}
protected function findConfiguration($config_type) {
$names = array(
'' => $this
->t('- Select -'),
);
if ($config_type && $config_type !== 'system.simple') {
$entity_storage = $this->entityManager
->getStorage($config_type);
foreach ($entity_storage
->loadMultiple() as $entity) {
$entity_id = $entity
->id();
$label = $entity
->label() ?: $entity_id;
$names[$entity_id] = $label;
}
}
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 (strpos($config_name, $config_prefix) === 0) {
unset($names[$config_name]);
}
}
}
}
return $names;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}