View source
<?php
namespace Drupal\key\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class KeyConfigOverrideAddForm extends EntityForm {
protected $entityTypeManager;
protected $configFactory;
protected $configStorage;
protected $storage;
protected $configEntityTypeDefinitions;
protected $requestStack;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, StorageInterface $config_storage, RequestStack $request_stack) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->configStorage = $config_storage;
$this->storage = $entity_type_manager
->getStorage('key_config_override');
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('config.factory'), $container
->get('config.storage'), $container
->get('request_stack'));
}
public function getFormId() {
return 'key_config_override_add_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config_type = $form_state
->getValue('config_type');
$config_name = $form_state
->getValue('config_name');
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Key configuration override name'),
'#description' => $this
->t('A human readable name for this override.'),
'#size' => 30,
'#maxlength' => 64,
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#required' => TRUE,
'#size' => 30,
'#maxlength' => 64,
'#machine_name' => [
'exists' => [
$this->storage,
'load',
],
],
];
$entity_types = array_map(function (EntityTypeInterface $definition) {
return $definition
->getLabel();
}, $this
->getConfigEntityTypeDefinitions());
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,
'#required' => TRUE,
'#ajax' => [
'callback' => '::changeConfigObject',
'wrapper' => 'edit-config-object-wrapper',
],
];
$form['config_object'] = [
'#type' => 'container',
'#prefix' => '<div id="edit-config-object-wrapper">',
'#suffix' => '</div>',
];
$form['config_object']['config_name'] = [
'#title' => $this
->t('Configuration name'),
'#type' => 'select',
'#options' => $this
->getConfigNames($config_type),
'#required' => TRUE,
'#ajax' => [
'callback' => '::changeConfigObject',
'wrapper' => 'edit-config-object-wrapper',
],
];
$form['config_object']['config_item'] = [
'#title' => $this
->t('Configuration item'),
'#type' => 'select',
'#options' => $this
->getConfigItems($config_type, $config_name),
'#required' => TRUE,
];
$request = $this->requestStack
->getCurrentRequest();
$query_key = $request->query
->get('key');
$form['key_id'] = [
'#title' => $this
->t('Key'),
'#type' => 'key_select',
'#default_value' => $query_key,
'#required' => TRUE,
];
$form['clear_overridden'] = [
'#title' => $this
->t('Clear overridden value'),
'#type' => 'checkbox',
'#description' => $this
->t('Check this field to clear any existing value for the overridden configuration item. This is important to make sure potentially sensitive data is removed from the configuration.'),
'#default_value' => TRUE,
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($form_state
->isSubmitted()) {
$definitions = $this
->getConfigEntityTypeDefinitions();
$config_type = $form_state
->getValue('config_type');
if (array_key_exists($config_type, $definitions)) {
$config_prefix = $definitions[$config_type]
->getConfigPrefix();
}
else {
$config_prefix = '';
}
$form_state
->setValue('config_prefix', $config_prefix);
}
}
public function save(array $form, FormStateInterface $form_state) {
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
$saved = parent::save($form, $form_state);
if ($saved && $form_state
->getValue('clear_overridden')) {
$override = $this->entity;
$type = $override
->getConfigType();
$name = $override
->getConfigName();
$item = $override
->getConfigItem();
if ($type !== 'system.simple') {
$definition = $this->entityTypeManager
->getDefinition($type);
$name = $definition
->getConfigPrefix() . '.' . $name;
}
$config = $this
->configFactory()
->getEditable($name);
$config
->set($item, NULL);
$config
->save();
}
return $saved;
}
public function changeConfigObject($form, FormStateInterface $form_state) {
return $form['config_object'];
}
protected function getConfigEntityTypeDefinitions($with_excluded = FALSE) {
if (!isset($this->configEntityTypeDefinitions)) {
$config_entity_type_definitions = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type => $definition) {
if ($definition
->entityClassImplements(ConfigEntityInterface::class)) {
$config_entity_type_definitions[$entity_type] = $definition;
}
}
$this->configEntityTypeDefinitions = $config_entity_type_definitions;
}
if ($with_excluded) {
$definitions = $this->configEntityTypeDefinitions;
}
else {
$definitions = array_diff_key($this->configEntityTypeDefinitions, $this
->excludedConfigTypes());
}
return $definitions;
}
protected function getConfigNames($config_type = NULL) {
$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('@label (@id)', [
'@label' => $label,
'@id' => $entity_id,
]);
}
else {
$names[$entity_id] = $entity_id;
}
}
}
elseif ($config_type == 'system.simple') {
$config_prefixes = array_map(function (EntityTypeInterface $definition) {
return $definition
->getConfigPrefix() . '.';
}, $this
->getConfigEntityTypeDefinitions(TRUE));
$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;
}
protected function getConfigItems($config_type = NULL, $config_name = NULL) {
$config_items = [];
if (!$config_name) {
return $config_items;
}
if ($config_type == 'system.simple') {
$name = $config_name;
}
else {
$definition = $this
->getConfigEntityTypeDefinitions()[$config_type];
$name = $definition
->getConfigPrefix() . '.' . $config_name;
}
$config_object = $this->configFactory
->get($name);
$config_array = $config_object
->get();
$config_items += $this
->flattenConfigItemList($config_array);
$config_items = array_combine($config_items, $config_items);
return $config_items;
}
protected function excludedConfigTypes() {
$exclude = [
'key',
'key_config_override',
];
return array_combine($exclude, $exclude);
}
protected function flattenConfigItemList(array $config_array, $prefix = '', $level = 0) {
$config_items = [];
$ignore = [
'uuid',
'_core',
];
foreach ($config_array as $key => $value) {
if (in_array($key, $ignore) && $level == 0) {
continue;
}
if (is_array($value) && $level < 5) {
$config_items = array_merge($config_items, $this
->flattenConfigItemList($value, $prefix . $key . '.', $level + 1));
}
else {
$config_items[] = $prefix . $key;
}
}
return $config_items;
}
}