You are here

protected function OfficeHoursDefaultWidget::formMultipleElements in Office Hours 8

Special handling to create form elements for multiple values.

Removed the added generic features for multiple fields:

  • Number of widgets;
  • AHAH 'add more' button;
  • Table display and drag-n-drop value reordering.

N.B. This is never called with Annotation: multiple_values = "FALSE".

Overrides WidgetBase::formMultipleElements

File

src/Plugin/Field/FieldWidget/OfficeHoursDefaultWidget.php, line 35

Class

OfficeHoursDefaultWidget
Plugin implementation of the 'office_hours_default' widget.

Namespace

Drupal\office_hours\Plugin\Field\FieldWidget

Code

protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
  $field_cardinality = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality();
  if ($field_cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
    $this->fieldDefinition
      ->getFieldStorageDefinition()
      ->setCardinality($this
      ->getFieldSetting('cardinality_per_day') * 7);
  }
  $elements = parent::formMultipleElements($items, $form, $form_state);

  // Remove the 'drag-n-drop reordering' element.
  $elements['#cardinality_multiple'] = FALSE;

  // Remove the little 'Weight for row n' box.
  unset($elements[0]['_weight']);
  return $elements;
}