You are here

protected function Address::buildAddress in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/Address.php \Drupal\webform\Plugin\WebformElement\Address::buildAddress()

Build formatted address.

The below code is copied form the protected AddressDefaultFormatter::viewElements method.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

array $options: An array of options.

Return value

array A render array containing the formatted address.

See also

\Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter::viewElements

\Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter::viewElement

2 calls to Address::buildAddress()
Address::formatHtmlItem in src/Plugin/WebformElement/Address.php
Format an element's value as HTML.
Address::formatTextItem in src/Plugin/WebformElement/Address.php
Format an element's value as text.

File

src/Plugin/WebformElement/Address.php, line 238

Class

Address
Provides a 'address' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function buildAddress(array $element, WebformSubmissionInterface $webform_submission, array $options) {

  /** @var \CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface $address_format_repository */
  $address_format_repository = \Drupal::service('address.address_format_repository');

  /** @var \CommerceGuys\Addressing\Country\CountryRepositoryInterface $country_repository */
  $country_repository = \Drupal::service('address.country_repository');
  $value = $this
    ->getValue($element, $webform_submission, $options);

  // Skip if value or country code is empty.
  if (empty($value) || empty($value['country_code'])) {
    return [];
  }

  // @see \Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter::viewElements
  $build = [
    '#prefix' => '<div class="address" translate="no">',
    '#suffix' => '</div>',
    '#post_render' => [
      [
        '\\Drupal\\address\\Plugin\\Field\\FieldFormatter\\AddressDefaultFormatter',
        'postRender',
      ],
    ],
    '#cache' => [
      'contexts' => [
        'languages:' . LanguageInterface::TYPE_INTERFACE,
      ],
    ],
  ];

  // @see \Drupal\address\Plugin\Field\FieldFormatter\AddressDefaultFormatter::viewElement
  $country_code = $value['country_code'];
  $countries = $country_repository
    ->getList();
  $address_format = $address_format_repository
    ->get($country_code);
  $build += [
    '#address_format' => $address_format,
    '#locale' => 'und',
  ];
  $build['country'] = [
    '#type' => 'html_tag',
    '#tag' => 'span',
    '#attributes' => [
      'class' => [
        'country',
      ],
    ],
    '#value' => Html::escape($countries[$country_code]),
    '#placeholder' => '%country',
  ];
  foreach ($address_format
    ->getUsedFields() as $field) {
    $property = FieldHelper::getPropertyName($field);
    $class = str_replace('_', '-', $property);
    $build[$property] = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#attributes' => [
        'class' => [
          $class,
        ],
      ],
      '#value' => !empty($value[$property]) ? Html::escape($value[$property]) : '',
      '#placeholder' => '%' . $field,
    ];
  }
  return $build;
}