View source
<?php
namespace Drupal\pca_webform\Element;
use Drupal\loqate\Loqate;
use Drupal\webform\Element\WebformCompositeBase;
class WebformAddressLoqate extends WebformCompositeBase {
public function getInfo() {
return parent::getInfo() + [
'#theme' => 'webform_composite_address',
];
}
public static function preRenderWebformCompositeFormElement($element) {
$element = parent::preRenderWebformCompositeFormElement($element);
$element['#attributes']['class'][] = 'address-lookup';
foreach (array_keys($element['#webform_composite_elements']) as $key) {
if ($key !== 'postal_code') {
$element[$key]['#wrapper_attributes']['class'][] = 'address-lookup__field--initial';
}
$element[$key]['#wrapper_attributes']['class'][] = 'address-lookup__field';
$element[$key]['#wrapper_attributes']['data-key'] = $element['#webform_key'];
}
$element['#attached'] = [
'library' => [
'pca_webform/element.pca_webform.address.js',
],
'drupalSettings' => [
'loqate' => [
'loqate' => [
'key' => Loqate::getApiKey(),
],
],
],
];
return $element;
}
public static function getCompositeElements(array $element) {
$drupal_translation = \Drupal::translation();
$elements = [];
$elements['address'] = [
'#type' => 'textfield',
'#title' => $drupal_translation
->translate('Address'),
];
$elements['address_2'] = [
'#type' => 'textfield',
'#title' => $drupal_translation
->translate('Address 2'),
];
$elements['city'] = [
'#type' => 'textfield',
'#title' => $drupal_translation
->translate('City/Town'),
];
$elements['region'] = [
'#type' => 'textfield',
'#title' => $drupal_translation
->translate('Region'),
];
$elements['state_province'] = [
'#type' => 'select',
'#title' => $drupal_translation
->translate('State/Province'),
'#options' => 'state_province_names',
'#empty_option' => '',
];
$elements['postal_code'] = [
'#type' => 'textfield',
'#title' => $drupal_translation
->translate('Zip/Postal Code'),
];
$elements['country'] = [
'#type' => 'select',
'#title' => $drupal_translation
->translate('Country'),
'#options' => 'country_names',
'#empty_option' => '',
];
return $elements;
}
}