View source
<?php
namespace Drupal\pca_webform\Plugin\WebformElement;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\Plugin\WebformElement\WebformCompositeBase;
class WebformAddressLoqate extends WebformCompositeBase {
public function getPluginLabel() {
return \Drupal::moduleHandler()
->moduleExists('pca_address') ? $this
->t('Basic PCA address (deprecated)') : parent::getPluginLabel();
}
protected function formatHtmlItemValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
return $this
->formatTextItemValue($element, $webform_submission, $options);
}
protected function formatTextItemValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$composite_elements = $element['#webform_composite_elements'];
$location = '';
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']);
}
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;
}
protected function getValueFromOptions($field, $composite_elements, $values) {
$value = $values[$field];
$field = $composite_elements[$field];
$type = $field['#type'];
$option_value = $type === 'select' ? $field['#options'][$value] : $value;
if ($option_value instanceof TranslatableMarkup) {
$option_value = (string) $option_value;
}
return $option_value;
}
}