547724a50f6ff6ba313c7861356748f0db946fee
[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 \Drupal\Core\TypedData\DataDefinitionInterface[]
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    * @param string $key
34    *   The key of the configuration option to get.
35    *
36    * @todo: This needs to go into an interface.
37    *
38    * @return \Drupal\Core\TypedData\DataDefinitionInterface|false
39    *   The typed data definition describing the configuration option, or FALSE
40    *   if the option does not exist.
41    */
42   public function getConfigDefinition($key) {
43     $definition = $this->getPluginDefinition();
44     if (!empty($definition['configuration'][$key])) {
45       return $definition['configuration'][$key];
46     }
47     return FALSE;
48   }
49
50   /**
51    * Gets all configuration values.
52    *
53    * @todo: This needs to go into an interface.
54    *
55    * @return array
56    *   The array of all configuration values, keyed by configuration option
57    *   name.
58    */
59   public function getConfig() {
60     return $this->configuration;
61   }
62
63   /**
64    * Sets the value of a particular configuration option.
65    *
66    * @param string $key
67    *   The key of the configuration option to set.
68    * @param mixed $value
69    *   The value to set.
70    *
71    * @todo This doesn't belong here. Move this into a new base class in
72    *   https://www.drupal.org/node/1764380.
73    * @todo This does not set a value in \Drupal::config(), so the name is confusing.
74    *
75    * @return \Drupal\Core\Executable\ExecutablePluginBase
76    *   The executable object for chaining.
77    *
78    * @throws \Drupal\Component\Plugin\Exception\PluginException
79    *   If the provided configuration value does not pass validation.
80    */
81   public function setConfig($key, $value) {
82     if ($definition = $this->getConfigDefinition($key)) {
83       $typed_data = \Drupal::typedDataManager()->create($definition, $value);
84
85       if ($typed_data->validate()->count() > 0) {
86         throw new PluginException("The provided configuration value does not pass validation.");
87       }
88     }
89     $this->configuration[$key] = $value;
90     return $this;
91   }
92
93 }