f8ff705bc7b975e8a9bf5947601344962ee5d2c7
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin-manager / yaml / src / ExamplePluginManager.php.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }};
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Plugin\DefaultPluginManager;
8 use Drupal\Core\Plugin\Discovery\YamlDiscovery;
9 use Drupal\Core\Plugin\Factory\ContainerFactory;
10
11 /**
12  * Defines a plugin manager to deal with {{ plugin_type|plural }}.
13  *
14  * Modules can define {{ plugin_type|plural }} in a MODULE_NAME.{{ plugin_type|plural }}.yml file contained
15  * in the module's base directory. Each {{ plugin_type }} has the following structure:
16  *
17  * @code
18  *   MACHINE_NAME:
19  *     label: STRING
20  *     description: STRING
21  * @endcode
22  *
23  * @see \Drupal\{{ machine_name }}\{{ class_prefix }}Default
24  * @see \Drupal\{{ machine_name }}\{{ class_prefix }}Interface
25  * @see plugin_api
26  */
27 class {{ class_prefix }}PluginManager extends DefaultPluginManager {
28
29   /**
30    * {@inheritdoc}
31    */
32   protected $defaults = [
33     // The {{ plugin_type }} id. Set by the plugin system based on the top-level YAML key.
34     'id' => '',
35     // The {{ plugin_type }} label.
36     'label' => '',
37     // The {{ plugin_type }} description.
38     'description' => '',
39     // Default plugin class.
40     'class' => 'Drupal\{{ machine_name }}\{{ class_prefix }}Default',
41   ];
42
43   /**
44    * Constructs {{ class_prefix }}PluginManager object.
45    *
46    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
47    *   The module handler to invoke the alter hook with.
48    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
49    *   Cache backend instance to use.
50    */
51   public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
52     $this->factory = new ContainerFactory($this);
53     $this->moduleHandler = $module_handler;
54     $this->alterInfo('{{ plugin_type }}_info');
55     $this->setCacheBackend($cache_backend, '{{ plugin_type }}_plugins');
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function getDiscovery() {
62     if (!isset($this->discovery)) {
63       $this->discovery = new YamlDiscovery('{{ plugin_type|plural }}', $this->moduleHandler->getModuleDirectories());
64       $this->discovery->addTranslatableProperty('label', 'label_context');
65       $this->discovery->addTranslatableProperty('description', 'description_context');
66     }
67     return $this->discovery;
68   }
69
70 }