View source
<?php
namespace Drupal\geocoder;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\geocoder\Annotation\GeocoderProvider;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class ProviderPluginManager extends GeocoderPluginManagerBase {
use StringTranslationTrait;
protected $config;
protected $renderer;
protected $link;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, RendererInterface $renderer, LinkGeneratorInterface $link_generator) {
parent::__construct('Plugin/Geocoder/Provider', $namespaces, $module_handler, ProviderInterface::class, GeocoderProvider::class);
$this
->alterInfo('geocoder_provider_info');
$this
->setCacheBackend($cache_backend, 'geocoder_provider_plugins');
$this->config = $config_factory
->get('geocoder.settings');
$this->stringTranslation = $string_translation;
$this->renderer = $renderer;
$this->link = $link_generator;
}
public function getPlugins() {
$plugins_arguments = (array) $this->config
->get('plugins_options');
$definitions = array_map(function (array $definition) use ($plugins_arguments) {
$plugins_arguments += [
$definition['id'] => [],
];
$definition += [
'name' => $definition['id'],
'arguments' => [],
];
$definition['arguments'] = array_merge((array) $definition['arguments'], (array) $plugins_arguments[$definition['id']]);
return $definition;
}, $this
->getDefinitions());
ksort($definitions);
return $definitions;
}
public function providersPluginsTableList(array $enabled_plugins) {
$geocoder_settings_link = $this->link
->generate(t('Edit options in the Geocoder configuration page</span>'), Url::fromRoute('geocoder.settings', [], [
'query' => [
'destination' => Url::fromRoute('<current>')
->toString(),
],
]));
$options_field_description = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('Object literals in YAML format. @geocoder_settings_link', [
'@geocoder_settings_link' => $geocoder_settings_link,
]),
'#attributes' => [
'class' => [
'options-field-description',
],
],
];
$caption = [
'title' => [
'#type' => 'html_tag',
'#tag' => 'label',
'#value' => $this
->t('Geocoder plugin(s)'),
],
'caption' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('Select the Geocoder plugins to use, you can reorder them. The first one to return a valid value will be used.'),
],
];
$element['plugins'] = [
'#type' => 'table',
'#header' => [
$this
->t('Name'),
$this
->t('Weight'),
$this
->t('Options<br>@options_field_description', [
'@options_field_description' => $this->renderer
->renderRoot($options_field_description),
]),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'plugins-order-weight',
],
],
'#caption' => $this->renderer
->renderRoot($caption),
'#attributes' => [
'class' => [
'js-form-item',
'geocode-plugins-list',
],
],
];
$plugins = array_combine($enabled_plugins, $enabled_plugins);
foreach ($this
->getPlugins() as $plugin) {
$plugins[$plugin['id']] = $plugin;
}
$plugins = array_map(function ($plugin, $weight) use ($enabled_plugins) {
$checked = in_array($plugin['id'], $enabled_plugins);
return array_merge($plugin, [
'checked' => $checked,
'weight' => $checked ? $weight : 0,
'arguments' => empty($plugin['arguments']) ? (string) $this
->t("This plugin doesn't accept arguments.") : Yaml::encode($plugin['arguments']),
]);
}, $plugins, range(0, count($plugins) - 1));
uasort($plugins, function ($pluginA, $pluginB) {
$order = strcmp($pluginB['checked'], $pluginA['checked']);
if (0 === $order) {
$order = $pluginA['weight'] - $pluginB['weight'];
if (0 === $order) {
$order = strcmp($pluginA['name'], $pluginB['name']);
}
}
return $order;
});
foreach ($plugins as $plugin) {
$element['plugins'][$plugin['id']] = [
'checked' => [
'#type' => 'checkbox',
'#title' => $plugin['name'],
'#default_value' => $plugin['checked'],
],
'weight' => [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $plugin['name'],
]),
'#title_display' => 'invisible',
'#default_value' => $plugin['weight'],
'#delta' => 20,
'#attributes' => [
'class' => [
'plugins-order-weight',
],
],
],
'arguments' => [
'#type' => 'html_tag',
'#tag' => 'pre',
'#value' => $plugin['arguments'],
],
'#attributes' => [
'class' => [
'draggable',
],
],
];
}
return $element['plugins'];
}
private function arrayLowerKeyCaseRecursive(array $arr) {
return array_map(function ($item) {
if (is_array($item)) {
$item = $this
->arrayLowerKeyCaseRecursive($item);
}
return $item;
}, array_change_key_case($arr));
}
}