3 namespace Drupal\Core\Field;
5 use Drupal\Component\Plugin\DependentPluginInterface;
6 use Drupal\Core\Plugin\PluginBase;
9 * Base class for the Field API plugins.
11 * This class handles lazy replacement of default settings values.
13 abstract class PluginSettingsBase extends PluginBase implements PluginSettingsInterface, DependentPluginInterface {
16 * The plugin settings.
20 protected $settings = [];
23 * The plugin settings injected by third party modules.
29 protected $thirdPartySettings = [];
32 * Whether default settings have been merged into the current $settings.
36 protected $defaultSettingsMerged = FALSE;
41 public static function defaultSettings() {
48 public function getSettings() {
49 // Merge defaults before returning the array.
50 if (!$this->defaultSettingsMerged) {
51 $this->mergeDefaults();
53 return $this->settings;
59 public function getSetting($key) {
60 // Merge defaults if we have no value for the key.
61 if (!$this->defaultSettingsMerged && !array_key_exists($key, $this->settings)) {
62 $this->mergeDefaults();
64 return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
68 * Merges default settings values into $settings.
70 protected function mergeDefaults() {
71 $this->settings += static::defaultSettings();
72 $this->defaultSettingsMerged = TRUE;
78 public function setSettings(array $settings) {
79 $this->settings = $settings;
80 $this->defaultSettingsMerged = FALSE;
87 public function setSetting($key, $value) {
88 $this->settings[$key] = $value;
95 public function getThirdPartySettings($module = NULL) {
97 return isset($this->thirdPartySettings[$module]) ? $this->thirdPartySettings[$module] : [];
99 return $this->thirdPartySettings;
105 public function getThirdPartySetting($module, $key, $default = NULL) {
106 return isset($this->thirdPartySettings[$module][$key]) ? $this->thirdPartySettings[$module][$key] : $default;
112 public function setThirdPartySetting($module, $key, $value) {
113 $this->thirdPartySettings[$module][$key] = $value;
120 public function unsetThirdPartySetting($module, $key) {
121 unset($this->thirdPartySettings[$module][$key]);
122 // If the third party is no longer storing any information, completely
123 // remove the array holding the settings for this module.
124 if (empty($this->thirdPartySettings[$module])) {
125 unset($this->thirdPartySettings[$module]);
133 public function getThirdPartyProviders() {
134 return array_keys($this->thirdPartySettings);
140 public function calculateDependencies() {
141 if (!empty($this->thirdPartySettings)) {
142 // Create dependencies on any modules providing third party settings.
144 'module' => array_keys($this->thirdPartySettings),
153 public function onDependencyRemoval(array $dependencies) {
155 if (!empty($this->thirdPartySettings) && !empty($dependencies['module'])) {
156 $old_count = count($this->thirdPartySettings);
157 $this->thirdPartySettings = array_diff_key($this->thirdPartySettings, array_flip($dependencies['module']));
158 $changed = $old_count != count($this->thirdPartySettings);