You are here

protected function SmartDateWidgetBase::formMultipleElements in Smart Date 3.2.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::formMultipleElements()
  2. 3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::formMultipleElements()
  3. 3.0.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::formMultipleElements()
  4. 3.1.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::formMultipleElements()
  5. 3.3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::formMultipleElements()
  6. 3.4.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::formMultipleElements()

Special handling to create form elements for multiple values.

Handles generic features for multiple fields:

  • number of widgets
  • AHAH-'add more' button
  • table display and drag-n-drop value reordering.

Overrides WidgetBase::formMultipleElements

File

src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php, line 403

Class

SmartDateWidgetBase
Base class for the 'smartdate_*' widgets.

Namespace

Drupal\smart_date\Plugin\Field\FieldWidget

Code

protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
  $field_name = $this->fieldDefinition
    ->getName();
  $cardinality = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality();
  $parents = $form['#parents'];

  // Determine the number of widgets to display.
  switch ($cardinality) {
    case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
      $field_state = static::getWidgetState($parents, $field_name, $form_state);
      $max = $field_state['items_count'];
      $is_multiple = TRUE;
      break;
    default:
      $max = $cardinality - 1;
      $is_multiple = $cardinality > 1;
      break;
  }
  if ($max > 0 && !$this
    ->getSetting('show_extra')) {
    $max -= 1;
  }
  $title = $this->fieldDefinition
    ->getLabel();
  $description = FieldFilteredMarkup::create(\Drupal::token()
    ->replace($this->fieldDefinition
    ->getDescription()));
  $elements = [];
  for ($delta = 0; $delta <= $max; $delta++) {

    // Add a new empty item if it doesn't exist yet at this delta.
    if (!isset($items[$delta])) {
      $items
        ->appendItem();
    }

    // For multiple fields, title and description are handled by the wrapping
    // table.
    if ($is_multiple) {
      $element = [
        '#title' => $this
          ->t('@title (value @number)', [
          '@title' => $title,
          '@number' => $delta + 1,
        ]),
        '#title_display' => 'invisible',
        '#description' => '',
      ];
    }
    else {
      $element = [
        '#title' => $title,
        '#title_display' => 'before',
        '#description' => $description,
      ];
    }
    $element = $this
      ->formSingleElement($items, $delta, $element, $form, $form_state);
    if ($element && (!isset($element['#access']) || $element['#access'] !== FALSE)) {

      // Input field for the delta (drag-n-drop reordering).
      if ($is_multiple) {

        // We name the element '_weight' to avoid clashing with elements
        // defined by widget.
        $element['_weight'] = [
          '#type' => 'weight',
          '#title' => $this
            ->t('Weight for row @number', [
            '@number' => $delta + 1,
          ]),
          '#title_display' => 'invisible',
          // Note: this 'delta' is the FAPI #type 'weight' element's property.
          '#delta' => $max,
          '#default_value' => $items[$delta]->_weight ?: $delta,
          '#weight' => 100,
        ];
      }
      $elements[$delta] = $element;
    }
  }
  if ($elements) {
    $elements += [
      '#theme' => 'field_multiple_value_form',
      '#field_name' => $field_name,
      '#cardinality' => $cardinality,
      '#cardinality_multiple' => $this->fieldDefinition
        ->getFieldStorageDefinition()
        ->isMultiple(),
      '#required' => $this->fieldDefinition
        ->isRequired(),
      '#title' => $title,
      '#description' => $description,
      '#max_delta' => $max,
    ];

    // Add 'add more' button, if not working with a programmed form.
    if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && !$form_state
      ->isProgrammed()) {
      $id_prefix = implode('-', array_merge($parents, [
        $field_name,
      ]));
      $wrapper_id = Html::getUniqueId($id_prefix . '-add-more-wrapper');
      $elements['#prefix'] = '<div id="' . $wrapper_id . '">';
      $elements['#suffix'] = '</div>';
      $elements['add_more'] = [
        '#type' => 'submit',
        '#name' => strtr($id_prefix, '-', '_') . '_add_more',
        '#value' => t('Add another item'),
        '#attributes' => [
          'class' => [
            'field-add-more-submit',
          ],
        ],
        '#limit_validation_errors' => [
          array_merge($parents, [
            $field_name,
          ]),
        ],
        '#submit' => [
          [
            get_class($this),
            'addMoreSubmit',
          ],
        ],
        '#ajax' => [
          'callback' => [
            get_class($this),
            'addMoreAjax',
          ],
          'wrapper' => $wrapper_id,
          'effect' => 'fade',
        ],
      ];
    }
  }
  return $elements;
}