You are here

public static function Address::groupElements in Address 8

Groups elements with the same #group so that they can be inlined.

File

src/Element/Address.php, line 347

Class

Address
Provides an address form element.

Namespace

Drupal\address\Element

Code

public static function groupElements(array $element) {
  $sort = [];
  foreach (Element::getVisibleChildren($element) as $key) {
    if (isset($element[$key]['#group'])) {

      // Copy the element to the container and remove the original.
      $group_index = $element[$key]['#group'];
      $container_key = 'container' . $group_index;
      $element[$container_key][$key] = $element[$key];
      unset($element[$key]);

      // Mark the container for sorting.
      if (!in_array($container_key, $sort)) {
        $sort[] = $container_key;
      }
    }
  }

  // Sort the moved elements, so that their #weight stays respected.
  foreach ($sort as $key) {
    uasort($element[$key], [
      SortArray::class,
      'sortByWeightProperty',
    ]);
  }
  return $element;
}