/**
* Provides a form for the "field storage" add page.
+ *
+ * @internal
*/
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'])) {
}
$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.
$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.
* 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();
* 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();