You are here

public static function WebformMultiple::convertValuesToItems in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformMultiple.php \Drupal\webform\Element\WebformMultiple::convertValuesToItems()

Convert an array containing of values (elements or _item_ and weight) to an array of items.

Parameters

array $element: The multiple element.

array $values: An array containing of item and weight.

Return value

array An array of items.

Throws

\Exception Throws unique key required validation error message as an exception.

2 calls to WebformMultiple::convertValuesToItems()
WebformMultiple::validateWebformMultiple in src/Element/WebformMultiple.php
Validates webform multiple element.
WebformMultiple::valueCallback in src/Element/WebformMultiple.php
Determines how user input is mapped to an element's #value property.

File

src/Element/WebformMultiple.php, line 983

Class

WebformMultiple
Provides a webform element to assist in creation of multiple elements.

Namespace

Drupal\webform\Element

Code

public static function convertValuesToItems(array $element, array $values = []) {

  // Sort the item values.
  if ($element['#sorting']) {
    uasort($values, [
      'Drupal\\Component\\Utility\\SortArray',
      'sortByWeightElement',
    ]);
  }

  // Now build the associative array of items.
  $items = [];
  foreach ($values as $value) {
    $item = static::convertValueToItem($value);

    // Never add an empty item.
    if (static::isEmpty($item)) {
      continue;
    }

    // If #key is defined use it as the $items key.
    if (!empty($element['#key']) && isset($item[$element['#key']])) {
      $key_name = $element['#key'];
      $key_value = $item[$key_name];
      unset($item[$key_name]);
      $items[$key_value] = $item;
    }
    else {
      $items[] = $item;
    }
  }
  return $items;
}