WidgetBase::formSingleElement

protected WidgetBase::formSingleElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)

Generates the form element for a single copy of the widget.

File

core/lib/Drupal/Core/Field/WidgetBase.php, line 313

Class

WidgetBase
Base class for 'Field widget' plugin implementations.

Namespace

Drupal\Core\Field

Code

protected function formSingleElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  $entity = $items->getEntity();

  $element += array(
    '#field_parents' => $form['#parents'],
    // Only the first widget should be required.
    '#required' => $delta == 0 && $this->fieldDefinition->isRequired(),
    '#delta' => $delta,
    '#weight' => $delta,
  );

  $element = $this->formElement($items, $delta, $element, $form, $form_state);

  if ($element) {
    // Allow modules to alter the field widget form element.
    $context = array(
      'form' => $form,
      'widget' => $this,
      'items' => $items,
      'delta' => $delta,
      'default' => $this->isDefaultValueWidget($form_state),
    );
    \Drupal::moduleHandler()->alter(array('field_widget_form', 'field_widget_' . $this->getPluginId() . '_form'), $element, $form_state, $context);
  }

  return $element;
}
doc_Drupal
2016-10-29 09:56:41
Comments
Leave a Comment

Please login to continue.