Skip to content

Instantly share code, notes, and snippets.

@dbjpanda
Created July 13, 2017 09:25
Show Gist options
  • Save dbjpanda/53e45dbf81662941e7cae56115b40671 to your computer and use it in GitHub Desktop.
Save dbjpanda/53e45dbf81662941e7cae56115b40671 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\search_api_location\Plugin\search_api_location\location_input;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api_location\LocationInput\LocationInputPluginBase;
/**
* Represents the Raw Location Input.
*
* @LocationInput(
* id = "geocode",
* label = @Translation("Geocoded input"),
* description = @Translation("Let user enter an address that will be geocoded."),
* )
*/
class Geocode extends LocationInputPluginBase {
/**
* {@inheritdoc}
*/
public function getParsedInput($input) {
if (!isset($input['value'])) {
throw new \InvalidArgumentException('Input doesn\'t contain a location value.');
}
else {
$active_plugins = $this->getActivePlugins();
/** @var \Geocoder\Model\AddressCollection $geocoded_addresses */
$geocoded_addresses = \Drupal::service('geocoder')
->geocode($input['value'], $active_plugins);
if ($geocoded_addresses) {
return $geocoded_addresses->first()
->getLatitude() . ',' . $geocoded_addresses->first()
->getLongitude();
}
}
return NULL;
}
/**
* Gets the active geocoder plugins.
*/
public function getActivePlugins() {
$plugins = $this->configuration['plugins'];
uasort($plugins, function($a,$b){return ($a["weight"] < $b["weight"]) ? -1 : 1;});
$active_plugins = array();
foreach ($plugins as $id => $plugin) {
if ($plugin['checked']) {
$active_plugins[$id] = $id;
}
}
return $active_plugins;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'plugins' => array(),
);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$geocoderpluginmanager = \Drupal::service('plugin.manager.geocoder.provider');
$form['plugins'] = [
'#type' => 'table',
'#header' => [$this->t('Geocoder plugins'), $this->t('Weight')],
'#tabledrag' => [[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'plugins-order-weight',
],
],
'#caption' => $this->t('Select the Geocoder plugins to use, you can reorder them. The first one to return a valid value will be used.'),
];
foreach ($geocoderpluginmanager->getPluginsAsOptions() as $plugin_id => $plugin_name) {
$form['plugins'][$plugin_id] = [
'checked' => [
'#type' => 'checkbox',
'#title' => $plugin_name,
'#default_value' => $this->configuration['plugins'][$plugin_id]['checked'],
],
'weight' => array(
'#type' => 'weight',
'#title' => $this->t('Weight for @title', ['@title' => $plugin_name]),
'#title_display' => 'invisible',
'#attributes' => ['class' => ['plugins-order-weight']],
'#default_value' => $this->configuration['plugins'][$plugin_id]['weight'],
),
'#attributes' => ['class' => ['draggable']],
];
}
$form += parent::buildConfigurationForm($form, $form_state);
return $form;
}
/**
* Form validation handler.
*
* @param array $form
* An associative array containing the structure of the plugin form as built
* by static::buildConfigurationForm().
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the complete form.
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// TODO: Implement validateConfigurationForm() method.
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the plugin form as built
* by static::buildConfigurationForm().
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the complete form.
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// TODO: Implement submitConfigurationForm() method.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment