You are here

class ProviderPluginManager in Geocoder 8.2

Same name and namespace in other branches
  1. 8.3 src/ProviderPluginManager.php \Drupal\geocoder\ProviderPluginManager

Provides a plugin manager for geocoder providers.

Hierarchy

Expanded class hierarchy of ProviderPluginManager

8 files declare their use of ProviderPluginManager
AddressGeocodeFormatter.php in modules/geocoder_address/src/Plugin/Field/FieldFormatter/AddressGeocodeFormatter.php
DefaultField.php in modules/geocoder_field/src/Plugin/Geocoder/Field/DefaultField.php
FileGeocodeFormatter.php in modules/geocoder_field/src/Plugin/Field/FieldFormatter/FileGeocodeFormatter.php
GeocodeFormatterBase.php in modules/geocoder_field/src/Plugin/Field/GeocodeFormatterBase.php
GeocodeOrigin.php in src/Plugin/GeofieldProximitySource/GeocodeOrigin.php

... See full list

1 string reference to 'ProviderPluginManager'
geocoder.services.yml in ./geocoder.services.yml
geocoder.services.yml
1 service uses ProviderPluginManager
plugin.manager.geocoder.provider in ./geocoder.services.yml
Drupal\geocoder\ProviderPluginManager

File

src/ProviderPluginManager.php, line 19

Namespace

Drupal\geocoder
View source
class ProviderPluginManager extends GeocoderPluginManagerBase {
  use StringTranslationTrait;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * The Renderer service property.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $renderer;

  /**
   * The Link generator Service.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $link;

  /**
   * Constructs a new geocoder provider plugin manager.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations,.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   The cache backend to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
   *   The Link Generator service.
   */
  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;
  }

  /**
   * Return the array of plugins and their settings if any.
   *
   * @return array
   *   A list of plugins with their settings.
   */
  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;
  }

  /**
   * Generates the Draggable Table of Selectable Geocoder Plugins.
   *
   * @param array $enabled_plugins
   *   The list of the enabled plugins machine names.
   *
   * @return array
   *   The plugins table list.
   */
  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),
      // We need this class for #states to hide the entire table.
      '#attributes' => [
        'class' => [
          'js-form-item',
          'geocode-plugins-list',
        ],
      ],
    ];

    // Reorder the plugins promoting the default ones in the proper order.
    $plugins = array_combine($enabled_plugins, $enabled_plugins);
    foreach ($this
      ->getPlugins() as $plugin) {

      // Non-default values are appended at the end.
      $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'];
  }

  /**
   * Function to lower case keys in a multidimensional array.
   *
   * @param array $arr
   *   The input array.
   *
   * @return array
   *   The return array
   *
   * @TODO: This should be removed before the stable release 8.x-2.0.
   */
  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));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultPluginManager::$additionalAnnotationNamespaces protected property Additional namespaces the annotation discovery mechanism should scan for annotation definitions.
DefaultPluginManager::$alterHook protected property Name of the alter hook if one should be invoked.
DefaultPluginManager::$cacheKey protected property The cache key.
DefaultPluginManager::$cacheTags protected property An array of cache tags to use for the cached definitions.
DefaultPluginManager::$defaults protected property A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. 9
DefaultPluginManager::$moduleHandler protected property The module handler to invoke the alter hook. 1
DefaultPluginManager::$namespaces protected property An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations.
DefaultPluginManager::$pluginDefinitionAnnotationName protected property The name of the annotation that contains the plugin definition.
DefaultPluginManager::$pluginInterface protected property The interface each plugin should implement. 1
DefaultPluginManager::$subdir protected property The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace.
DefaultPluginManager::alterDefinitions protected function Invokes the hook to alter the definitions if the alter hook is set. 1
DefaultPluginManager::alterInfo protected function Sets the alter hook name.
DefaultPluginManager::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface::clearCachedDefinitions 5
DefaultPluginManager::extractProviderFromDefinition protected function Extracts the provider from a plugin definition.
DefaultPluginManager::findDefinitions protected function Finds plugin definitions. 7
DefaultPluginManager::fixContextAwareDefinitions private function Fix the definitions of context-aware plugins.
DefaultPluginManager::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
DefaultPluginManager::getCachedDefinitions protected function Returns the cached plugin definitions of the decorated discovery class.
DefaultPluginManager::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
DefaultPluginManager::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
DefaultPluginManager::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryTrait::getDefinitions 2
DefaultPluginManager::getDiscovery protected function Gets the plugin discovery. Overrides PluginManagerBase::getDiscovery 12
DefaultPluginManager::getFactory protected function Gets the plugin factory. Overrides PluginManagerBase::getFactory
DefaultPluginManager::processDefinition public function Performs extra processing on plugin definitions. 13
DefaultPluginManager::providerExists protected function Determines if the provider of a definition exists. 3
DefaultPluginManager::setCacheBackend public function Initialize the cache backend.
DefaultPluginManager::setCachedDefinitions protected function Sets a cache of plugin definitions for the decorated discovery class.
DefaultPluginManager::useCaches public function Disable the use of caches. Overrides CachedDiscoveryInterface::useCaches 1
DiscoveryCachedTrait::$definitions protected property Cached definitions array. 1
DiscoveryCachedTrait::getDefinition public function Overrides DiscoveryTrait::getDefinition 3
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::hasDefinition public function
GeocoderPluginManagerBase::$geocodeSourceFieldsTypes protected property List of fields types available as source for Geocode operations.
GeocoderPluginManagerBase::$reverseGeocodeSourceFieldsTypes protected property List of fields types available as source for Reverse Geocode operations.
GeocoderPluginManagerBase::getGeocodeSourceFieldsTypes public function Gets a list of fields types available for Geocode operations.
GeocoderPluginManagerBase::getPluginsAsOptions public function Gets a list of available plugins to be used in forms.
GeocoderPluginManagerBase::getReverseGeocodeSourceFieldsTypes public function Gets a list of fields types available for Reverse Geocode operations.
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::createInstance public function Creates a pre-configured instance of a plugin. Overrides FactoryInterface::createInstance 12
PluginManagerBase::getInstance public function Gets a preconfigured instance of a plugin. Overrides MapperInterface::getInstance 7
PluginManagerBase::handlePluginNotFound protected function Allows plugin managers to specify custom behavior if a plugin is not found. 1
ProviderPluginManager::$config protected property The config factory service.
ProviderPluginManager::$link protected property The Link generator Service.
ProviderPluginManager::$renderer protected property The Renderer service property.
ProviderPluginManager::arrayLowerKeyCaseRecursive private function Function to lower case keys in a multidimensional array.
ProviderPluginManager::getPlugins public function Return the array of plugins and their settings if any. Overrides GeocoderPluginManagerBase::getPlugins
ProviderPluginManager::providersPluginsTableList public function Generates the Draggable Table of Selectable Geocoder Plugins.
ProviderPluginManager::__construct public function Constructs a new geocoder provider plugin manager. Overrides DefaultPluginManager::__construct
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.
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag. 1
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.