3 namespace Drupal\field_ui\Form;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Field\FieldTypePluginManagerInterface;
8 use Drupal\Core\Form\FormBase;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\field\Entity\FieldStorageConfig;
11 use Drupal\field\FieldStorageConfigInterface;
12 use Drupal\field_ui\FieldUI;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides a form for the "field storage" add page.
18 class FieldStorageAddForm extends FormBase {
21 * The name of the entity type.
25 protected $entityTypeId;
37 * @var \Drupal\Core\Entity\EntityManager
39 protected $entityManager;
42 * The field type plugin manager.
44 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
46 protected $fieldTypePluginManager;
49 * The configuration factory.
51 * @var \Drupal\Core\Config\ConfigFactoryInterface
53 protected $configFactory;
56 * Constructs a new FieldStorageAddForm object.
58 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
60 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_plugin_manager
61 * The field type plugin manager.
62 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
63 * The configuration factory.
65 public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_plugin_manager, ConfigFactoryInterface $config_factory) {
66 $this->entityManager = $entity_manager;
67 $this->fieldTypePluginManager = $field_type_plugin_manager;
68 $this->configFactory = $config_factory;
74 public function getFormId() {
75 return 'field_ui_field_storage_add_form';
81 public static function create(ContainerInterface $container) {
83 $container->get('entity.manager'),
84 $container->get('plugin.manager.field.field_type'),
85 $container->get('config.factory')
92 public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
93 if (!$form_state->get('entity_type_id')) {
94 $form_state->set('entity_type_id', $entity_type_id);
96 if (!$form_state->get('bundle')) {
97 $form_state->set('bundle', $bundle);
100 $this->entityTypeId = $form_state->get('entity_type_id');
101 $this->bundle = $form_state->get('bundle');
103 // Gather valid field types.
104 $field_type_options = [];
105 foreach ($this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions()) as $category => $field_types) {
106 foreach ($field_types as $name => $field_type) {
107 $field_type_options[$category][$name] = $field_type['label'];
112 '#type' => 'container',
113 '#attributes' => ['class' => ['form--inline', 'clearfix']],
116 $form['add']['new_storage_type'] = [
118 '#title' => $this->t('Add a new field'),
119 '#options' => $field_type_options,
120 '#empty_option' => $this->t('- Select a field type -'),
123 // Re-use existing field.
124 if ($existing_field_storage_options = $this->getExistingFieldStorageOptions()) {
125 $form['add']['separator'] = [
127 '#markup' => $this->t('or'),
129 $form['add']['existing_storage_name'] = [
131 '#title' => $this->t('Re-use an existing field'),
132 '#options' => $existing_field_storage_options,
133 '#empty_option' => $this->t('- Select an existing field -'),
136 $form['#attached']['drupalSettings']['existingFieldLabels'] = $this->getExistingFieldLabels(array_keys($existing_field_storage_options));
139 // Provide a placeholder form element to simplify the validation code.
140 $form['add']['existing_storage_name'] = [
146 // Field label and field_name.
147 $form['new_storage_wrapper'] = [
148 '#type' => 'container',
151 ':input[name="new_storage_type"]' => ['value' => ''],
155 $form['new_storage_wrapper']['label'] = [
156 '#type' => 'textfield',
157 '#title' => $this->t('Label'),
161 $field_prefix = $this->config('field_ui.settings')->get('field_prefix');
162 $form['new_storage_wrapper']['field_name'] = [
163 '#type' => 'machine_name',
164 // This field should stay LTR even for RTL languages.
165 '#field_prefix' => '<span dir="ltr">' . $field_prefix,
166 '#field_suffix' => '</span>‎',
168 '#description' => $this->t('A unique machine-readable name containing letters, numbers, and underscores.'),
169 // Calculate characters depending on the length of the field prefix
170 // setting. Maximum length is 32.
171 '#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix),
173 'source' => ['new_storage_wrapper', 'label'],
174 'exists' => [$this, 'fieldNameExists'],
176 '#required' => FALSE,
179 // Provide a separate label element for the "Re-use existing field" case
180 // and place it outside the $form['add'] wrapper because those elements
181 // are displayed inline.
182 if ($existing_field_storage_options) {
183 $form['existing_storage_label'] = [
184 '#type' => 'textfield',
185 '#title' => $this->t('Label'),
189 ':input[name="existing_storage_name"]' => ['value' => ''],
195 // Place the 'translatable' property as an explicit value so that contrib
196 // modules can form_alter() the value for newly created fields. By default
197 // we create field storage as translatable so it will be possible to enable
198 // translation at field level.
199 $form['translatable'] = [
204 $form['actions'] = ['#type' => 'actions'];
205 $form['actions']['submit'] = [
207 '#value' => $this->t('Save and continue'),
208 '#button_type' => 'primary',
211 $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
219 public function validateForm(array &$form, FormStateInterface $form_state) {
220 // Missing field type.
221 if (!$form_state->getValue('new_storage_type') && !$form_state->getValue('existing_storage_name')) {
222 $form_state->setErrorByName('new_storage_type', $this->t('You need to select a field type or an existing field.'));
224 // Both field type and existing field option selected. This is prevented in
225 // the UI with JavaScript but we also need a proper server-side validation.
226 elseif ($form_state->getValue('new_storage_type') && $form_state->getValue('existing_storage_name')) {
227 $form_state->setErrorByName('new_storage_type', $this->t('Adding a new field and re-using an existing field at the same time is not allowed.'));
231 $this->validateAddNew($form, $form_state);
232 $this->validateAddExisting($form, $form_state);
236 * Validates the 'add new field' case.
239 * An associative array containing the structure of the form.
240 * @param \Drupal\Core\Form\FormStateInterface $form_state
241 * The current state of the form.
243 * @see \Drupal\field_ui\Form\FieldStorageAddForm::validateForm()
245 protected function validateAddNew(array $form, FormStateInterface $form_state) {
246 // Validate if any information was provided in the 'add new field' case.
247 if ($form_state->getValue('new_storage_type')) {
249 if (!$form_state->getValue('label')) {
250 $form_state->setErrorByName('label', $this->t('Add new field: you need to provide a label.'));
253 // Missing field name.
254 if (!$form_state->getValue('field_name')) {
255 $form_state->setErrorByName('field_name', $this->t('Add new field: you need to provide a machine name for the field.'));
257 // Field name validation.
259 $field_name = $form_state->getValue('field_name');
261 // Add the field prefix.
262 $field_name = $this->configFactory->get('field_ui.settings')->get('field_prefix') . $field_name;
263 $form_state->setValueForElement($form['new_storage_wrapper']['field_name'], $field_name);
269 * Validates the 're-use existing field' case.
272 * An associative array containing the structure of the form.
273 * @param \Drupal\Core\Form\FormStateInterface $form_state
274 * The current state of the form.
276 * @see \Drupal\field_ui\Form\FieldStorageAddForm::validateForm()
278 protected function validateAddExisting(array $form, FormStateInterface $form_state) {
279 if ($form_state->getValue('existing_storage_name')) {
281 if (!$form_state->getValue('existing_storage_label')) {
282 $form_state->setErrorByName('existing_storage_label', $this->t('Re-use existing field: you need to provide a label.'));
290 public function submitForm(array &$form, FormStateInterface $form_state) {
292 $values = $form_state->getValues();
294 $entity_type = $this->entityManager->getDefinition($this->entityTypeId);
297 if ($values['new_storage_type']) {
298 $field_storage_values = [
299 'field_name' => $values['field_name'],
300 'entity_type' => $this->entityTypeId,
301 'type' => $values['new_storage_type'],
302 'translatable' => $values['translatable'],
305 'field_name' => $values['field_name'],
306 'entity_type' => $this->entityTypeId,
307 'bundle' => $this->bundle,
308 'label' => $values['label'],
309 // Field translatability should be explicitly enabled by the users.
310 'translatable' => FALSE,
312 $widget_id = $formatter_id = NULL;
314 // Check if we're dealing with a preconfigured field.
315 if (strpos($field_storage_values['type'], 'field_ui:') !== FALSE) {
316 list(, $field_type, $option_key) = explode(':', $field_storage_values['type'], 3);
317 $field_storage_values['type'] = $field_type;
319 $field_type_class = $this->fieldTypePluginManager->getDefinition($field_type)['class'];
320 $field_options = $field_type_class::getPreconfiguredOptions()[$option_key];
322 // Merge in preconfigured field storage options.
323 if (isset($field_options['field_storage_config'])) {
324 foreach (['cardinality', 'settings'] as $key) {
325 if (isset($field_options['field_storage_config'][$key])) {
326 $field_storage_values[$key] = $field_options['field_storage_config'][$key];
331 // Merge in preconfigured field options.
332 if (isset($field_options['field_config'])) {
333 foreach (['required', 'settings'] as $key) {
334 if (isset($field_options['field_config'][$key])) {
335 $field_values[$key] = $field_options['field_config'][$key];
340 $widget_id = isset($field_options['entity_form_display']['type']) ? $field_options['entity_form_display']['type'] : NULL;
341 $formatter_id = isset($field_options['entity_view_display']['type']) ? $field_options['entity_view_display']['type'] : NULL;
344 // Create the field storage and field.
346 $this->entityManager->getStorage('field_storage_config')->create($field_storage_values)->save();
347 $field = $this->entityManager->getStorage('field_config')->create($field_values);
350 $this->configureEntityFormDisplay($values['field_name'], $widget_id);
351 $this->configureEntityViewDisplay($values['field_name'], $formatter_id);
353 // Always show the field settings step, as the cardinality needs to be
354 // configured for new fields.
355 $route_parameters = [
356 'field_config' => $field->id(),
357 ] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
358 $destinations[] = ['route_name' => "entity.field_config.{$this->entityTypeId}_storage_edit_form", 'route_parameters' => $route_parameters];
359 $destinations[] = ['route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form", 'route_parameters' => $route_parameters];
360 $destinations[] = ['route_name' => "entity.{$this->entityTypeId}.field_ui_fields", 'route_parameters' => $route_parameters];
362 // Store new field information for any additional submit handlers.
363 $form_state->set(['fields_added', '_add_new_field'], $values['field_name']);
365 catch (\Exception $e) {
367 drupal_set_message($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()]), 'error');
371 // Re-use existing field.
372 if ($values['existing_storage_name']) {
373 $field_name = $values['existing_storage_name'];
376 $field = $this->entityManager->getStorage('field_config')->create([
377 'field_name' => $field_name,
378 'entity_type' => $this->entityTypeId,
379 'bundle' => $this->bundle,
380 'label' => $values['existing_storage_label'],
384 $this->configureEntityFormDisplay($field_name);
385 $this->configureEntityViewDisplay($field_name);
387 $route_parameters = [
388 'field_config' => $field->id(),
389 ] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
390 $destinations[] = ['route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form", 'route_parameters' => $route_parameters];
391 $destinations[] = ['route_name' => "entity.{$this->entityTypeId}.field_ui_fields", 'route_parameters' => $route_parameters];
393 // Store new field information for any additional submit handlers.
394 $form_state->set(['fields_added', '_add_existing_field'], $field_name);
396 catch (\Exception $e) {
398 drupal_set_message($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()]), 'error');
403 $destination = $this->getDestinationArray();
404 $destinations[] = $destination['destination'];
405 $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state));
408 drupal_set_message($this->t('Your settings have been saved.'));
413 * Configures the field for the default form mode.
415 * @param string $field_name
417 * @param string|null $widget_id
418 * (optional) The plugin ID of the widget. Defaults to NULL.
420 protected function configureEntityFormDisplay($field_name, $widget_id = NULL) {
421 // Make sure the field is displayed in the 'default' form mode (using
422 // default widget and settings). It stays hidden for other form modes
423 // until it is explicitly configured.
424 $options = $widget_id ? ['type' => $widget_id] : [];
425 entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
426 ->setComponent($field_name, $options)
431 * Configures the field for the default view mode.
433 * @param string $field_name
435 * @param string|null $formatter_id
436 * (optional) The plugin ID of the formatter. Defaults to NULL.
438 protected function configureEntityViewDisplay($field_name, $formatter_id = NULL) {
439 // Make sure the field is displayed in the 'default' view mode (using
440 // default formatter and settings). It stays hidden for other view
441 // modes until it is explicitly configured.
442 $options = $formatter_id ? ['type' => $formatter_id] : [];
443 entity_get_display($this->entityTypeId, $this->bundle, 'default')
444 ->setComponent($field_name, $options)
449 * Returns an array of existing field storages that can be added to a bundle.
452 * An array of existing field storages keyed by name.
454 protected function getExistingFieldStorageOptions() {
456 // Load the field_storages and build the list of options.
457 $field_types = $this->fieldTypePluginManager->getDefinitions();
458 foreach ($this->entityManager->getFieldStorageDefinitions($this->entityTypeId) as $field_name => $field_storage) {
460 // - non-configurable field storages,
461 // - locked field storages,
462 // - field storages that should not be added via user interface,
463 // - field storages that already have a field in the bundle.
464 $field_type = $field_storage->getType();
465 if ($field_storage instanceof FieldStorageConfigInterface
466 && !$field_storage->isLocked()
467 && empty($field_types[$field_type]['no_ui'])
468 && !in_array($this->bundle, $field_storage->getBundles(), TRUE)) {
469 $options[$field_name] = $this->t('@type: @field', [
470 '@type' => $field_types[$field_type]['label'],
471 '@field' => $field_name,
481 * Gets the human-readable labels for the given field storage names.
483 * Since not all field storages are required to have a field, we can only
484 * provide the field labels on a best-effort basis (e.g. the label of a field
485 * storage without any field attached to a bundle will be the field name).
487 * @param array $field_names
488 * An array of field names.
491 * An array of field labels keyed by field name.
493 protected function getExistingFieldLabels(array $field_names) {
494 // Get all the fields corresponding to the given field storage names and
496 $field_ids = $this->entityManager->getStorage('field_config')->getQuery()
497 ->condition('entity_type', $this->entityTypeId)
498 ->condition('field_name', $field_names)
500 $fields = $this->entityManager->getStorage('field_config')->loadMultiple($field_ids);
502 // Go through all the fields and use the label of the first encounter.
504 foreach ($fields as $field) {
505 if (!isset($labels[$field->getName()])) {
506 $labels[$field->getName()] = $field->label();
510 // For field storages without any fields attached to a bundle, the default
511 // label is the field name.
512 $labels += array_combine($field_names, $field_names);
518 * Checks if a field machine name is taken.
520 * @param string $value
521 * The machine name, not prefixed.
522 * @param array $element
523 * An array containing the structure of the 'field_name' element.
524 * @param \Drupal\Core\Form\FormStateInterface $form_state
525 * The current state of the form.
528 * Whether or not the field machine name is taken.
530 public function fieldNameExists($value, $element, FormStateInterface $form_state) {
531 // Don't validate the case when an existing field has been selected.
532 if ($form_state->getValue('existing_storage_name')) {
536 // Add the field prefix.
537 $field_name = $this->configFactory->get('field_ui.settings')->get('field_prefix') . $value;
539 $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityTypeId);
540 return isset($field_storage_definitions[$field_name]);