View source
<?php
namespace Drupal\loqate;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\loqate\Form\PcaAddressSettingsForm;
trait PcaAddressElementTrait {
public function buildElementGetInfo() {
return [
'#pca_fields' => [],
'#pca_options' => [],
'#show_address_fields' => FALSE,
'#allow_manual_input' => TRUE,
];
}
public static function processPcaAddress(array &$element, FormStateInterface $form_state, array &$complete_form) {
self::wrapAddressFieldsInit($element);
self::addAddressLookupField($element);
self::addAddressLabelField($element);
self::preparePcaFieldMapping($element);
self::preparePcaOptions($element);
return $element;
}
private static function wrapAddressFieldsInit(array &$element) : void {
$wrapper_id = Html::getUniqueId($element['#name'] . '-address-wrapper');
$wrapper_class = '';
if ($element['#show_address_fields'] !== TRUE) {
$wrapper_class = 'hidden';
}
$children = Element::children($element);
foreach ($children as $i => $key) {
if (isset($element[$key]) && !empty($element[$key])) {
if ($i === 0) {
$element[$key]['#prefix'] = '<div id="' . $wrapper_id . '" class="' . $wrapper_class . '">';
}
elseif ($i === count($children) - 1) {
$element[$key]['#suffix'] = '</div>';
}
}
}
$element['#attached']['drupalSettings']['pca_address']['elements']['#' . $element['#id']]['address_wrapper'] = $wrapper_id;
}
private static function addAddressLookupField(array &$element) : void {
$element['address_lookup'] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('Search Address'),
'#placeholder' => new TranslatableMarkup('Start typing your address'),
'#weight' => -90,
'#states' => [
'visible' => [
'select[name="' . "{$element['#name']}[country_code]" . '"]' => [
'filled' => TRUE,
],
],
],
];
if ($element['#show_address_fields'] !== TRUE && $element['#allow_manual_input'] === TRUE) {
$manual_input_link = Link::fromTextAndUrl(new TranslatableMarkup('Click here'), Url::fromUserInput('#manual-address'));
$element['address_lookup']['#description'] = [
'#type' => '#markup',
'#markup' => '<span class="manual-address">' . new TranslatableMarkup('@link to enter your address manually.', [
'@link' => $manual_input_link
->toString(),
]) . '</span>',
];
}
}
private static function addAddressLabelField(array &$element) : void {
if ($element['#show_address_fields'] === TRUE) {
return;
}
$element['address_label'] = [
'#type' => 'fieldset',
'#title' => new TranslatableMarkup('Address'),
'#markup' => '<span class="address-label"></span>',
'#weight' => -80,
'#attributes' => [
'class' => [
'address-label-wrapper',
'hidden',
],
],
];
if ($element['#allow_manual_input'] === TRUE) {
$edit_input_link = Link::fromTextAndUrl(new TranslatableMarkup('Edit address'), Url::fromUserInput('#edit-address'));
$element['address_label']['#markup'] .= '<span class="edit-address">' . $edit_input_link
->toString() . '</span>';
}
}
private static function preparePcaFieldMapping(array &$element) : void {
if (empty($element['#pca_fields'])) {
$element['#pca_fields'] = \Drupal::config('loqate.settings')
->get(PcaAddressSettingsForm::PCA_FIELDS);
}
foreach ($element['#pca_fields'] as $i => $field_mapping) {
if (!isset($field_mapping['enabled']) || $field_mapping['enabled'] === FALSE) {
continue;
}
unset($element['#pca_fields'][$i]['enabled']);
if (isset($field_mapping['element']) && !empty($field_mapping['element'])) {
$element['#pca_fields'][$i]['element'] = "{$element['#name']}[{$field_mapping['element']}]";
}
if (!isset($field_mapping['field']) || empty($field_mapping['field'])) {
$element['#pca_fields'][$i]['field'] = '';
}
}
$element['#pca_fields'][] = [
'element' => "{$element['#name']}[address_lookup]",
'field' => '',
];
$element['#attached']['drupalSettings']['pca_address']['elements']['#' . $element['#id']]['fields'] = $element['#pca_fields'];
}
private static function preparePcaOptions(array &$element) : void {
$element['#attached']['drupalSettings']['pca_address']['elements']['#' . $element['#id']]['options'] = [
'key' => Loqate::getApiKey(),
];
if (isset($element['#pca_options'])) {
if (isset($element['#pca_options']['key']) && ($key = Loqate::getApiKey($element['#pca_options']['key']))) {
$element['#pca_options']['key'] = $key;
}
else {
unset($element['#pca_options']['key']);
}
$element['#attached']['drupalSettings']['pca_address']['elements']['#' . $element['#id']]['options'] = array_merge($element['#attached']['drupalSettings']['pca_address']['elements']['#' . $element['#id']]['options'], $element['#pca_options']);
}
}
}