View source
<?php
namespace Drupal\loqate\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\loqate\PcaAddressFieldMapping\PcaAddressElement;
use Drupal\loqate\PcaAddressElementTrait;
class LoqatePcaAddress extends FormElement {
use PcaAddressElementTrait;
public function getInfo() {
$class = get_class($this);
return $this
->buildElementGetInfo() + [
'#process' => [
[
$class,
'processAddress',
],
[
$class,
'processPcaAddress',
],
],
'#element_validate' => [
[
$class,
'validateAddress',
],
],
'#attached' => [
'library' => [
'loqate/element.pca_address.address.js',
],
],
'#theme_wrappers' => [
'container',
],
];
}
public static function processAddress(array &$element, FormStateInterface $form_state, array &$complete_form) {
$element['#tree'] = TRUE;
$element[PcaAddressElement::LINE1] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('Address Line 1'),
'#default_value' => $element['#default_value'][PcaAddressElement::LINE1] ?? NULL,
'#required' => TRUE,
];
$element[PcaAddressElement::LINE2] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('Address Line 2'),
'#default_value' => $element['#default_value'][PcaAddressElement::LINE2] ?? NULL,
];
$element[PcaAddressElement::POSTAL_CODE] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('ZIP/Postal Code'),
'#default_value' => $element['#default_value'][PcaAddressElement::POSTAL_CODE] ?? NULL,
'#required' => TRUE,
'#size' => 10,
];
$element[PcaAddressElement::LOCALITY] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('City/Town'),
'#default_value' => $element['#default_value'][PcaAddressElement::LOCALITY] ?? NULL,
'#required' => TRUE,
'#size' => 30,
];
$element[PcaAddressElement::ADMINISTRATIVE_AREA] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('State/Province'),
'#default_value' => $element['#default_value'][PcaAddressElement::ADMINISTRATIVE_AREA] ?? NULL,
'#size' => 30,
];
$element[PcaAddressElement::COUNTRY_CODE] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('Country'),
'#default_value' => $element['#default_value'][PcaAddressElement::COUNTRY_CODE] ?? NULL,
];
return $element;
}
public static function validateAddress(&$element, FormStateInterface $form_state, &$complete_form) {
}
}