You are here

protected function WebformAddressLoqate::formatTextItemValue in Loqate 8

Same name and namespace in other branches
  1. 2.x modules/pca_webform/src/Plugin/WebformElement/WebformAddressLoqate.php \Drupal\pca_webform\Plugin\WebformElement\WebformAddressLoqate::formatTextItemValue()

Format composite element value into lines of text.

Parameters

array $element: An element.

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

array $options: An array of options.

Return value

array Composite element values converted into lines of text.

Overrides WebformCompositeBase::formatTextItemValue

1 call to WebformAddressLoqate::formatTextItemValue()
WebformAddressLoqate::formatHtmlItemValue in modules/pca_webform/src/Plugin/WebformElement/WebformAddressLoqate.php
Format composite element value into lines of text.

File

modules/pca_webform/src/Plugin/WebformElement/WebformAddressLoqate.php, line 41

Class

WebformAddressLoqate
Provides an 'address' element.

Namespace

Drupal\pca_webform\Plugin\WebformElement

Code

protected function formatTextItemValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $value = $this
    ->getValue($element, $webform_submission, $options);
  $composite_elements = $element['#webform_composite_elements'];
  $location = '';

  // State/Province and Region can't both have values, since Region is only
  // used when S/P is not populatable.
  if (!empty($value['state_province'])) {
    if (!empty($value['city'])) {
      $location .= $value['city'];
    }
    $location .= $location ? ', ' : '';
    $location .= $this
      ->getValueFromOptions('state_province', $composite_elements, $value);
    if (!empty($value['postal_code'])) {
      $location .= $location ? '. ' : '';
      $location .= $value['postal_code'];
    }
    $value['location'] = $location;
    unset($value['city'], $value['region'], $value['postal_code']);
  }

  // Country preprocessing.
  if (!empty($value['country'])) {
    $value['country'] = $this
      ->getValueFromOptions('country', $composite_elements, $value);
  }
  $address_lines = [
    'address',
    'address_2',
    'location',
    'city',
    'region',
    'postal_code',
    'country',
  ];
  $display_lines = [];
  foreach ($address_lines as $line) {
    if (!empty($value[$line])) {
      $display_lines[$line] = $value[$line];
    }
  }
  return $display_lines;
}