public WidgetBase::form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL)
Creates a form element for a field.
If the entity associated with the form is new (i.e., $entity->isNew() is TRUE), the 'default value', if any, is pre-populated. Also allows other modules to alter the form element by implementing their own hooks.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: An array of the field values. When creating a new entity this may be NULL or an empty array to use default values.
array $form: An array representing the form that the editing element will be attached to.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
int $get_delta: Used to get only a specific delta value of a multiple value field.
Return value
array The form element array created for this field.
Overrides WidgetBaseInterface::form
File
- core/lib/Drupal/Core/Field/WidgetBase.php, line 60
Class
- WidgetBase
- Base class for 'Field widget' plugin implementations.
Namespace
Drupal\Core\Field
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 | public function form(FieldItemListInterface $items , array & $form , FormStateInterface $form_state , $get_delta = NULL) { $field_name = $this ->fieldDefinition->getName(); $parents = $form [ '#parents' ]; // Store field information in $form_state. if (! static ::getWidgetState( $parents , $field_name , $form_state )) { $field_state = array ( 'items_count' => count ( $items ), 'array_parents' => array (), ); static ::setWidgetState( $parents , $field_name , $form_state , $field_state ); } // Collect widget elements. $elements = array (); // If the widget is handling multiple values (e.g Options), or if we are // displaying an individual element, just get a single form element and make // it the $delta value. if ( $this ->handlesMultipleValues() || isset( $get_delta )) { $delta = isset( $get_delta ) ? $get_delta : 0; $element = array ( '#title' => $this ->fieldDefinition->getLabel(), '#description' => FieldFilteredMarkup::create(\Drupal::token()->replace( $this ->fieldDefinition->getDescription())), ); $element = $this ->formSingleElement( $items , $delta , $element , $form , $form_state ); if ( $element ) { if (isset( $get_delta )) { // If we are processing a specific delta value for a field where the // field module handles multiples, set the delta in the result. $elements [ $delta ] = $element ; } else { // For fields that handle their own processing, we cannot make // assumptions about how the field is structured, just merge in the // returned element. $elements = $element ; } } } // If the widget does not handle multiple values itself, (and we are not // displaying an individual element), process the multiple value form. else { $elements = $this ->formMultipleElements( $items , $form , $form_state ); } // Populate the 'array_parents' information in $form_state->get('field') // after the form is built, so that we catch changes in the form structure // performed in alter() hooks. $elements [ '#after_build' ][] = array (get_class( $this ), 'afterBuild' ); $elements [ '#field_name' ] = $field_name ; $elements [ '#field_parents' ] = $parents ; // Enforce the structure of submitted values. $elements [ '#parents' ] = array_merge ( $parents , array ( $field_name )); // Most widgets need their internal structure preserved in submitted values. $elements += array ( '#tree' => TRUE); return array ( // Aid in theming of widgets by rendering a classified container. '#type' => 'container' , // Assign a different parent, to keep the main id for the widget itself. '#parents' => array_merge ( $parents , array ( $field_name . '_wrapper' )), '#attributes' => array ( 'class' => array ( 'field--type-' . Html::getClass( $this ->fieldDefinition-> getType ()), 'field--name-' . Html::getClass( $field_name ), 'field--widget-' . Html::getClass( $this ->getPluginId()), ), ), 'widget' => $elements , ); } |
Please login to continue.