public function WidgetBase::form

Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form().

Overrides WidgetBaseInterface::form

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php, line 75
Definition of Drupal\field\Plugin\Type\Widget\WidgetBase.

Class

WidgetBase
Base class for 'Field widget' plugin implementations.

Namespace

Drupal\field\Plugin\Type\Widget

Code

public function form(EntityInterface $entity, $langcode, array $items, array &$form, array &$form_state, $get_delta = NULL) {
  $entity_type = $entity
    ->entityType();
  $field = $this->field;
  $instance = $this->instance;
  $field_name = $field['field_name'];
  $parents = $form['#parents'];
  $addition = array(
    $field_name => array(),
  );

  // Populate widgets with default values when creating a new entity.
  if (empty($items) && $entity
    ->isNew()) {
    $items = field_get_default_value($entity_type, $entity, $field, $instance, $langcode);
  }

  // Store field information in $form_state.
  if (!field_form_get_state($parents, $field_name, $langcode, $form_state)) {
    $field_state = array(
      'field' => $field,
      'instance' => $instance,
      'items_count' => count($items),
      'array_parents' => array(),
      'errors' => array(),
    );
    field_form_set_state($parents, $field_name, $langcode, $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.
  $definition = $this
    ->getDefinition();
  if (isset($get_delta) || $definition['multiple_values']) {
    $delta = isset($get_delta) ? $get_delta : 0;
    $element = array(
      '#title' => check_plain($instance['label']),
      '#description' => field_filter_xss(token_replace($instance['description'])),
    );
    $element = $this
      ->formSingleElement($entity, $items, $delta, $langcode, $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;
      }
    }
  }
  else {
    $elements = $this
      ->formMultipleElements($entity, $items, $langcode, $form, $form_state);
  }

  // Also aid in theming of field widgets by rendering a classified
  // container.
  $addition[$field_name] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'field-type-' . drupal_html_class($field['type']),
        'field-name-' . drupal_html_class($field_name),
        'field-widget-' . drupal_html_class($this
          ->getPluginId()),
      ),
    ),
    '#weight' => $this->weight,
  );

  // Populate the 'array_parents' information in $form_state['field'] after
  // the form is built, so that we catch changes in the form structure performed
  // in alter() hooks.
  $elements['#after_build'][] = 'field_form_element_after_build';
  $elements['#field_name'] = $field_name;
  $elements['#language'] = $langcode;
  $elements['#field_parents'] = $parents;
  $addition[$field_name] += array(
    '#tree' => TRUE,
    // The '#language' key can be used to access the field's form element
    // when $langcode is unknown.
    '#language' => $langcode,
    $langcode => $elements,
    '#access' => field_access('edit', $field, $entity_type, $entity),
  );
  return $addition;
}