You are here

public function GoogleGeocodingAPI::formAttachGeocoder in Geolocation Field 8

Attach geocoding logic to input element.

Parameters

array $render_array: Form containing the input element.

string $element_name: Name of the input element.

Return value

array|null Updated form element or NULL.

Overrides GeocoderBase::formAttachGeocoder

File

src/Plugin/geolocation/Geocoder/GoogleGeocodingAPI.php, line 72

Class

GoogleGeocodingAPI
Provides the Google Geocoding API.

Namespace

Drupal\geolocation\Plugin\geolocation\Geocoder

Code

public function formAttachGeocoder(array &$render_array, $element_name) {
  $render_array['geolocation_geocoder_google_geocoding_api'] = [
    '#type' => 'textfield',
    '#description' => $this
      ->t('Enter an address to filter results.'),
    '#attributes' => [
      'class' => [
        'form-autocomplete',
        'geolocation-views-filter-geocoder',
        'geolocation-geocoder-google-geocoding-api',
      ],
      'data-source-identifier' => $element_name,
    ],
  ];
  if (!empty($render_array[$element_name]['#title'])) {
    $render_array['geolocation_geocoder_google_geocoding_api']['#title'] = $render_array[$element_name]['#title'];
  }
  elseif ($this->configuration['label']) {
    $render_array['geolocation_geocoder_google_geocoding_api']['#title'] = $this->configuration['label'];
  }
  $render_array = array_merge_recursive($render_array, [
    '#attached' => [
      'library' => [
        0 => 'geolocation/geolocation.geocoder.googlegeocodingapi',
      ],
      'drupalSettings' => [
        'geolocation' => [
          'google_map_url' => $this
            ->getGoogleMapsApiUrl(),
        ],
      ],
    ],
  ]);
  $render_array['geolocation_geocoder_google_geocoding_api_state'] = [
    '#type' => 'hidden',
    '#default_value' => 1,
    '#attributes' => [
      'class' => [
        'geolocation-geocoder-google-geocoding-api-state',
      ],
      'data-source-identifier' => $element_name,
    ],
  ];
  if (!empty($this->geolocationSettings
    ->get('google_map_custom_url_parameters')['region'])) {
    $render_array['#attached']['drupalSettings']['geolocation']['geocoder']['googleGeocodingAPI']['region'] = $this->geolocationSettings
      ->get('google_map_custom_url_parameters')['region'];
  }
  if (!empty($this->configuration['components'])) {
    foreach ($this->configuration['components'] as $component => $restriction) {
      if (empty($restriction)) {
        continue;
      }
      $render_array['geolocation_geocoder_google_geocoding_api'] = array_merge_recursive($render_array['geolocation_geocoder_google_geocoding_api'], [
        '#attached' => [
          'drupalSettings' => [
            'geolocation' => [
              'geocoder' => [
                'googleGeocodingAPI' => [
                  'components' => [
                    $component => $restriction,
                  ],
                ],
              ],
            ],
          ],
        ],
      ]);
    }
  }
}