X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Ffield_ui%2Fsrc%2FForm%2FFieldStorageAddForm.php;h=5afd358a6782b62a70c403da3e5606c32ac04eae;hb=1c1cb0980bfa6caf0c24cce671b6bb541dc87583;hp=e1f278796e577dbdff111cc6cbbb375d03a68ce8;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/core/modules/field_ui/src/Form/FieldStorageAddForm.php b/web/core/modules/field_ui/src/Form/FieldStorageAddForm.php index e1f278796..5afd358a6 100644 --- a/web/core/modules/field_ui/src/Form/FieldStorageAddForm.php +++ b/web/core/modules/field_ui/src/Form/FieldStorageAddForm.php @@ -14,6 +14,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a form for the "field storage" add page. + * + * @internal */ class FieldStorageAddForm extends FormBase { @@ -310,14 +312,16 @@ class FieldStorageAddForm extends FormBase { 'translatable' => FALSE, ]; $widget_id = $formatter_id = NULL; + $widget_settings = $formatter_settings = []; // Check if we're dealing with a preconfigured field. if (strpos($field_storage_values['type'], 'field_ui:') !== FALSE) { list(, $field_type, $option_key) = explode(':', $field_storage_values['type'], 3); $field_storage_values['type'] = $field_type; - $field_type_class = $this->fieldTypePluginManager->getDefinition($field_type)['class']; - $field_options = $field_type_class::getPreconfiguredOptions()[$option_key]; + $field_definition = $this->fieldTypePluginManager->getDefinition($field_type); + $options = $this->fieldTypePluginManager->getPreconfiguredOptions($field_definition['id']); + $field_options = $options[$option_key]; // Merge in preconfigured field storage options. if (isset($field_options['field_storage_config'])) { @@ -338,7 +342,9 @@ class FieldStorageAddForm extends FormBase { } $widget_id = isset($field_options['entity_form_display']['type']) ? $field_options['entity_form_display']['type'] : NULL; + $widget_settings = isset($field_options['entity_form_display']['settings']) ? $field_options['entity_form_display']['settings'] : []; $formatter_id = isset($field_options['entity_view_display']['type']) ? $field_options['entity_view_display']['type'] : NULL; + $formatter_settings = isset($field_options['entity_view_display']['settings']) ? $field_options['entity_view_display']['settings'] : []; } // Create the field storage and field. @@ -347,8 +353,8 @@ class FieldStorageAddForm extends FormBase { $field = $this->entityManager->getStorage('field_config')->create($field_values); $field->save(); - $this->configureEntityFormDisplay($values['field_name'], $widget_id); - $this->configureEntityViewDisplay($values['field_name'], $formatter_id); + $this->configureEntityFormDisplay($values['field_name'], $widget_id, $widget_settings); + $this->configureEntityViewDisplay($values['field_name'], $formatter_id, $formatter_settings); // Always show the field settings step, as the cardinality needs to be // configured for new fields. @@ -364,7 +370,7 @@ class FieldStorageAddForm extends FormBase { } catch (\Exception $e) { $error = TRUE; - drupal_set_message($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()]), 'error'); + $this->messenger()->addError($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()])); } } @@ -395,7 +401,7 @@ class FieldStorageAddForm extends FormBase { } catch (\Exception $e) { $error = TRUE; - drupal_set_message($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()]), 'error'); + $this->messenger()->addError($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()])); } } @@ -405,7 +411,7 @@ class FieldStorageAddForm extends FormBase { $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state)); } elseif (!$error) { - drupal_set_message($this->t('Your settings have been saved.')); + $this->messenger()->addStatus($this->t('Your settings have been saved.')); } } @@ -416,12 +422,20 @@ class FieldStorageAddForm extends FormBase { * The field name. * @param string|null $widget_id * (optional) The plugin ID of the widget. Defaults to NULL. + * @param array $widget_settings + * (optional) An array of widget settings. Defaults to an empty array. */ - protected function configureEntityFormDisplay($field_name, $widget_id = NULL) { + protected function configureEntityFormDisplay($field_name, $widget_id = NULL, array $widget_settings = []) { + $options = []; + if ($widget_id) { + $options['type'] = $widget_id; + if (!empty($widget_settings)) { + $options['settings'] = $widget_settings; + } + } // Make sure the field is displayed in the 'default' form mode (using // default widget and settings). It stays hidden for other form modes // until it is explicitly configured. - $options = $widget_id ? ['type' => $widget_id] : []; entity_get_form_display($this->entityTypeId, $this->bundle, 'default') ->setComponent($field_name, $options) ->save(); @@ -434,12 +448,20 @@ class FieldStorageAddForm extends FormBase { * The field name. * @param string|null $formatter_id * (optional) The plugin ID of the formatter. Defaults to NULL. + * @param array $formatter_settings + * (optional) An array of formatter settings. Defaults to an empty array. */ - protected function configureEntityViewDisplay($field_name, $formatter_id = NULL) { + protected function configureEntityViewDisplay($field_name, $formatter_id = NULL, array $formatter_settings = []) { + $options = []; + if ($formatter_id) { + $options['type'] = $formatter_id; + if (!empty($formatter_settings)) { + $options['settings'] = $formatter_settings; + } + } // Make sure the field is displayed in the 'default' view mode (using // default formatter and settings). It stays hidden for other view // modes until it is explicitly configured. - $options = $formatter_id ? ['type' => $formatter_id] : []; entity_get_display($this->entityTypeId, $this->bundle, 'default') ->setComponent($field_name, $options) ->save();