3c36eac51236b9c929eb75be4177183ae2dd3e3a
[yaffs-website] / web / core / lib / Drupal / Core / Executable / ExecutablePluginBase.php
1 <?php
2
3 namespace Drupal\Core\Executable;
4
5 use Drupal\Core\Plugin\ContextAwarePluginBase;
6 use Drupal\Component\Plugin\Exception\PluginException;
7
8 /**
9  * Provides the basic architecture for executable plugins.
10  */
11 abstract class ExecutablePluginBase extends ContextAwarePluginBase implements ExecutableInterface {
12
13   /**
14    * Gets an array of definitions of available configuration options.
15    *
16    * @todo: This needs to go into an interface.
17    *
18    * @return array
19    *   An array of typed data definitions describing available configuration
20    *   options, keyed by option name.
21    */
22   public function getConfigDefinitions() {
23     $definition = $this->getPluginDefinition();
24     if (!empty($definition['configuration'])) {
25       return $definition['configuration'];
26     }
27     return [];
28   }
29
30   /**
31    * Gets the definition of a configuration option.
32    *
33    * @todo: This needs to go into an interface.
34    *
35    * @return array
36    *   The typed data definition describing the configuration option, or FALSE
37    *   if the option does not exist.
38    */
39   public function getConfigDefinition($key) {
40     $definition = $this->getPluginDefinition();
41     if (!empty($definition['configuration'][$key])) {
42       return $definition['configuration'][$key];
43     }
44     return FALSE;
45   }
46
47   /**
48    * Gets all configuration values.
49    *
50    * @todo: This needs to go into an interface.
51    *
52    * @return array
53    *   The array of all configuration values, keyed by configuration option
54    *   name.
55    */
56   public function getConfig() {
57     return $this->configuration;
58   }
59
60   /**
61    * Sets the value of a particular configuration option.
62    *
63    * @param string $key
64    *   The key of the configuration option to set.
65    * @param mixed $value
66    *   The value to set.
67    *
68    * @todo This doesn't belong here. Move this into a new base class in
69    *   https://www.drupal.org/node/1764380.
70    * @todo This does not set a value in \Drupal::config(), so the name is confusing.
71    *
72    * @return \Drupal\Core\Executable\ExecutablePluginBase
73    *   The executable object for chaining.
74    */
75   public function setConfig($key, $value) {
76     if ($definition = $this->getConfigDefinition($key)) {
77       $typed_data = \Drupal::typedDataManager()->create($definition, $value);
78
79       if ($typed_data->validate()->count() > 0) {
80         throw new PluginException("The provided configuration value does not pass validation.");
81       }
82     }
83     $this->configuration[$key] = $value;
84     return $this;
85   }
86
87 }