You are here

private static function PcaAddressElementTrait::wrapAddressFieldsInit in Loqate 8

Same name and namespace in other branches
  1. 2.x src/PcaAddressElementTrait.php \Drupal\loqate\PcaAddressElementTrait::wrapAddressFieldsInit()

Alters the original render array in favor of PCA.

Parameters

array $element: Element array.

1 call to PcaAddressElementTrait::wrapAddressFieldsInit()
PcaAddressElementTrait::processPcaAddress in src/PcaAddressElementTrait.php
Process the PCA address form element.

File

src/PcaAddressElementTrait.php, line 55

Class

PcaAddressElementTrait
Class PcaAddressElementTrait.

Namespace

Drupal\loqate

Code

private static function wrapAddressFieldsInit(array &$element) : void {

  // Wrap original elements in a wrapper w/o affecting the render array
  // structure.
  $wrapper_id = Html::getUniqueId($element['#name'] . '-address-wrapper');

  // Check if we need to hide the address fields initially.
  $wrapper_class = '';
  if ($element['#show_address_fields'] !== TRUE) {
    $wrapper_class = 'hidden';
  }
  $children = Element::children($element);
  foreach ($children as $i => $key) {
    if (isset($element[$key]) && !empty($element[$key])) {

      // Prefix first child.
      if ($i === 0) {
        $element[$key]['#prefix'] = '<div id="' . $wrapper_id . '" class="' . $wrapper_class . '">';
      }
      elseif ($i === count($children) - 1) {
        $element[$key]['#suffix'] = '</div>';
      }
    }
  }

  // Expose address wrapper id to Drupal Settings.
  $element['#attached']['drupalSettings']['pca_address']['elements']['#' . $element['#id']]['address_wrapper'] = $wrapper_id;
}