Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\config\Form\ConfigSingleExportForm.
*/
namespace Drupal\config\Form;
use Drupal\Component\Utility\MapArray;
use Drupal\Core\Config\StorageInterface;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
Alex Pott
committed
use Drupal\Core\Entity\EntityTypeInterface;
Angie Byron
committed
use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for exporting a single configuration file.
*/
class ConfigSingleExportForm extends FormBase {
/**
* The entity manager.
*
Angie Byron
committed
* @var \Drupal\Core\Entity\EntityManagerInterface
Angie Byron
committed
*/
protected $entityManager;
/**
* The config storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $configStorage;
/**
* Tracks the valid config entity type definitions.
*
Alex Pott
committed
* @var \Drupal\Core\Entity\EntityTypeInterface[]
Angie Byron
committed
*/
protected $definitions = array();
/**
* Constructs a new ConfigSingleImportForm.
*
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Angie Byron
committed
* The entity manager.
* @param \Drupal\Core\Config\StorageInterface $config_storage
* The config storage.
*/
Angie Byron
committed
public function __construct(EntityManagerInterface $entity_manager, StorageInterface $config_storage) {
Angie Byron
committed
$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, array &$form_state, $config_type = NULL, $config_name = NULL) {
foreach ($this->entityManager->getDefinitions() as $entity_type => $definition) {
Alex Pott
committed
if ($definition->getConfigPrefix() && $definition->hasKey('uuid')) {
Angie Byron
committed
$this->definitions[$entity_type] = $definition;
}
}
Alex Pott
committed
$entity_types = array_map(function (EntityTypeInterface $definition) {
return $definition->getLabel();
Angie Byron
committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
}, $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' => array($this, 'updateConfigurationType'),
'wrapper' => 'edit-config-type-wrapper',
),
);
$default_type = isset($form_state['values']['config_type']) ? $form_state['values']['config_type'] : $config_type;
$form['config_name'] = array(
'#title' => $this->t('Configuration name'),
'#type' => 'select',
'#options' => $this->findConfiguration($default_type),
'#default_value' => $config_name,
'#required' => TRUE,
'#prefix' => '<div id="edit-config-type-wrapper">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => array($this, 'updateExport'),
'wrapper' => 'edit-export-wrapper',
),
);
$form['export'] = array(
'#title' => $this->t('Here is your configuration:'),
'#type' => 'textarea',
'#rows' => 24,
'#required' => TRUE,
'#prefix' => '<div id="edit-export-wrapper">',
'#suffix' => '</div>',
);
if ($config_type && $config_name) {
$fake_form_state = array('values' => array(
'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, &$form_state) {
$form['config_name']['#options'] = $this->findConfiguration($form_state['values']['config_type']);
return $form['config_name'];
}
/**
* Handles switching the export textarea.
*/
public function updateExport($form, &$form_state) {
// Determine the full config name for the selected config entity.
if ($form_state['values']['config_type'] !== 'system.simple') {
$definition = $this->entityManager->getDefinition($form_state['values']['config_type']);
Alex Pott
committed
$name = $definition->getConfigPrefix() . '.' . $form_state['values']['config_name'];
Angie Byron
committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
}
// The config name is used directly for simple configuration.
else {
$name = $form_state['values']['config_name'];
}
// Read the raw data for this config name, encode it, and display it.
$data = $this->configStorage->read($name);
$form['export']['#value'] = $this->configStorage->encode($data);
$form['export']['#description'] = $this->t('The filename is %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->getStorageController($config_type);
foreach ($entity_storage->loadMultiple() as $entity) {
$entity_id = $entity->id();
$label = $entity->label() ?: $entity_id;
$names[$entity_id] = $label;
}
}
// Handle simple configuration.
else {
// Gather the config entity prefixes.
Alex Pott
committed
$config_prefixes = array_map(function (EntityTypeInterface $definition) {
return $definition->getConfigPrefix() . '.';
Angie Byron
committed
}, $this->definitions);
// Find all config, and then filter our anything matching a config prefix.
$names = MapArray::copyValuesToKeys($this->configStorage->listAll());
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, array &$form_state) {
// Nothing to submit.
}
}