AttributeBase.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\linkit\AttributeBase.
  5. */
  6. namespace Drupal\linkit;
  7. use Drupal\Core\Plugin\PluginBase;
  8. /**
  9. * Provides a base class for attribute plugins.
  10. *
  11. * @see \Drupal\linkit\Annotation\Attribute
  12. * @see \Drupal\linkit\AttributeBase
  13. * @see \Drupal\linkit\AttributeManager
  14. * @see plugin_api
  15. */
  16. abstract class AttributeBase extends PluginBase implements AttributeInterface {
  17. /**
  18. * The weight of the attribute compared to others in an attribute collection.
  19. *
  20. * @var int
  21. */
  22. protected $weight = 0;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  27. parent::__construct($configuration, $plugin_id, $plugin_definition);
  28. $this->setConfiguration($configuration);
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getConfiguration() {
  34. return [
  35. 'id' => $this->getPluginId(),
  36. 'weight' => $this->weight,
  37. 'settings' => $this->configuration,
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function setConfiguration(array $configuration) {
  44. $configuration += [
  45. 'weight' => '0',
  46. 'settings' => [],
  47. ];
  48. $this->configuration = $configuration['settings'] + $this->defaultConfiguration();
  49. $this->weight = $configuration['weight'];
  50. return $this;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function defaultConfiguration() {
  56. return [];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function calculateDependencies() {
  62. return [];
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getLabel() {
  68. return $this->pluginDefinition['label'];
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getHtmlName() {
  74. return $this->pluginDefinition['html_name'];
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getDescription() {
  80. return $this->pluginDefinition['description'];
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getWeight() {
  86. return $this->weight;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function setWeight($weight) {
  92. $this->weight = $weight;
  93. return $this;
  94. }
  95. }