You are here

protected function AssociativeArrayWidget::initCount in Map Widget 8

Include the element count for this item in the field state.

Parameters

array $element: The current field element.

int $delta: The item delta in the field.

array $value: The value of the item.

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

Return value

int The current delta count.

1 call to AssociativeArrayWidget::initCount()
AssociativeArrayWidget::formElement in src/Plugin/Field/FieldWidget/AssociativeArrayWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/AssociativeArrayWidget.php, line 217

Class

AssociativeArrayWidget
Plugin implementation of the 'map_item_widget' widget.

Namespace

Drupal\map_widget\Plugin\Field\FieldWidget

Code

protected function initCount(array $element, $delta, array $value, FormStateInterface $form_state) {
  $field_name = $element['#field_name'];
  $parents = $element['#field_parents'];

  // Set the array element count for this delta if not set.
  $field_state = static::getWidgetState($parents, $field_name, $form_state);
  if (!isset($field_state['map_assoc_count'])) {
    $field_state['map_assoc_count'] = [];
  }
  if (!isset($field_state['map_assoc_count'][$delta])) {
    $count = count($value);
    $field_state['map_assoc_count'][$delta] = $count ? $count : 1;
  }
  static::setWidgetState($parents, $field_name, $form_state, $field_state);
  return $field_state['map_assoc_count'][$delta];
}