PluginSettingsBase.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Component\Plugin\DependentPluginInterface;
  4. use Drupal\Core\Plugin\PluginBase;
  5. /**
  6. * Base class for the Field API plugins.
  7. *
  8. * This class handles lazy replacement of default settings values.
  9. */
  10. abstract class PluginSettingsBase extends PluginBase implements PluginSettingsInterface, DependentPluginInterface {
  11. /**
  12. * The plugin settings.
  13. *
  14. * @var array
  15. */
  16. protected $settings = [];
  17. /**
  18. * The plugin settings injected by third party modules.
  19. *
  20. * @see hooks
  21. *
  22. * @var array
  23. */
  24. protected $thirdPartySettings = [];
  25. /**
  26. * Whether default settings have been merged into the current $settings.
  27. *
  28. * @var bool
  29. */
  30. protected $defaultSettingsMerged = FALSE;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function defaultSettings() {
  35. return [];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getSettings() {
  41. // Merge defaults before returning the array.
  42. if (!$this->defaultSettingsMerged) {
  43. $this->mergeDefaults();
  44. }
  45. return $this->settings;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getSetting($key) {
  51. // Merge defaults if we have no value for the key.
  52. if (!$this->defaultSettingsMerged && !array_key_exists($key, $this->settings)) {
  53. $this->mergeDefaults();
  54. }
  55. return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
  56. }
  57. /**
  58. * Merges default settings values into $settings.
  59. */
  60. protected function mergeDefaults() {
  61. $this->settings += static::defaultSettings();
  62. $this->defaultSettingsMerged = TRUE;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setSettings(array $settings) {
  68. $this->settings = $settings;
  69. $this->defaultSettingsMerged = FALSE;
  70. return $this;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function setSetting($key, $value) {
  76. $this->settings[$key] = $value;
  77. return $this;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getThirdPartySettings($module = NULL) {
  83. if ($module) {
  84. return isset($this->thirdPartySettings[$module]) ? $this->thirdPartySettings[$module] : [];
  85. }
  86. return $this->thirdPartySettings;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getThirdPartySetting($module, $key, $default = NULL) {
  92. return isset($this->thirdPartySettings[$module][$key]) ? $this->thirdPartySettings[$module][$key] : $default;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function setThirdPartySetting($module, $key, $value) {
  98. $this->thirdPartySettings[$module][$key] = $value;
  99. return $this;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function unsetThirdPartySetting($module, $key) {
  105. unset($this->thirdPartySettings[$module][$key]);
  106. // If the third party is no longer storing any information, completely
  107. // remove the array holding the settings for this module.
  108. if (empty($this->thirdPartySettings[$module])) {
  109. unset($this->thirdPartySettings[$module]);
  110. }
  111. return $this;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getThirdPartyProviders() {
  117. return array_keys($this->thirdPartySettings);
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function calculateDependencies() {
  123. if (!empty($this->thirdPartySettings)) {
  124. // Create dependencies on any modules providing third party settings.
  125. return [
  126. 'module' => array_keys($this->thirdPartySettings),
  127. ];
  128. }
  129. return [];
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function onDependencyRemoval(array $dependencies) {
  135. $changed = FALSE;
  136. if (!empty($this->thirdPartySettings) && !empty($dependencies['module'])) {
  137. $old_count = count($this->thirdPartySettings);
  138. $this->thirdPartySettings = array_diff_key($this->thirdPartySettings, array_flip($dependencies['module']));
  139. $changed = $old_count != count($this->thirdPartySettings);
  140. }
  141. return $changed;
  142. }
  143. }