public function WSFieldAddFieldForm::buildForm in Web Service Data 8
Same name and namespace in other branches
- 2.0.x modules/wsdata_field/src/Form/WSFieldAddFieldForm.php \Drupal\wsdata_field\Form\WSFieldAddFieldForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FieldStorageAddForm::buildForm
File
- modules/
wsdata_field/ src/ Form/ WSFieldAddFieldForm.php, line 25
Class
- WSFieldAddFieldForm
- Provides a form for the "field storage" add page.
Namespace
Drupal\wsdata_field\FormCode
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');
// Gather valid field types.
$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 -'),
];
// Field label and field_name.
$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',
// This field should stay LTR even for RTL languages.
'#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.'),
// Calculate characters depending on the length of the field prefix
// setting. Maximum length is 32.
'#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix),
'#machine_name' => [
'source' => [
'new_storage_wrapper',
'label',
],
'exists' => [
$this,
'fieldNameExists',
],
],
'#required' => FALSE,
];
// Place the 'translatable' property as an explicit value so that contrib
// modules can form_alter() the value for newly created fields. By default
// we create field storage as translatable so it will be possible to enable
// translation at field level.
$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;
}