ExecutablePluginBase.php 2.4 KB

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