SelectionPluginBase.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Drupal\Core\Entity\EntityReferenceSelection;
  3. use Drupal\Component\Plugin\ConfigurablePluginInterface;
  4. use Drupal\Component\Utility\NestedArray;
  5. use Drupal\Core\Database\Query\SelectInterface;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Plugin\PluginBase;
  8. /**
  9. * Provides a base class for configurable selection handlers.
  10. */
  11. abstract class SelectionPluginBase extends PluginBase implements SelectionInterface, ConfigurablePluginInterface {
  12. /**
  13. * Constructs a new selection object.
  14. *
  15. * @param array $configuration
  16. * A configuration array containing information about the plugin instance.
  17. * @param string $plugin_id
  18. * The plugin_id for the plugin instance.
  19. * @param mixed $plugin_definition
  20. * The plugin implementation definition.
  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 defaultConfiguration() {
  30. return [
  31. 'target_type' => NULL,
  32. // @todo Remove this key in Drupal 9.0.x.
  33. 'handler' => $this->getPluginId(),
  34. 'entity' => NULL,
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getConfiguration() {
  41. return $this->configuration;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function setConfiguration(array $configuration) {
  47. // Resolve backward compatibility level configurations, if any.
  48. $this->resolveBackwardCompatibilityConfiguration($configuration);
  49. // Merge in defaults.
  50. $this->configuration = NestedArray::mergeDeep(
  51. $this->defaultConfiguration(),
  52. $configuration
  53. );
  54. // Ensure a backward compatibility level configuration.
  55. $this->ensureBackwardCompatibilityConfiguration();
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function calculateDependencies() {
  61. return [];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  67. return $form;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {}
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function entityQueryAlter(SelectInterface $query) {}
  81. /**
  82. * Moves the backward compatibility level configurations in the right place.
  83. *
  84. * In order to keep backward compatibility, we copy all settings, except
  85. * 'target_type', 'handler' and 'entity' under 'handler_settings', following
  86. * the structure from the field config. If the plugin was instantiated using
  87. * the 'handler_settings' level, those values will be used. In case of
  88. * conflict, the root level settings will take precedence. The backward
  89. * compatibility aware configuration will have the next structure:
  90. * - target_type
  91. * - handler (will be removed in Drupal 9.0.x, it's the plugin id)
  92. * - entity
  93. * - setting_1
  94. * - setting_2
  95. * ...
  96. * - setting_N
  97. * - handler_settings: (will be removed in Drupal 9.0.x)
  98. * - setting_1
  99. * - setting_2
  100. * ...
  101. * - setting_N
  102. *
  103. * @param array $configuration
  104. * The configuration array to be altered.
  105. *
  106. * @deprecated Scheduled for removal in Drupal 9.0.x.
  107. *
  108. * @see https://www.drupal.org/node/2870971
  109. */
  110. protected function resolveBackwardCompatibilityConfiguration(array &$configuration) {
  111. if (isset($this->defaultConfiguration()['handler_settings'])) {
  112. throw new \InvalidArgumentException("{$this->getPluginDefinition()['class']}::defaultConfiguration() should not contain a 'handler_settings' key. All settings should be placed in the root level.");
  113. }
  114. // Extract the BC level from the passed configuration, if any.
  115. if (array_key_exists('handler_settings', $configuration)) {
  116. if (!is_array($configuration['handler_settings'])) {
  117. throw new \InvalidArgumentException("The setting 'handler_settings' is reserved and cannot be used.");
  118. }
  119. @trigger_error("Providing settings under 'handler_settings' is deprecated and will be removed before 9.0.0. Move the settings in the root of the configuration array. See https://www.drupal.org/node/2870971.", E_USER_DEPRECATED);
  120. // Settings passed in the root level take precedence over BC settings.
  121. $configuration += $configuration['handler_settings'];
  122. unset($configuration['handler_settings']);
  123. }
  124. }
  125. /**
  126. * Ensures a backward compatibility level configuration.
  127. *
  128. * @deprecated Scheduled for removal in Drupal 9.0.x.
  129. *
  130. * @see https://www.drupal.org/node/2870971
  131. */
  132. protected function ensureBackwardCompatibilityConfiguration() {
  133. $keys = ['handler', 'target_type', 'entity', 'handler_settings'];
  134. // Synchronize back 'handler_settings'.
  135. foreach ($this->configuration as $key => $value) {
  136. // Filter out keys that belong strictly to the root level.
  137. if (!in_array($key, $keys, TRUE)) {
  138. $this->configuration['handler_settings'][$key] = $value;
  139. }
  140. }
  141. }
  142. }