You are here

protected static function Address::addressElements in Address 8

Builds the format-specific address elements.

Parameters

array $element: The existing form element array.

array $value: The address value, in $property_name => $value format.

Return value

array The modified form element array containing the format specific elements.

1 call to Address::addressElements()
Address::processAddress in src/Element/Address.php
Processes the address form element.

File

src/Element/Address.php, line 219

Class

Address
Provides an address form element.

Namespace

Drupal\address\Element

Code

protected static function addressElements(array $element, array $value) {
  $size_attributes = [
    AddressField::ADMINISTRATIVE_AREA => 30,
    AddressField::LOCALITY => 30,
    AddressField::DEPENDENT_LOCALITY => 30,
    AddressField::POSTAL_CODE => 10,
    AddressField::SORTING_CODE => 10,
    AddressField::GIVEN_NAME => 25,
    AddressField::ADDITIONAL_NAME => 25,
    AddressField::FAMILY_NAME => 25,
  ];
  $field_overrides = $element['#parsed_field_overrides'];

  /** @var \CommerceGuys\Addressing\AddressFormat\AddressFormat $address_format */
  $address_format = \Drupal::service('address.address_format_repository')
    ->get($value['country_code']);
  $required_fields = AddressFormatHelper::getRequiredFields($address_format, $field_overrides);
  $labels = LabelHelper::getFieldLabels($address_format);
  $locale = \Drupal::languageManager()
    ->getConfigOverrideLanguage()
    ->getId();
  if (Locale::matchCandidates($address_format
    ->getLocale(), $locale)) {
    $format_string = $address_format
      ->getLocalFormat();
  }
  else {
    $format_string = $address_format
      ->getFormat();
  }
  $grouped_fields = AddressFormatHelper::getGroupedFields($format_string, $field_overrides);
  foreach ($grouped_fields as $line_index => $line_fields) {
    if (count($line_fields) > 1) {

      // Used by the #pre_render callback to group fields inline.
      $element['container' . $line_index] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'address-container-inline',
          ],
        ],
      ];
    }
    foreach ($line_fields as $field_index => $field) {
      $property = FieldHelper::getPropertyName($field);
      $class = str_replace('_', '-', $property);
      $element[$property] = [
        '#type' => 'textfield',
        '#title' => $labels[$field],
        '#default_value' => isset($value[$property]) ? $value[$property] : '',
        '#required' => in_array($field, $required_fields),
        '#size' => isset($size_attributes[$field]) ? $size_attributes[$field] : 60,
        '#attributes' => [
          'class' => [
            $class,
          ],
          'autocomplete' => FieldHelper::getAutocompleteAttribute($field),
          'autocapitalize' => 'words',
        ],
      ];
      if (count($line_fields) > 1) {
        $element[$property]['#group'] = $line_index;
      }
    }
  }

  // Hide the label for the second address line.
  if (isset($element['address_line2'])) {
    $element['address_line2']['#title_display'] = 'invisible';
  }

  // Add predefined options to the created subdivision elements.
  $element = static::processSubdivisionElements($element, $value, $address_format);
  return $element;
}