3 namespace Drupal\Core\Executable;
5 use Drupal\Core\Plugin\ContextAwarePluginBase;
6 use Drupal\Component\Plugin\Exception\PluginException;
9 * Provides the basic architecture for executable plugins.
11 abstract class ExecutablePluginBase extends ContextAwarePluginBase implements ExecutableInterface {
14 * Gets an array of definitions of available configuration options.
16 * @todo: This needs to go into an interface.
18 * @return \Drupal\Core\TypedData\DataDefinitionInterface[]
19 * An array of typed data definitions describing available configuration
20 * options, keyed by option name.
22 public function getConfigDefinitions() {
23 $definition = $this->getPluginDefinition();
24 if (!empty($definition['configuration'])) {
25 return $definition['configuration'];
31 * Gets the definition of a configuration option.
34 * The key of the configuration option to get.
36 * @todo: This needs to go into an interface.
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.
42 public function getConfigDefinition($key) {
43 $definition = $this->getPluginDefinition();
44 if (!empty($definition['configuration'][$key])) {
45 return $definition['configuration'][$key];
51 * Gets all configuration values.
53 * @todo: This needs to go into an interface.
56 * The array of all configuration values, keyed by configuration option
59 public function getConfig() {
60 return $this->configuration;
64 * Sets the value of a particular configuration option.
67 * The key of the configuration option to set.
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.
75 * @return \Drupal\Core\Executable\ExecutablePluginBase
76 * The executable object for chaining.
78 * @throws \Drupal\Component\Plugin\Exception\PluginException
79 * If the provided configuration value does not pass validation.
81 public function setConfig($key, $value) {
82 if ($definition = $this->getConfigDefinition($key)) {
83 $typed_data = \Drupal::typedDataManager()->create($definition, $value);
85 if ($typed_data->validate()->count() > 0) {
86 throw new PluginException("The provided configuration value does not pass validation.");
89 $this->configuration[$key] = $value;