3 namespace Drupal\Core\Entity\EntityReferenceSelection;
5 use Drupal\Component\Plugin\ConfigurablePluginInterface;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Database\Query\SelectInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\PluginBase;
12 * Provides a base class for configurable selection handlers.
14 abstract class SelectionPluginBase extends PluginBase implements SelectionInterface, ConfigurablePluginInterface {
17 * Constructs a new selection object.
19 * @param array $configuration
20 * A configuration array containing information about the plugin instance.
21 * @param string $plugin_id
22 * The plugin_id for the plugin instance.
23 * @param mixed $plugin_definition
24 * The plugin implementation definition.
26 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
27 parent::__construct($configuration, $plugin_id, $plugin_definition);
28 $this->setConfiguration($configuration);
34 public function defaultConfiguration() {
36 'target_type' => NULL,
37 // @todo Remove this key in Drupal 9.0.x.
38 'handler' => $this->getPluginId(),
46 public function getConfiguration() {
47 return $this->configuration;
53 public function setConfiguration(array $configuration) {
54 // Resolve backward compatibility level configurations, if any.
55 $this->resolveBackwardCompatibilityConfiguration($configuration);
58 $this->configuration = NestedArray::mergeDeep(
59 $this->defaultConfiguration(),
63 // Ensure a backward compatibility level configuration.
64 $this->ensureBackwardCompatibilityConfiguration();
70 public function calculateDependencies() {
77 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
84 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
89 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {}
94 public function entityQueryAlter(SelectInterface $query) {}
97 * Moves the backward compatibility level configurations in the right place.
99 * In order to keep backward compatibility, we copy all settings, except
100 * 'target_type', 'handler' and 'entity' under 'handler_settings', following
101 * the structure from the field config. If the plugin was instantiated using
102 * the 'handler_settings' level, those values will be used. In case of
103 * conflict, the root level settings will take precedence. The backward
104 * compatibility aware configuration will have the next structure:
106 * - handler (will be removed in Drupal 9.0.x, it's the plugin id)
112 * - handler_settings: (will be removed in Drupal 9.0.x)
118 * @param array $configuration
119 * The configuration array to be altered.
121 * @deprecated Scheduled for removal in Drupal 9.0.x.
123 * @see https://www.drupal.org/node/2870971
125 protected function resolveBackwardCompatibilityConfiguration(array &$configuration) {
126 if (isset($this->defaultConfiguration()['handler_settings'])) {
127 throw new \InvalidArgumentException("{$this->getPluginDefinition()['class']}::defaultConfiguration() should not contain a 'handler_settings' key. All settings should be placed in the root level.");
130 // Extract the BC level from the passed configuration, if any.
131 if (array_key_exists('handler_settings', $configuration)) {
132 if (!is_array($configuration['handler_settings'])) {
133 throw new \InvalidArgumentException("The setting 'handler_settings' is reserved and cannot be used.");
135 @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);
137 // Settings passed in the root level take precedence over BC settings.
138 $configuration += $configuration['handler_settings'];
139 unset($configuration['handler_settings']);
144 * Ensures a backward compatibility level configuration.
146 * @deprecated Scheduled for removal in Drupal 9.0.x.
148 * @see https://www.drupal.org/node/2870971
150 protected function ensureBackwardCompatibilityConfiguration() {
151 $keys = ['handler', 'target_type', 'entity', 'handler_settings'];
152 // Synchronize back 'handler_settings'.
153 foreach ($this->configuration as $key => $value) {
154 // Filter out keys that belong strictly to the root level.
155 if (!in_array($key, $keys, TRUE)) {
156 $this->configuration['handler_settings'][$key] = $value;