VariantBase.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Drupal\Core\Display;
  3. use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Plugin\PluginBase;
  6. use Drupal\Core\Plugin\PluginDependencyTrait;
  7. use Drupal\Core\Session\AccountInterface;
  8. /**
  9. * Provides a base class for DisplayVariant plugins.
  10. *
  11. * @see \Drupal\Core\Display\Annotation\DisplayVariant
  12. * @see \Drupal\Core\Display\VariantInterface
  13. * @see \Drupal\Core\Display\VariantManager
  14. * @see plugin_api
  15. */
  16. abstract class VariantBase extends PluginBase implements VariantInterface {
  17. use PluginDependencyTrait;
  18. use RefinableCacheableDependencyTrait;
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  23. parent::__construct($configuration, $plugin_id, $plugin_definition);
  24. $this->setConfiguration($configuration);
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function label() {
  30. return $this->configuration['label'];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function adminLabel() {
  36. return $this->pluginDefinition['admin_label'];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function id() {
  42. return $this->configuration['uuid'];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getWeight() {
  48. return (int) $this->configuration['weight'];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function setWeight($weight) {
  54. $this->configuration['weight'] = (int) $weight;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getConfiguration() {
  60. return [
  61. 'id' => $this->getPluginId(),
  62. ] + $this->configuration;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setConfiguration(array $configuration) {
  68. $this->configuration = $configuration + $this->defaultConfiguration();
  69. return $this;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function defaultConfiguration() {
  75. return [
  76. 'label' => '',
  77. 'uuid' => '',
  78. 'weight' => 0,
  79. ];
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function calculateDependencies() {
  85. return $this->dependencies;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  91. $form['label'] = [
  92. '#type' => 'textfield',
  93. '#title' => $this->t('Label'),
  94. '#description' => $this->t('The label for this display variant.'),
  95. '#default_value' => $this->label(),
  96. '#maxlength' => '255',
  97. ];
  98. return $form;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  109. $this->configuration['label'] = $form_state->getValue('label');
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function access(AccountInterface $account = NULL) {
  115. return TRUE;
  116. }
  117. }