You are here

public function GooglePlacesAPI::formAttachGeocoder in Geolocation Field 8

Same name and namespace in other branches
  1. 8.3 modules/geolocation_google_maps/modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php \Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder\GooglePlacesAPI::formAttachGeocoder()
  2. 8.2 modules/geolocation_google_maps/modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php \Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder\GooglePlacesAPI::formAttachGeocoder()

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

modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php, line 87

Class

GooglePlacesAPI
Provides the Google Places API.

Namespace

Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder

Code

public function formAttachGeocoder(array &$render_array, $element_name) {
  $render_array['#attached']['drupalSettings']['geolocation']['google_map_url'] = $this
    ->getGoogleMapsApiUrl();
  $render_array['geolocation_geocoder_google_places_api'] = [
    '#type' => 'textfield',
    '#description' => $this
      ->t('Enter an address to filter results.'),
    '#attributes' => [
      'class' => [
        'form-autocomplete',
        'geolocation-views-filter-geocoder',
        'geolocation-geocoder-google-places-api',
      ],
      'data-source-identifier' => $element_name,
    ],
  ];
  $render_array['geolocation_geocoder_google_places_api_state'] = [
    '#type' => 'hidden',
    '#default_value' => 1,
    '#attributes' => [
      'class' => [
        'geolocation-geocoder-google-places-api-state',
      ],
      'data-source-identifier' => $element_name,
    ],
  ];
  if (!empty($render_array[$element_name]['#title'])) {
    $render_array['geolocation_geocoder_google_places_api']['#title'] = $render_array[$element_name]['#title'];
  }
  elseif ($this->configuration['label']) {
    $render_array['geolocation_geocoder_google_places_api']['#title'] = $this->configuration['label'];
  }
  $render_array['geolocation_geocoder_google_places_api'] = array_merge_recursive($render_array['geolocation_geocoder_google_places_api'], [
    '#attached' => [
      'library' => [
        0 => 'geolocation_google_places_api/geolocation_google_places_api.geocoder.googleplacesapi',
      ],
    ],
  ]);
  if (!empty($this->configuration['component_restrictions'])) {
    foreach ($this->configuration['component_restrictions'] as $component => $restriction) {
      if (empty($restriction)) {
        continue;
      }
      $render_array['geolocation_geocoder_google_places_api'] = array_merge_recursive($render_array['geolocation_geocoder_google_places_api'], [
        '#attached' => [
          'drupalSettings' => [
            'geolocation' => [
              'geocoder' => [
                'googlePlacesAPI' => [
                  'restrictions' => [
                    $component => $restriction,
                  ],
                ],
              ],
            ],
          ],
        ],
      ]);
    }
  }
}