View source
<?php
namespace Drupal\wsdata_field\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_ui\FieldUI;
use Drupal\field_ui\Form\FieldStorageAddForm;
class WSFieldAddFieldForm extends FieldStorageAddForm {
public function getFormId() {
return 'wsfield_field_add_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
if (!$form_state
->get('entity_type_id')) {
$form_state
->set('entity_type_id', $entity_type_id);
}
if (!$form_state
->get('bundle')) {
$form_state
->set('bundle', $bundle);
}
$this->entityTypeId = $form_state
->get('entity_type_id');
$this->bundle = $form_state
->get('bundle');
$field_type_options = [];
foreach ($this->fieldTypePluginManager
->getGroupedDefinitions($this->fieldTypePluginManager
->getUiDefinitions()) as $category => $field_types) {
foreach ($field_types as $name => $field_type) {
$field_type_options[$category][$name] = $field_type['label'];
}
}
$form['add'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'form--inline',
'clearfix',
],
],
];
$form['add']['new_storage_type'] = [
'#type' => 'select',
'#title' => $this
->t('Add a new field'),
'#options' => $field_type_options,
'#empty_option' => $this
->t('- Select a field type -'),
];
$form['new_storage_wrapper'] = [
'#type' => 'container',
'#states' => [
'!visible' => [
':input[name="new_storage_type"]' => [
'value' => '',
],
],
],
];
$form['new_storage_wrapper']['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#size' => 15,
];
$field_prefix = $this
->config('field_ui.settings')
->get('field_prefix');
$form['new_storage_wrapper']['field_name'] = [
'#type' => 'machine_name',
'#field_prefix' => '<span dir="ltr">' . $field_prefix,
'#field_suffix' => '</span>‎',
'#size' => 15,
'#description' => $this
->t('A unique machine-readable name containing letters, numbers, and underscores.'),
'#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix),
'#machine_name' => [
'source' => [
'new_storage_wrapper',
'label',
],
'exists' => [
$this,
'fieldNameExists',
],
],
'#required' => FALSE,
];
$form['translatable'] = [
'#type' => 'value',
'#value' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save and continue'),
'#button_type' => 'primary',
];
$form['#attached']['library'][] = 'field_ui/drupal.field_ui';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$error = FALSE;
$values = $form_state
->getValues();
$destinations = [];
$entity_type = $this->entityManager
->getDefinition($this->entityTypeId);
if ($values['new_storage_type']) {
$field_storage_values = [
'field_name' => $values['field_name'],
'entity_type' => $this->entityTypeId,
'type' => $values['new_storage_type'],
'translatable' => $values['translatable'],
'custom_storage' => TRUE,
];
$field_values = [
'field_name' => $values['field_name'],
'entity_type' => $this->entityTypeId,
'bundle' => $this->bundle,
'label' => $values['label'],
'translatable' => FALSE,
];
$widget_id = $formatter_id = NULL;
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];
if (isset($field_options['field_storage_config'])) {
foreach ([
'cardinality',
'settings',
] as $key) {
if (isset($field_options['field_storage_config'][$key])) {
$field_storage_values[$key] = $field_options['field_storage_config'][$key];
}
}
}
if (isset($field_options['field_config'])) {
foreach ([
'required',
'settings',
] as $key) {
if (isset($field_options['field_config'][$key])) {
$field_values[$key] = $field_options['field_config'][$key];
}
}
}
$widget_id = isset($field_options['entity_form_display']['type']) ? $field_options['entity_form_display']['type'] : NULL;
$formatter_id = isset($field_options['entity_view_display']['type']) ? $field_options['entity_view_display']['type'] : NULL;
}
try {
$this->entityManager
->getStorage('field_storage_config')
->create($field_storage_values)
->save();
$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);
$route_parameters = [
'field_config' => $field
->id(),
] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
$destinations[] = [
'route_name' => "entity.field_config.{$this->entityTypeId}_wsfield_edit_form",
'route_parameters' => $route_parameters,
];
$destinations[] = [
'route_name' => "entity.field_config.{$this->entityTypeId}_storage_edit_form",
'route_parameters' => $route_parameters,
];
$destinations[] = [
'route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form",
'route_parameters' => $route_parameters,
];
$destinations[] = [
'route_name' => "entity.{$this->entityTypeId}.field_ui_fields",
'route_parameters' => $route_parameters,
];
$form_state
->set([
'fields_added',
'_add_new_field',
], $values['field_name']);
} catch (\Exception $e) {
$error = TRUE;
$this
->messenger()
->addError($this
->t('There was a problem creating field %label: @message', [
'%label' => $values['label'],
'@message' => $e
->getMessage(),
]));
}
}
if ($destinations) {
$destination = $this
->getDestinationArray();
$destinations[] = $destination['destination'];
$form_state
->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state));
}
elseif (!$error) {
$this
->messenger()
->addStatus($this
->t('Your settings have been saved.'));
}
}
}