PluginBase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Drupal\Component\Plugin;
  3. /**
  4. * Base class for plugins wishing to support metadata inspection.
  5. */
  6. abstract class PluginBase implements PluginInspectionInterface, DerivativeInspectionInterface {
  7. /**
  8. * A string which is used to separate base plugin IDs from the derivative ID.
  9. */
  10. const DERIVATIVE_SEPARATOR = ':';
  11. /**
  12. * The plugin_id.
  13. *
  14. * @var string
  15. */
  16. protected $pluginId;
  17. /**
  18. * The plugin implementation definition.
  19. *
  20. * @var array
  21. */
  22. protected $pluginDefinition;
  23. /**
  24. * Configuration information passed into the plugin.
  25. *
  26. * When using an interface like
  27. * \Drupal\Component\Plugin\ConfigurableInterface, this is where the
  28. * configuration should be stored.
  29. *
  30. * Plugin configuration is optional, so plugin implementations must provide
  31. * their own setters and getters.
  32. *
  33. * @var array
  34. */
  35. protected $configuration;
  36. /**
  37. * Constructs a \Drupal\Component\Plugin\PluginBase object.
  38. *
  39. * @param array $configuration
  40. * A configuration array containing information about the plugin instance.
  41. * @param string $plugin_id
  42. * The plugin_id for the plugin instance.
  43. * @param mixed $plugin_definition
  44. * The plugin implementation definition.
  45. */
  46. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  47. $this->configuration = $configuration;
  48. $this->pluginId = $plugin_id;
  49. $this->pluginDefinition = $plugin_definition;
  50. if ($this instanceof ConfigurablePluginInterface && !$this instanceof ConfigurableInterface) {
  51. @trigger_error('Drupal\Component\Plugin\ConfigurablePluginInterface is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. You should implement ConfigurableInterface and/or DependentPluginInterface directly as needed. If you implement ConfigurableInterface you may choose to implement ConfigurablePluginInterface in Drupal 8 as well for maximum compatibility, however this must be removed prior to Drupal 9. See https://www.drupal.org/node/2946161', E_USER_DEPRECATED);
  52. }
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getPluginId() {
  58. return $this->pluginId;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getBaseId() {
  64. $plugin_id = $this->getPluginId();
  65. if (strpos($plugin_id, static::DERIVATIVE_SEPARATOR)) {
  66. list($plugin_id) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 2);
  67. }
  68. return $plugin_id;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getDerivativeId() {
  74. $plugin_id = $this->getPluginId();
  75. $derivative_id = NULL;
  76. if (strpos($plugin_id, static::DERIVATIVE_SEPARATOR)) {
  77. list(, $derivative_id) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 2);
  78. }
  79. return $derivative_id;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getPluginDefinition() {
  85. return $this->pluginDefinition;
  86. }
  87. /**
  88. * Determines if the plugin is configurable.
  89. *
  90. * @return bool
  91. * A boolean indicating whether the plugin is configurable.
  92. */
  93. public function isConfigurable() {
  94. return $this instanceof ConfigurableInterface || $this instanceof ConfigurablePluginInterface;
  95. }
  96. }