public FieldStorageAddForm::buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL)
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 FormInterface::buildForm
File
- core/modules/field_ui/src/Form/FieldStorageAddForm.php, line 104
Class
- FieldStorageAddForm
- Provides a form for the "field storage" add page.
Namespace
Drupal\field_ui\Form
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 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 = array (); 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' ] = array ( '#type' => 'container' , '#attributes' => array ( 'class' => array ( 'form--inline' , 'clearfix' )), ); $form [ 'add' ][ 'new_storage_type' ] = array ( '#type' => 'select' , '#title' => $this ->t( 'Add a new field' ), '#options' => $field_type_options , '#empty_option' => $this ->t( '- Select a field type -' ), ); // Re-use existing field. if ( $existing_field_storage_options = $this ->getExistingFieldStorageOptions()) { $form [ 'add' ][ 'separator' ] = array ( '#type' => 'item' , '#markup' => $this ->t( 'or' ), ); $form [ 'add' ][ 'existing_storage_name' ] = array ( '#type' => 'select' , '#title' => $this ->t( 'Re-use an existing field' ), '#options' => $existing_field_storage_options , '#empty_option' => $this ->t( '- Select an existing field -' ), ); $form [ '#attached' ][ 'drupalSettings' ][ 'existingFieldLabels' ] = $this ->getExistingFieldLabels( array_keys ( $existing_field_storage_options )); } else { // Provide a placeholder form element to simplify the validation code. $form [ 'add' ][ 'existing_storage_name' ] = array ( '#type' => 'value' , '#value' => FALSE, ); } // Field label and field_name. $form [ 'new_storage_wrapper' ] = array ( '#type' => 'container' , '#states' => array ( '!visible' => array ( ':input[name="new_storage_type"]' => array ( 'value' => '' ), ), ), ); $form [ 'new_storage_wrapper' ][ 'label' ] = array ( '#type' => 'textfield' , '#title' => $this ->t( 'Label' ), '#size' => 15, ); $field_prefix = $this ->config( 'field_ui.settings' )->get( 'field_prefix' ); $form [ 'new_storage_wrapper' ][ 'field_name' ] = array ( '#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' => array ( 'source' => array ( 'new_storage_wrapper' , 'label' ), 'exists' => array ( $this , 'fieldNameExists' ), ), '#required' => FALSE, ); // Provide a separate label element for the "Re-use existing field" case // and place it outside the $form['add'] wrapper because those elements // are displayed inline. if ( $existing_field_storage_options ) { $form [ 'existing_storage_label' ] = array ( '#type' => 'textfield' , '#title' => $this ->t( 'Label' ), '#size' => 15, '#states' => array ( '!visible' => array ( ':input[name="existing_storage_name"]' => array ( 'value' => '' ), ), ), ); } // 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' ] = array ( '#type' => 'value' , '#value' => TRUE, ); $form [ 'actions' ] = array ( '#type' => 'actions' ); $form [ 'actions' ][ 'submit' ] = array ( '#type' => 'submit' , '#value' => $this ->t( 'Save and continue' ), '#button_type' => 'primary' , ); $form [ '#attached' ][ 'library' ][] = 'field_ui/drupal.field_ui' ; return $form ; } |
Please login to continue.