Many plugins implement \Drupal\Component\Plugin\ConfigurablePluginInterface, which allows the plugin to specify default configuration values via public function defaultConfiguration();.
Plugins should merge these defaults with the specific configuration of the plugin instance. Usually this is done in the constructor.
However this should be done from within public function setConfiguration(array $configuration);, so that any callers of that can safely specify only the keys they care about, and can assume that all expected keys will be available when calling ->getConfiguration() or using $this->configuration internally.
An example change from a core plugin base class:
+++ b/core/modules/search/src/Plugin/ConfigurableSearchPluginBase.php
@@ -23,7 +23,7 @@
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
- $this->configuration = NestedArray::mergeDeepArray([$this->defaultConfiguration(), $this->configuration], TRUE);
+ $this->setConfiguration($configuration);
}
@@ -44,7 +44,7 @@ public function getConfiguration() {
public function setConfiguration(array $configuration) {
- $this->configuration = $configuration;
+ $this->configuration = NestedArray::mergeDeepArray([$this->defaultConfiguration(), $configuration], TRUE);
}
This uses the \Drupal\Component\Utility\NestedArray::mergeDeepArray() method to ensure multidimensional arrays are properly handled while preserving integer keys.