W3cubDocs

/Drupal 8

public function EntityFormDisplayInterface::buildForm

public EntityFormDisplayInterface::buildForm(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state)

Adds field widgets to an entity form.

The form elements for the entity's fields are added by reference as direct children in the $form parameter. This parameter can be a full form structure (most common case for entity edit forms), or a sub-element of a larger form.

By default, submitted field values appear at the top-level of $form_state->getValues(). A different location within $form_state->getValues() can be specified by setting the '#parents' property on the incoming $form parameter. Because of name clashes, two instances of the same field cannot appear within the same $form element, or within the same '#parents' space.

Sample resulting structure in $form:

  '#parents' => The location of field values in $form_state->getValues(),
  '#entity_type' => The name of the entity type,
  '#bundle' => The name of the bundle,
  // One sub-array per field appearing in the entity, keyed by field name.
  // The structure of the array differs slightly depending on whether the
  // widget is 'single-value' (provides the input for one field value,
  // most common case), and will therefore be repeated as many times as
  // needed, or 'multiple-values' (one single widget allows the input of
  // several values; e.g., checkboxes, select box, etc.).
  'field_foo' => array(
    '#access' => TRUE if the current user has 'edit' grants for the field,
      FALSE if not.
    'widget' => array(
      '#field_name' => The name of the field,
      '#title' => The label of the field,
      '#description' => The description text for the field,
      '#required' => Whether or not the field is required,
      '#field_parents' => The 'parents' space for the field in the form,
         equal to the #parents property of the $form parameter received by
         this method,

      // For 'multiple-value' widgets, the remaining elements in the
      // sub-array depend on the widget.

      // For 'single-value' widgets:
      '#theme' => 'field_multiple_value_form',
      '#cardinality' => The field cardinality,
      '#cardinality_multiple' => TRUE if the field can contain multiple
        items, FALSE otherwise.
      // One sub-array per copy of the widget, keyed by delta.
      0 => array(
        '#title' => The title to be displayed by the widget,
        '#description' => The description text for the field,
        '#required' => Whether the widget should be marked required,
        '#delta' => 0,
        '#weight' => 0,
        '#field_parents' => Same as above,
        // The remaining elements in the sub-array depend on the widget.
        ...
      ),
      1 => array(
        ...
      ),
      ...
    ),
    ...
  ),
)

Additionally, some processing data is placed in $form_state, and can be accessed by \Drupal\Core\Field\WidgetBaseInterface::getWidgetState() and \Drupal\Core\Field\WidgetBaseInterface::setWidgetState().

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity.

array $form: The form structure to fill in. This can be a full form structure, or a sub-element of a larger form. The #parents property can be set to control the location of submitted field values within $form_state->getValues(). If not specified, $form['#parents'] is set to an empty array, which results in field values located at the top-level of $form_state->getValues().

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

File

core/lib/Drupal/Core/Entity/Display/EntityFormDisplayInterface.php, line 97

Class

EntityFormDisplayInterface
Provides a common interface for entity form displays.

Namespace

Drupal\Core\Entity\Display

Code

public function buildForm(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state);

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Display!EntityFormDisplayInterface.php/function/EntityFormDisplayInterface::buildForm/8.1.x