You are here

class Dummy in Geolocation Field 8

Same name and namespace in other branches
  1. 8.3 tests/modules/geolocation_dummy_geocoder/src/Plugin/geolocation/Geocoder/Dummy.php \Drupal\geolocation_dummy_geocoder\Plugin\geolocation\Geocoder\Dummy
  2. 8.2 tests/modules/geolocation_dummy_geocoder/src/Plugin/geolocation/Geocoder/Dummy.php \Drupal\geolocation_dummy_geocoder\Plugin\geolocation\Geocoder\Dummy

Provides the Google Geocoding API.

Plugin annotation


@Geocoder(
  id = "dummy",
  name = @Translation("Dummy Geocoder"),
  locationCapable = true,
  boundaryCapable = false,
)

Hierarchy

Expanded class hierarchy of Dummy

1 file declares its use of Dummy
DummyGeocoderController.php in tests/modules/geolocation_dummy_geocoder/src/Controller/DummyGeocoderController.php

File

tests/modules/geolocation_dummy_geocoder/src/Plugin/geolocation/Geocoder/Dummy.php, line 18

Namespace

Drupal\geolocation_dummy_geocoder\Plugin\geolocation\Geocoder
View source
class Dummy extends GeocoderBase {
  public static $targets = [
    'Berlin' => [
      'lat' => 52.517037,
      'lng' => 13.38886,
    ],
    'Vladivostok' => [
      'lat' => 43.115284,
      'lng' => 131.885401,
    ],
    'Santiago de Chile' => [
      'lat' => -33.437913,
      'lng' => -70.65045600000001,
    ],
  ];

  /**
   * {@inheritdoc}
   */
  public function formAttachGeocoder(array &$render_array, $element_name) {
    $render_array['geolocation_geocoder_dummy'] = [
      '#type' => 'textfield',
      '#description' => $this
        ->t('Enter one of the statically defined targets. See geolocation/Geocoder/Dummy.php.'),
      '#attributes' => [
        'class' => [
          'form-autocomplete',
          'geolocation-views-filter-geocoder',
          'geolocation-geocoder-dummy',
        ],
        'data-source-identifier' => $element_name,
      ],
    ];
    $render_array = array_merge_recursive($render_array, [
      '#attached' => [
        'library' => [
          0 => 'geolocation_dummy_geocoder/geocoder',
        ],
      ],
    ]);
    $render_array['geolocation_geocoder_dummy_state'] = [
      '#type' => 'hidden',
      '#default_value' => 1,
      '#attributes' => [
        'class' => [
          'geolocation-geocoder-dummy-state',
        ],
        'data-source-identifier' => $element_name,
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function formValidateInput(FormStateInterface $form_state) {
    $input = $form_state
      ->getUserInput();
    if (!empty($input['geolocation_geocoder_dummy']) && empty($input['geolocation_geocoder_dummy_state'])) {
      $location_data = $this
        ->geocode($input['geolocation_geocoder_dummy']);
      if (empty($location_data)) {
        $form_state
          ->setErrorByName('geolocation_geocoder_dummy', $this
          ->t('Failed to geocode %input.', [
          '%input' => $input['geolocation_geocoder_dummy'],
        ]));
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function formProcessInput(array &$input, $element_name) {
    if (!empty($input['geolocation_geocoder_dummy']) && empty($input['geolocation_geocoder_dummy_state'])) {
      $location_data = $this
        ->geocode($input['geolocation_geocoder_dummy']);
      if (empty($location_data)) {
        $input['geolocation_geocoder_dummy_state'] = 0;
        return FALSE;
      }
      $input['geolocation_geocoder_dummy'] = $location_data['address'];
      $input['geolocation_geocoder_dummy_state'] = 1;
      return $location_data;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function geocode($address) {
    if (empty($address)) {
      return FALSE;
    }
    if (!empty(self::$targets[$address])) {
      return [
        'location' => [
          'lat' => self::$targets[$address]['lat'],
          'lng' => self::$targets[$address]['lng'],
        ],
        'boundary' => [
          'lat_north_east' => self::$targets[$address]['lat'] + 0.01,
          'lng_north_east' => self::$targets[$address]['lng'] + 0.01,
          'lat_south_west' => self::$targets[$address]['lat'] + 0.01,
          'lng_south_west' => self::$targets[$address]['lng'] + 0.01,
        ],
      ];
    }
    else {
      return FALSE;
    }
  }

}

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
Dummy::$targets public static property
Dummy::formAttachGeocoder public function Attach geocoding logic to input element. Overrides GeocoderBase::formAttachGeocoder
Dummy::formProcessInput public function Process from as altered above. Overrides GeocoderBase::formProcessInput
Dummy::formValidateInput public function Process from as altered above. Overrides GeocoderBase::formValidateInput
Dummy::geocode public function Geocode an address. Overrides GeocoderBase::geocode
GeocoderBase::$geolocationSettings protected property Geolocation settings config instance.
GeocoderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
GeocoderBase::getOptionsForm public function Return additional options form. Overrides GeocoderInterface::getOptionsForm 2
GeocoderBase::processOptionsForm public function Process the form built above. Overrides GeocoderInterface::processOptionsForm
GeocoderBase::__construct public function Constructs a new GeocoderBase object. Overrides PluginBase::__construct
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.