PcaAddressFieldWidgetTrait.php in Loqate 8
File
src/PcaAddressFieldWidgetTrait.php
View source
<?php
namespace Drupal\loqate;
use Drupal\Core\Form\FormStateInterface;
trait PcaAddressFieldWidgetTrait {
public static function getFieldWidgetDefaultSettings() {
return [
'pca_fields' => [],
'pca_options' => [],
'show_address_fields' => FALSE,
'allow_manual_input' => TRUE,
'loqate_api_key' => NULL,
];
}
public function buildFieldWidgetsettingsForm(array $form) {
$form['show_address_fields'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show address fields'),
'#default_value' => $this
->getSetting('show_address_fields'),
];
$form['allow_manual_input'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow manual input'),
'#default_value' => $this
->getSetting('allow_manual_input'),
];
$form['loqate_api_key'] = [
'#type' => 'key_select',
'#title' => $this
->t('Loqate API key'),
'#default_value' => $this
->getSetting('loqate_api_key'),
];
return $form;
}
public function buildFieldWidgetSettingsSummary() {
$summary = [];
$widget_settings = $this
->getSettings();
$summary[] = $this
->t('Show address fields: @bool', [
'@bool' => (bool) $widget_settings['show_address_fields'] ? 'Yes' : 'No',
]);
$summary[] = $this
->t('Allow manual input: @bool', [
'@bool' => (bool) $widget_settings['allow_manual_input'] ? 'Yes' : 'No',
]);
$summary[] = $this
->t('Key ID: @key', [
'@key' => !empty($widget_settings['loqate_api_key']) ? $widget_settings['loqate_api_key'] : 'None',
]);
return $summary;
}
public function buildFieldWidgetFormElement(array $element) {
$widget_settings = $this
->getSettings();
$element['address']['#pca_fields'] = $widget_settings['pca_fields'];
$element['address']['#pca_options'] = $widget_settings['pca_options'];
$element['address']['#show_address_fields'] = (bool) $widget_settings['show_address_fields'];
$element['address']['#allow_manual_input'] = (bool) $widget_settings['allow_manual_input'];
if ($widget_settings['loqate_api_key']) {
$element['address']['#pca_options']['key'] = $widget_settings['loqate_api_key'];
}
return $element;
}
}