Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityReferenceSelection / SelectionPluginBase.php
1 <?php
2
3 namespace Drupal\Core\Entity\EntityReferenceSelection;
4
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;
10
11 /**
12  * Provides a base class for configurable selection handlers.
13  */
14 abstract class SelectionPluginBase extends PluginBase implements SelectionInterface, ConfigurablePluginInterface {
15
16   /**
17    * Constructs a new selection object.
18    *
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.
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   /**
32    * {@inheritdoc}
33    */
34   public function defaultConfiguration() {
35     return [
36       'target_type' => NULL,
37       // @todo Remove this key in Drupal 9.0.x.
38       'handler' => $this->getPluginId(),
39       'entity' => NULL,
40     ];
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getConfiguration() {
47     return $this->configuration;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function setConfiguration(array $configuration) {
54     // Resolve backward compatibility level configurations, if any.
55     $this->resolveBackwardCompatibilityConfiguration($configuration);
56
57     // Merge in defaults.
58     $this->configuration = NestedArray::mergeDeep(
59       $this->defaultConfiguration(),
60       $configuration
61     );
62
63     // Ensure a backward compatibility level configuration.
64     $this->ensureBackwardCompatibilityConfiguration();
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function calculateDependencies() {
71     return [];
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
78     return $form;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
85
86   /**
87    * {@inheritdoc}
88    */
89   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {}
90
91   /**
92    * {@inheritdoc}
93    */
94   public function entityQueryAlter(SelectInterface $query) {}
95
96   /**
97    * Moves the backward compatibility level configurations in the right place.
98    *
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:
105    * - target_type
106    * - handler (will be removed in Drupal 9.0.x, it's the plugin id)
107    * - entity
108    * - setting_1
109    * - setting_2
110    *   ...
111    * - setting_N
112    * - handler_settings: (will be removed in Drupal 9.0.x)
113    *   - setting_1
114    *   - setting_2
115    *     ...
116    *   - setting_N
117    *
118    * @param array $configuration
119    *   The configuration array to be altered.
120    *
121    * @deprecated Scheduled for removal in Drupal 9.0.x.
122    *
123    * @see https://www.drupal.org/node/2870971
124    */
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.");
128     }
129
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.");
134       }
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);
136
137       // Settings passed in the root level take precedence over BC settings.
138       $configuration += $configuration['handler_settings'];
139       unset($configuration['handler_settings']);
140     }
141   }
142
143   /**
144    * Ensures a backward compatibility level configuration.
145    *
146    * @deprecated Scheduled for removal in Drupal 9.0.x.
147    *
148    * @see https://www.drupal.org/node/2870971
149    */
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;
157       }
158     }
159   }
160
161 }