You are here

class Geocoder in Geolocation Field 8.3

Same name in this branch
  1. 8.3 src/Annotation/Geocoder.php \Drupal\geolocation\Annotation\Geocoder
  2. 8.3 src/Plugin/geolocation/LocationInput/Geocoder.php \Drupal\geolocation\Plugin\geolocation\LocationInput\Geocoder
Same name and namespace in other branches
  1. 8.2 src/Plugin/geolocation/LocationInput/Geocoder.php \Drupal\geolocation\Plugin\geolocation\LocationInput\Geocoder

Location based proximity center.

Plugin annotation


@LocationInput(
  id = "geocoder",
  name = @Translation("Geocoder address input"),
  description = @Translation("Enter an address and use the geocoded location."),
)

Hierarchy

Expanded class hierarchy of Geocoder

4 string references to 'Geocoder'
AddressFieldProvider::getSettingsForm in modules/geolocation_address/src/Plugin/geolocation/DataProvider/AddressFieldProvider.php
Provide data provider settings form array.
geolocation.location_input.schema.yml in config/schema/geolocation.location_input.schema.yml
config/schema/geolocation.location_input.schema.yml
geolocation_google_maps.map_features.schema.yml in modules/geolocation_google_maps/config/schema/geolocation_google_maps.map_features.schema.yml
modules/geolocation_google_maps/config/schema/geolocation_google_maps.map_features.schema.yml
geolocation_leaflet.map_features.schema.yml in modules/geolocation_leaflet/config/schema/geolocation_leaflet.map_features.schema.yml
modules/geolocation_leaflet/config/schema/geolocation_leaflet.map_features.schema.yml

File

src/Plugin/geolocation/LocationInput/Geocoder.php, line 20

Namespace

Drupal\geolocation\Plugin\geolocation\LocationInput
View source
class Geocoder extends LocationInputBase implements LocationInputInterface, ContainerFactoryPluginInterface {

  /**
   * Geocoder Manager.
   *
   * @var \Drupal\geolocation\GeocoderManager
   */
  protected $geocoderManager;

  /**
   * Geocoder constructor.
   *
   * @param array $configuration
   *   Configuration.
   * @param string $plugin_id
   *   Plugin ID.
   * @param mixed $plugin_definition
   *   Plugin definition.
   * @param \Drupal\geolocation\GeocoderManager $geocoder_manager
   *   Geocoder Manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, GeocoderManager $geocoder_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->geocoderManager = $geocoder_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('plugin.manager.geolocation.geocoder'));
  }

  /**
   * {@inheritdoc}
   */
  public static function getDefaultSettings() {
    $settings = parent::getDefaultSettings();
    $settings['auto_submit'] = FALSE;
    $settings['hide_form'] = FALSE;
    $settings['plugin_id'] = '';
    $settings['settings'] = [];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm($option_id = NULL, array $settings = [], $context = NULL) {
    $form = [];
    $settings = $this
      ->getSettings($settings);
    $geocoder_options = [];
    foreach ($this->geocoderManager
      ->getDefinitions() as $geocoder_id => $geocoder_definition) {
      if (empty($geocoder_definition['locationCapable'])) {
        continue;
      }
      $geocoder_options[$geocoder_id] = $geocoder_definition['name'];
    }
    if ($geocoder_options) {
      $form['plugin_id'] = [
        '#type' => 'select',
        '#options' => $geocoder_options,
        '#title' => $this
          ->t('Geocoder plugin'),
        '#default_value' => $settings['plugin_id'],
        '#ajax' => [
          'callback' => [
            get_class($this->geocoderManager),
            'addGeocoderSettingsFormAjax',
          ],
          'wrapper' => 'location-input-geocoder-plugin-settings',
          'effect' => 'fade',
        ],
      ];
      if (!empty($settings['plugin_id'])) {
        $geocoder_plugin = $this->geocoderManager
          ->getGeocoder($settings['plugin_id'], $settings['settings']);
      }
      elseif (current(array_keys($geocoder_options))) {
        $geocoder_plugin = $this->geocoderManager
          ->getGeocoder(current(array_keys($geocoder_options)));
      }
      if (!empty($geocoder_plugin)) {
        $geocoder_settings_form = $geocoder_plugin
          ->getOptionsForm();
        if ($geocoder_settings_form) {
          $form['settings'] = $geocoder_settings_form;
        }
      }
      if (empty($form['settings'])) {
        $form['settings'] = [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#value' => $this
            ->t("No settings available."),
        ];
      }
      $form['settings'] = array_replace_recursive($form['settings'], [
        '#flatten' => TRUE,
        '#prefix' => '<div id="location-input-geocoder-plugin-settings">',
        '#suffix' => '</div>',
      ]);
      $form['auto_submit'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Auto-submit form'),
        '#default_value' => $settings['auto_submit'],
        '#description' => $this
          ->t('Only triggers if location could be set'),
      ];
      $form['hide_form'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Hide coordinates form'),
        '#default_value' => $settings['hide_form'],
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getCoordinates($form_value, $option_id, array $option_settings, $context = NULL) {
    $coordinates = parent::getCoordinates($form_value, $option_id, $option_settings, $context);
    if ($coordinates) {
      return $coordinates;
    }
    if (empty($form_value['geocoder'])) {
      return [];
    }
    $settings = $this
      ->getSettings($option_settings);
    $location_data = $this->geocoderManager
      ->getGeocoder($settings['plugin_id'], $settings['settings'])
      ->geocode($form_value['geocoder']['geolocation_geocoder_address']);
    if (!empty($location_data['location'])) {
      return $location_data['location'];
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getForm($option_id, array $option_settings, $context = NULL, array $default_value = NULL) {
    $form = parent::getForm($option_id, $option_settings, $context, $default_value);
    if (empty($form['coordinates'])) {
      return $form;
    }
    $option_settings = $this
      ->getSettings($option_settings);
    $identifier = uniqid($option_id);
    $form['coordinates']['#attributes'] = [
      'class' => [
        $identifier,
        'location-input-geocoder',
      ],
    ];
    $form['geocoder'] = [
      '#type' => 'container',
      '#attached' => [
        'library' => [
          'geolocation/location_input.geocoder',
        ],
        'drupalSettings' => [
          'geolocation' => [
            'locationInput' => [
              'geocoder' => [
                [
                  'identifier' => $identifier,
                  'autoSubmit' => $option_settings['auto_submit'],
                  'hideForm' => $option_settings['hide_form'],
                ],
              ],
            ],
          ],
        ],
      ],
    ];

    /** @var \Drupal\geolocation\GeocoderInterface $geocoder_plugin */
    $geocoder_plugin = $this->geocoderManager
      ->getGeocoder($option_settings['plugin_id'], $option_settings['settings']);
    $geocoder_plugin
      ->formAttachGeocoder($form['geocoder'], $identifier);
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
Geocoder::$geocoderManager protected property Geocoder Manager.
Geocoder::create public static function Creates an instance of the plugin. Overrides LocationInputBase::create
Geocoder::getCoordinates public function Get center value. Overrides LocationInputBase::getCoordinates
Geocoder::getDefaultSettings public static function Provide a populated settings array. Overrides LocationInputBase::getDefaultSettings
Geocoder::getForm public function Get center form. Overrides LocationInputBase::getForm
Geocoder::getSettingsForm public function Settings form by ID and context. Overrides LocationInputBase::getSettingsForm
Geocoder::__construct public function Geocoder constructor. Overrides PluginBase::__construct
LocationInputBase::getAvailableLocationInputOptions public function For one LocationInput (i.e. boundary filter), return all options. Overrides LocationInputInterface::getAvailableLocationInputOptions 1
LocationInputBase::getSettings public function Provide LocationInput option specific settings. Overrides LocationInputInterface::getSettings
LocationInputBase::validateSettingsForm public function
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.