SelectionPluginManager.php in Multi-path autocomplete 8
File
lib/Drupal/mpac/Plugin/Type/SelectionPluginManager.php
View source
<?php
namespace Drupal\mpac\Plugin\Type;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\Factory\ReflectionFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\mpac\Plugin\Type\Selection\SelectionBroken;
class SelectionPluginManager extends DefaultPluginManager {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$this->discovery = new AnnotatedClassDiscovery('Plugin/mpac/selection', $namespaces, 'Drupal\\mpac\\Annotation\\MpacSelection');
$this->factory = new ReflectionFactory($this);
$this
->alterInfo($module_handler, 'mpac_selection');
$this
->setCacheBackend($cache_backend, $language_manager, 'mpac_selection');
}
public function getInstance(array $options) {
$type = $options['type'];
$selection_handler_groups = $this
->getSelectionGroups($type);
uasort($selection_handler_groups, array(
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
));
end($selection_handler_groups);
$plugin_id = key($selection_handler_groups);
if ($plugin_id) {
return $this
->createInstance($plugin_id, $options);
}
else {
return new SelectionBroken();
}
}
public function getSelectionGroups($type) {
$plugins = array();
foreach ($this
->getDefinitions() as $plugin_id => $plugin) {
if (!isset($plugin['types']) || in_array($type, $plugin['types'])) {
$plugins[$plugin_id] = $plugin;
}
}
return $plugins;
}
}