Version 1
[yaffs-website] / web / core / modules / field_ui / src / Form / FieldStorageAddForm.php
1 <?php
2
3 namespace Drupal\field_ui\Form;
4
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;
14
15 /**
16  * Provides a form for the "field storage" add page.
17  */
18 class FieldStorageAddForm extends FormBase {
19
20   /**
21    * The name of the entity type.
22    *
23    * @var string
24    */
25   protected $entityTypeId;
26
27   /**
28    * The entity bundle.
29    *
30    * @var string
31    */
32   protected $bundle;
33
34   /**
35    * The entity manager.
36    *
37    * @var \Drupal\Core\Entity\EntityManager
38    */
39   protected $entityManager;
40
41   /**
42    * The field type plugin manager.
43    *
44    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
45    */
46   protected $fieldTypePluginManager;
47
48   /**
49    * The configuration factory.
50    *
51    * @var \Drupal\Core\Config\ConfigFactoryInterface
52    */
53   protected $configFactory;
54
55   /**
56    * Constructs a new FieldStorageAddForm object.
57    *
58    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
59    *   The 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.
64    */
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;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getFormId() {
75     return 'field_ui_field_storage_add_form';
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public static function create(ContainerInterface $container) {
82     return new static(
83       $container->get('entity.manager'),
84       $container->get('plugin.manager.field.field_type'),
85       $container->get('config.factory')
86     );
87   }
88
89   /**
90    * {@inheritdoc}
91    */
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);
95     }
96     if (!$form_state->get('bundle')) {
97       $form_state->set('bundle', $bundle);
98     }
99
100     $this->entityTypeId = $form_state->get('entity_type_id');
101     $this->bundle = $form_state->get('bundle');
102
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'];
108       }
109     }
110
111     $form['add'] = [
112       '#type' => 'container',
113       '#attributes' => ['class' => ['form--inline', 'clearfix']],
114     ];
115
116     $form['add']['new_storage_type'] = [
117       '#type' => 'select',
118       '#title' => $this->t('Add a new field'),
119       '#options' => $field_type_options,
120       '#empty_option' => $this->t('- Select a field type -'),
121     ];
122
123     // Re-use existing field.
124     if ($existing_field_storage_options = $this->getExistingFieldStorageOptions()) {
125       $form['add']['separator'] = [
126         '#type' => 'item',
127         '#markup' => $this->t('or'),
128       ];
129       $form['add']['existing_storage_name'] = [
130         '#type' => 'select',
131         '#title' => $this->t('Re-use an existing field'),
132         '#options' => $existing_field_storage_options,
133         '#empty_option' => $this->t('- Select an existing field -'),
134       ];
135
136       $form['#attached']['drupalSettings']['existingFieldLabels'] = $this->getExistingFieldLabels(array_keys($existing_field_storage_options));
137     }
138     else {
139       // Provide a placeholder form element to simplify the validation code.
140       $form['add']['existing_storage_name'] = [
141         '#type' => 'value',
142         '#value' => FALSE,
143       ];
144     }
145
146     // Field label and field_name.
147     $form['new_storage_wrapper'] = [
148       '#type' => 'container',
149       '#states' => [
150         '!visible' => [
151           ':input[name="new_storage_type"]' => ['value' => ''],
152         ],
153       ],
154     ];
155     $form['new_storage_wrapper']['label'] = [
156       '#type' => 'textfield',
157       '#title' => $this->t('Label'),
158       '#size' => 15,
159     ];
160
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>&lrm;',
167       '#size' => 15,
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),
172       '#machine_name' => [
173         'source' => ['new_storage_wrapper', 'label'],
174         'exists' => [$this, 'fieldNameExists'],
175       ],
176       '#required' => FALSE,
177     ];
178
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'),
186         '#size' => 15,
187         '#states' => [
188           '!visible' => [
189             ':input[name="existing_storage_name"]' => ['value' => ''],
190           ],
191         ],
192       ];
193     }
194
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'] = [
200       '#type' => 'value',
201       '#value' => TRUE,
202     ];
203
204     $form['actions'] = ['#type' => 'actions'];
205     $form['actions']['submit'] = [
206       '#type' => 'submit',
207       '#value' => $this->t('Save and continue'),
208       '#button_type' => 'primary',
209     ];
210
211     $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
212
213     return $form;
214   }
215
216   /**
217    * {@inheritdoc}
218    */
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.'));
223     }
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.'));
228       return;
229     }
230
231     $this->validateAddNew($form, $form_state);
232     $this->validateAddExisting($form, $form_state);
233   }
234
235   /**
236    * Validates the 'add new field' case.
237    *
238    * @param array $form
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.
242    *
243    * @see \Drupal\field_ui\Form\FieldStorageAddForm::validateForm()
244    */
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')) {
248       // Missing label.
249       if (!$form_state->getValue('label')) {
250         $form_state->setErrorByName('label', $this->t('Add new field: you need to provide a label.'));
251       }
252
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.'));
256       }
257       // Field name validation.
258       else {
259         $field_name = $form_state->getValue('field_name');
260
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);
264       }
265     }
266   }
267
268   /**
269    * Validates the 're-use existing field' case.
270    *
271    * @param array $form
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.
275    *
276    * @see \Drupal\field_ui\Form\FieldStorageAddForm::validateForm()
277    */
278   protected function validateAddExisting(array $form, FormStateInterface $form_state) {
279     if ($form_state->getValue('existing_storage_name')) {
280       // Missing label.
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.'));
283       }
284     }
285   }
286
287   /**
288    * {@inheritdoc}
289    */
290   public function submitForm(array &$form, FormStateInterface $form_state) {
291     $error = FALSE;
292     $values = $form_state->getValues();
293     $destinations = [];
294     $entity_type = $this->entityManager->getDefinition($this->entityTypeId);
295
296     // Create new field.
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'],
303       ];
304       $field_values = [
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,
311       ];
312       $widget_id = $formatter_id = NULL;
313
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;
318
319         $field_type_class = $this->fieldTypePluginManager->getDefinition($field_type)['class'];
320         $field_options = $field_type_class::getPreconfiguredOptions()[$option_key];
321
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];
327             }
328           }
329         }
330
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];
336             }
337           }
338         }
339
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;
342       }
343
344       // Create the field storage and field.
345       try {
346         $this->entityManager->getStorage('field_storage_config')->create($field_storage_values)->save();
347         $field = $this->entityManager->getStorage('field_config')->create($field_values);
348         $field->save();
349
350         $this->configureEntityFormDisplay($values['field_name'], $widget_id);
351         $this->configureEntityViewDisplay($values['field_name'], $formatter_id);
352
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];
361
362         // Store new field information for any additional submit handlers.
363         $form_state->set(['fields_added', '_add_new_field'], $values['field_name']);
364       }
365       catch (\Exception $e) {
366         $error = TRUE;
367         drupal_set_message($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()]), 'error');
368       }
369     }
370
371     // Re-use existing field.
372     if ($values['existing_storage_name']) {
373       $field_name = $values['existing_storage_name'];
374
375       try {
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'],
381         ]);
382         $field->save();
383
384         $this->configureEntityFormDisplay($field_name);
385         $this->configureEntityViewDisplay($field_name);
386
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];
392
393         // Store new field information for any additional submit handlers.
394         $form_state->set(['fields_added', '_add_existing_field'], $field_name);
395       }
396       catch (\Exception $e) {
397         $error = TRUE;
398         drupal_set_message($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()]), 'error');
399       }
400     }
401
402     if ($destinations) {
403       $destination = $this->getDestinationArray();
404       $destinations[] = $destination['destination'];
405       $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state));
406     }
407     elseif (!$error) {
408       drupal_set_message($this->t('Your settings have been saved.'));
409     }
410   }
411
412   /**
413    * Configures the field for the default form mode.
414    *
415    * @param string $field_name
416    *   The field name.
417    * @param string|null $widget_id
418    *   (optional) The plugin ID of the widget. Defaults to NULL.
419    */
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)
427       ->save();
428   }
429
430   /**
431    * Configures the field for the default view mode.
432    *
433    * @param string $field_name
434    *   The field name.
435    * @param string|null $formatter_id
436    *   (optional) The plugin ID of the formatter. Defaults to NULL.
437    */
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)
445       ->save();
446   }
447
448   /**
449    * Returns an array of existing field storages that can be added to a bundle.
450    *
451    * @return array
452    *   An array of existing field storages keyed by name.
453    */
454   protected function getExistingFieldStorageOptions() {
455     $options = [];
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) {
459       // Do not show:
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,
472         ]);
473       }
474     }
475     asort($options);
476
477     return $options;
478   }
479
480   /**
481    * Gets the human-readable labels for the given field storage names.
482    *
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).
486    *
487    * @param array $field_names
488    *   An array of field names.
489    *
490    * @return array
491    *   An array of field labels keyed by field name.
492    */
493   protected function getExistingFieldLabels(array $field_names) {
494     // Get all the fields corresponding to the given field storage names and
495     // this entity type.
496     $field_ids = $this->entityManager->getStorage('field_config')->getQuery()
497       ->condition('entity_type', $this->entityTypeId)
498       ->condition('field_name', $field_names)
499       ->execute();
500     $fields = $this->entityManager->getStorage('field_config')->loadMultiple($field_ids);
501
502     // Go through all the fields and use the label of the first encounter.
503     $labels = [];
504     foreach ($fields as $field) {
505       if (!isset($labels[$field->getName()])) {
506         $labels[$field->getName()] = $field->label();
507       }
508     }
509
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);
513
514     return $labels;
515   }
516
517   /**
518    * Checks if a field machine name is taken.
519    *
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.
526    *
527    * @return bool
528    *   Whether or not the field machine name is taken.
529    */
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')) {
533       return FALSE;
534     }
535
536     // Add the field prefix.
537     $field_name = $this->configFactory->get('field_ui.settings')->get('field_prefix') . $value;
538
539     $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityTypeId);
540     return isset($field_storage_definitions[$field_name]);
541   }
542
543 }