WebformLocationGeocomplete.php in Webform 6.x
File
modules/webform_location_geocomplete/src/Element/WebformLocationGeocomplete.php
View source
<?php
namespace Drupal\webform_location_geocomplete\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Element\WebformLocationBase;
class WebformLocationGeocomplete extends WebformLocationBase {
protected static $name = 'geocomplete';
public function getInfo() {
return parent::getInfo() + [
'#api_key' => '',
];
}
public static function getLocationAttributes() {
return [
'lat' => t('Latitude'),
'lng' => t('Longitude'),
'location' => t('Location'),
'formatted_address' => t('Formatted Address'),
'street_address' => t('Street Address'),
'street_number' => t('Street Number'),
'subpremise' => t('Unit'),
'postal_code' => t('Postal Code'),
'locality' => t('Locality'),
'sublocality' => t('City'),
'administrative_area_level_1' => t('State/Province'),
'country' => t('Country'),
'country_short' => t('Country Code'),
];
}
public static function processWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
$element = parent::processWebformComposite($element, $form_state, $complete_form);
if (!empty($element['#api_key'])) {
$api_key = $element['#api_key'];
}
else {
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$api_key = $third_party_settings_manager
->getThirdPartySetting('webform_location_geocomplete', 'default_google_maps_api_key') ?: '';
}
$element['#attached']['drupalSettings']['webform']['location']['geocomplete']['api_key'] = $api_key;
$element['#attached']['library'][] = 'webform_location_geocomplete/webform_location_geocomplete.element';
return $element;
}
}