SelectionPluginManager.php in Drupal 8
File
core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginManager.php
View source
<?php
namespace Drupal\Core\Entity\EntityReferenceSelection;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
class SelectionPluginManager extends DefaultPluginManager implements SelectionPluginManagerInterface, FallbackPluginManagerInterface {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
$this
->alterInfo('entity_reference_selection');
$this
->setCacheBackend($cache_backend, 'entity_reference_selection_plugins');
parent::__construct('Plugin/EntityReferenceSelection', $namespaces, $module_handler, 'Drupal\\Core\\Entity\\EntityReferenceSelection\\SelectionInterface', 'Drupal\\Core\\Entity\\Annotation\\EntityReferenceSelection');
}
public function getInstance(array $options) {
if (!isset($options['target_type'])) {
throw new \InvalidArgumentException("Missing required 'target_type' property for a EntityReferenceSelection plugin.");
}
$options += [
'handler' => $this
->getPluginId($options['target_type'], 'default'),
];
if (strpos($options['handler'], ':') !== FALSE) {
$plugin_id = $options['handler'];
}
else {
$plugin_id = $this
->getPluginId($options['target_type'], $options['handler']);
}
unset($options['handler']);
return $this
->createInstance($plugin_id, $options);
}
public function getPluginId($target_type, $base_plugin_id) {
$selection_handler_groups = $this
->getSelectionGroups($target_type);
uasort($selection_handler_groups[$base_plugin_id], [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
end($selection_handler_groups[$base_plugin_id]);
$plugin_id = key($selection_handler_groups[$base_plugin_id]);
return $plugin_id;
}
public function getSelectionGroups($entity_type_id) {
$plugins = [];
$definitions = $this
->getDefinitions();
unset($definitions['broken']);
foreach ($definitions as $plugin_id => $plugin) {
if (empty($plugin['entity_types']) || in_array($entity_type_id, $plugin['entity_types'])) {
$plugins[$plugin['group']][$plugin_id] = $plugin;
}
}
return $plugins;
}
public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
$options = $field_definition
->getSetting('handler_settings') ?: [];
$options += [
'target_type' => $field_definition
->getFieldStorageDefinition()
->getSetting('target_type'),
'handler' => $field_definition
->getSetting('handler'),
'entity' => $entity,
];
return $this
->getInstance($options);
}
public function getFallbackPluginId($plugin_id, array $configuration = []) {
return 'broken';
}
}