class ConfigSingleExportForm in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/config/src/Form/ConfigSingleExportForm.php \Drupal\config\Form\ConfigSingleExportForm
Provides a form for exporting a single configuration file.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\config\Form\ConfigSingleExportForm
Expanded class hierarchy of ConfigSingleExportForm
1 string reference to 'ConfigSingleExportForm'
- config.routing.yml in core/
modules/ config/ config.routing.yml - core/modules/config/config.routing.yml
File
- core/
modules/ config/ src/ Form/ ConfigSingleExportForm.php, line 22 - Contains \Drupal\config\Form\ConfigSingleExportForm.
Namespace
Drupal\config\FormView source
class ConfigSingleExportForm extends FormBase {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The config storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $configStorage;
/**
* Tracks the valid config entity type definitions.
*
* @var \Drupal\Core\Entity\EntityTypeInterface[]
*/
protected $definitions = array();
/**
* Constructs a new ConfigSingleImportForm.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Config\StorageInterface $config_storage
* The config storage.
*/
public function __construct(EntityManagerInterface $entity_manager, StorageInterface $config_storage) {
$this->entityManager = $entity_manager;
$this->configStorage = $config_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('config.storage'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'config_single_export_form';
}
/**
* {@inheritdoc}
*/
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);
// Sort the entity types by label, then add the simple config to the top.
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;
}
/**
* Handles switching the configuration type selector.
*/
public function updateConfigurationType($form, FormStateInterface $form_state) {
$form['config_name']['#options'] = $this
->findConfiguration($form_state
->getValue('config_type'));
return $form['config_name'];
}
/**
* Handles switching the export textarea.
*/
public function updateExport($form, FormStateInterface $form_state) {
// Determine the full config name for the selected config entity.
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');
}
// Read the raw data for this config name, encode it, and display it.
$form['export']['#value'] = Yaml::encode($this->configStorage
->read($name));
$form['export']['#description'] = $this
->t('Filename: %name', array(
'%name' => $name . '.yml',
));
return $form['export'];
}
/**
* Handles switching the configuration type selector.
*/
protected function findConfiguration($config_type) {
$names = array(
'' => $this
->t('- Select -'),
);
// For a given entity type, load all entities.
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 {
// Gather the config entity prefixes.
$config_prefixes = array_map(function (EntityTypeInterface $definition) {
return $definition
->getConfigPrefix() . '.';
}, $this->definitions);
// Find all config, and then filter our anything matching a config prefix.
$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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Nothing to submit.
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigSingleExportForm:: |
protected | property | The config storage. | |
ConfigSingleExportForm:: |
protected | property | Tracks the valid config entity type definitions. | |
ConfigSingleExportForm:: |
protected | property | The entity manager. | |
ConfigSingleExportForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ConfigSingleExportForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ConfigSingleExportForm:: |
protected | function | Handles switching the configuration type selector. | |
ConfigSingleExportForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ConfigSingleExportForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ConfigSingleExportForm:: |
public | function | Handles switching the configuration type selector. | |
ConfigSingleExportForm:: |
public | function | Handles switching the export textarea. | |
ConfigSingleExportForm:: |
public | function | Constructs a new ConfigSingleImportForm. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The logger factory. | |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
64 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Returns a redirect response object for the specified route. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |