View source
<?php
namespace Drupal\geofield\Plugin;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
class GeofieldProximitySourceManager extends DefaultPluginManager {
use StringTranslationTrait;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/GeofieldProximitySource', $namespaces, $module_handler, 'Drupal\\geofield\\Plugin\\GeofieldProximitySourceInterface', 'Drupal\\geofield\\Annotation\\GeofieldProximitySource');
$this
->alterInfo('geofield_geofield_proximity_source_info');
$this
->setCacheBackend($cache_backend, 'geofield_geofield_proximity_source_plugins');
}
public function buildCommonFormElements(array &$form, FormStateInterface $form_state, $context = NULL, $is_exposed = FALSE) {
$form['#attached']['library'][] = 'geofield/geofield_proximity';
$form['units'] = [
'#type' => 'select',
'#title' => $this
->t('Unit of Measure'),
'#description' => '',
'#options' => geofield_radius_options(),
'#default_value' => '',
];
$form['source_intro'] = [
'#markup' => $this
->t('How do you want to enter your proximity parameters (distance and origin point)?'),
];
$form['source'] = [
'#type' => 'select',
'#title' => $this
->t('Proximity Definition Mode (Source of Distance and Origin Point)'),
'#options' => [],
'#default_value' => '',
'#ajax' => [
'callback' => [
get_class($this),
'sourceUpdate',
],
'effect' => 'fade',
],
];
foreach ($this
->getDefinitions() as $plugin_id => $definition) {
if (isset($definition['context']) && (empty($definition['context']) || in_array($context, $definition['context'])) && (!isset($definition['exposedOnly']) || $definition['exposedOnly'] && $is_exposed) && (!isset($definition['no_ui']) || !$definition['no_ui'])) {
$form['source']['#options'][$plugin_id] = $definition['label'];
}
}
$form['source_configuration'] = [
'#type' => 'container',
'#tree' => TRUE,
'#prefix' => '<div id="proximity-source-configuration">',
'#suffix' => '</div>',
];
}
public static function sourceUpdate(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#proximity-source-configuration', $form['options']['source_configuration']));
return $response;
}
}