You are here

public static function MultiValue::processMultiValue in SAML Authentication 4.x

Same name and namespace in other branches
  1. 8.3 src/Element/MultiValue.php \Drupal\samlauth\Element\MultiValue::processMultiValue()

Processes a multi-value form element.

Parameters

array $element: The element being processed.

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

array $complete_form: The complete form.

Return value

array The processed element.

File

src/Element/MultiValue.php, line 177

Class

MultiValue
Provides a multi-value form element.

Namespace

Drupal\samlauth\Element

Code

public static function processMultiValue(array &$element, FormStateInterface $form_state, array &$complete_form) : array {
  $element_name = end($element['#array_parents']);
  $parents = $element['#parents'];
  $cardinality = $element['#cardinality'];
  $element['#tree'] = TRUE;
  $element['#field_name'] = $element_name;
  $element_state = static::getElementState($parents, $element_name, $form_state);
  if ($element_state === NULL) {

    // The default value should always have numeric keys. The initial count
    // is based on the default value... except if #add_empty says to add an
    // extra item only for 0 values.
    if (!$element['#default_value'] && isset($element['#add_empty']) && $element['#add_empty'] === FALSE) {
      $element_state = [
        'items_count' => 1,
      ];
    }
    else {
      $element_state = [
        'items_count' => count($element['#default_value'] ?? []),
      ];
    }
    static::setElementState($parents, $element_name, $form_state, $element_state);
  }

  // Determine the number of elements to display.
  if ($cardinality !== self::CARDINALITY_UNLIMITED) {
    $nr_elements = $cardinality;
  }
  elseif (!empty($element['#disabled']) || isset($element['#add_empty']) && !$element['#add_empty']) {
    $nr_elements = $element_state['items_count'];
  }
  else {
    $nr_elements = $element_state['items_count'] + 1;
  }

  // Extract the elements that will have to be repeated for each delta.
  $children = [];
  foreach (Element::children($element) as $child) {
    $children[$child] = $element[$child];
    unset($element[$child]);
  }
  $value = is_array($element['#value']) ? $element['#value'] : [];

  // Re-key the elements so that deltas are consecutive.
  $value = array_values($value);
  for ($i = 0; $i < $nr_elements; $i++) {
    $element[$i] = $children;
    if (isset($value[$i])) {
      static::setDefaultValue($element[$i], $value[$i]);
    }
    static::setRequiredProperty($element[$i], $i, $element['#required']);
    $element[$i]['_weight'] = [
      '#type' => 'weight',
      '#title' => t('Weight for row @number', [
        '@number' => $i + 1,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $i,
      '#weight' => 100,
    ];
  }
  if ($cardinality === self::CARDINALITY_UNLIMITED && !$form_state
    ->isProgrammed()) {
    $id_prefix = implode('-', $parents);
    $wrapper_id = Html::getUniqueId($id_prefix . '-add-more-wrapper');
    $element['#prefix'] = '<div id="' . $wrapper_id . '">';
    $element['#suffix'] = '</div>';
    $element['add_more'] = [
      '#type' => 'submit',
      '#name' => strtr($id_prefix, '-', '_') . '_add_more',
      '#value' => $element['#add_more_label'],
      '#attributes' => [
        'class' => [
          'multivalue-add-more-submit',
        ],
      ],
      '#limit_validation_errors' => [
        $element['#array_parents'],
      ],
      '#submit' => [
        [
          static::class,
          'addMoreSubmit',
        ],
      ],
      '#ajax' => [
        'callback' => [
          static::class,
          'addMoreAjax',
        ],
        'wrapper' => $wrapper_id,
        'effect' => 'fade',
      ],
    ];
  }
  return $element;
}