More tidying.
[yaffs-website] / web / modules / contrib / video_embed_field / src / ProviderManager.php
1 <?php
2
3 namespace Drupal\video_embed_field;
4
5 use Drupal\Component\Plugin\Mapper\MapperInterface;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Plugin\DefaultPluginManager;
9
10 /**
11  * Gathers the provider plugins.
12  */
13 class ProviderManager extends DefaultPluginManager implements ProviderManagerInterface, MapperInterface {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
19     parent::__construct('Plugin/video_embed_field/Provider', $namespaces, $module_handler, 'Drupal\video_embed_field\ProviderPluginInterface', 'Drupal\video_embed_field\Annotation\VideoEmbedProvider');
20     $this->alterInfo('video_embed_field_provider_info');
21   }
22
23   /**
24    * {@inheritdoc}
25    */
26   public function getProvidersOptionList() {
27     $options = [];
28     foreach ($this->getDefinitions() as $definition) {
29       $options[$definition['id']] = $definition['title'];
30     }
31     return $options;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function loadDefinitionsFromOptionList($options) {
38     $definitions = [];
39     // When no options are selected, all plugins are applicable.
40     if (count(array_keys($options, '0')) == count($options) || empty($options)) {
41       return $this->getDefinitions();
42     }
43     else {
44       foreach ($options as $provider_id => $enabled) {
45         if ($enabled) {
46           $definitions[$provider_id] = $this->getDefinition($provider_id);
47         }
48       }
49     }
50     return $definitions;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function filterApplicableDefinitions(array $definitions, $user_input) {
57     foreach ($definitions as $definition) {
58       $is_applicable = $definition['class']::isApplicable($user_input);
59       if ($is_applicable) {
60         return $definition;
61       }
62     }
63     return FALSE;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function loadProviderFromInput($input) {
70     $definition = $this->loadDefinitionFromInput($input);
71     return $definition ? $this->createInstance($definition['id'], ['input' => $input]) : FALSE;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function loadDefinitionFromInput($input) {
78     return $this->filterApplicableDefinitions($this->getDefinitions(), $input);
79   }
80
81 }