"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginManager.php" (26 Nov 2020, 3711 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "SelectionPluginManager.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Core\Entity\EntityReferenceSelection;
    4 
    5 use Drupal\Component\Plugin\FallbackPluginManagerInterface;
    6 use Drupal\Core\Cache\CacheBackendInterface;
    7 use Drupal\Core\Entity\EntityInterface;
    8 use Drupal\Core\Field\FieldDefinitionInterface;
    9 use Drupal\Core\Extension\ModuleHandlerInterface;
   10 use Drupal\Core\Plugin\DefaultPluginManager;
   11 
   12 /**
   13  * Plugin type manager for Entity Reference Selection plugins.
   14  *
   15  * @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
   16  * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
   17  * @see plugin_api
   18  */
   19 class SelectionPluginManager extends DefaultPluginManager implements SelectionPluginManagerInterface, FallbackPluginManagerInterface {
   20 
   21   /**
   22    * {@inheritdoc}
   23    */
   24   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
   25     $this->alterInfo('entity_reference_selection');
   26     $this->setCacheBackend($cache_backend, 'entity_reference_selection_plugins');
   27 
   28     parent::__construct('Plugin/EntityReferenceSelection', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface', 'Drupal\Core\Entity\Annotation\EntityReferenceSelection');
   29   }
   30 
   31   /**
   32    * {@inheritdoc}
   33    */
   34   public function getInstance(array $options) {
   35     if (!isset($options['target_type'])) {
   36       throw new \InvalidArgumentException("Missing required 'target_type' property for a EntityReferenceSelection plugin.");
   37     }
   38 
   39     // Initialize default options.
   40     $options += [
   41       'handler' => $this->getPluginId($options['target_type'], 'default'),
   42     ];
   43 
   44     // A specific selection plugin ID was already specified.
   45     if (strpos($options['handler'], ':') !== FALSE) {
   46       $plugin_id = $options['handler'];
   47     }
   48     // Only a selection group name was specified.
   49     else {
   50       $plugin_id = $this->getPluginId($options['target_type'], $options['handler']);
   51     }
   52     unset($options['handler']);
   53 
   54     return $this->createInstance($plugin_id, $options);
   55   }
   56 
   57   /**
   58    * {@inheritdoc}
   59    */
   60   public function getPluginId($target_type, $base_plugin_id) {
   61     // Get all available selection plugins for this entity type.
   62     $selection_handler_groups = $this->getSelectionGroups($target_type);
   63 
   64     // Sort the selection plugins by weight and select the best match.
   65     uasort($selection_handler_groups[$base_plugin_id], ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
   66     end($selection_handler_groups[$base_plugin_id]);
   67     $plugin_id = key($selection_handler_groups[$base_plugin_id]);
   68 
   69     return $plugin_id;
   70   }
   71 
   72   /**
   73    * {@inheritdoc}
   74    */
   75   public function getSelectionGroups($entity_type_id) {
   76     $plugins = [];
   77     $definitions = $this->getDefinitions();
   78 
   79     // Do not display the 'broken' plugin in the UI.
   80     unset($definitions['broken']);
   81 
   82     foreach ($definitions as $plugin_id => $plugin) {
   83       if (empty($plugin['entity_types']) || in_array($entity_type_id, $plugin['entity_types'])) {
   84         $plugins[$plugin['group']][$plugin_id] = $plugin;
   85       }
   86     }
   87 
   88     return $plugins;
   89   }
   90 
   91   /**
   92    * {@inheritdoc}
   93    */
   94   public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
   95     $options = $field_definition->getSetting('handler_settings') ?: [];
   96     $options += [
   97       'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'),
   98       'handler' => $field_definition->getSetting('handler'),
   99       'entity' => $entity,
  100     ];
  101     return $this->getInstance($options);
  102   }
  103 
  104   /**
  105    * {@inheritdoc}
  106    */
  107   public function getFallbackPluginId($plugin_id, array $configuration = []) {
  108     return 'broken';
  109   }
  110 
  111 }