ExecutablePluginBase.php 2.7 KB

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