comparison core/lib/Drupal/Core/Field/PluginSettingsBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Field;
4
5 use Drupal\Component\Plugin\DependentPluginInterface;
6 use Drupal\Core\Plugin\PluginBase;
7
8 /**
9 * Base class for the Field API plugins.
10 *
11 * This class handles lazy replacement of default settings values.
12 */
13 abstract class PluginSettingsBase extends PluginBase implements PluginSettingsInterface, DependentPluginInterface {
14
15 /**
16 * The plugin settings.
17 *
18 * @var array
19 */
20 protected $settings = [];
21
22 /**
23 * The plugin settings injected by third party modules.
24 *
25 * @see hooks
26 *
27 * @var array
28 */
29 protected $thirdPartySettings = [];
30
31 /**
32 * Whether default settings have been merged into the current $settings.
33 *
34 * @var bool
35 */
36 protected $defaultSettingsMerged = FALSE;
37
38 /**
39 * {@inheritdoc}
40 */
41 public static function defaultSettings() {
42 return [];
43 }
44
45 /**
46 * {@inheritdoc}
47 */
48 public function getSettings() {
49 // Merge defaults before returning the array.
50 if (!$this->defaultSettingsMerged) {
51 $this->mergeDefaults();
52 }
53 return $this->settings;
54 }
55
56 /**
57 * {@inheritdoc}
58 */
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();
63 }
64 return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
65 }
66
67 /**
68 * Merges default settings values into $settings.
69 */
70 protected function mergeDefaults() {
71 $this->settings += static::defaultSettings();
72 $this->defaultSettingsMerged = TRUE;
73 }
74
75 /**
76 * {@inheritdoc}
77 */
78 public function setSettings(array $settings) {
79 $this->settings = $settings;
80 $this->defaultSettingsMerged = FALSE;
81 return $this;
82 }
83
84 /**
85 * {@inheritdoc}
86 */
87 public function setSetting($key, $value) {
88 $this->settings[$key] = $value;
89 return $this;
90 }
91
92 /**
93 * {@inheritdoc}
94 */
95 public function getThirdPartySettings($module = NULL) {
96 if ($module) {
97 return isset($this->thirdPartySettings[$module]) ? $this->thirdPartySettings[$module] : [];
98 }
99 return $this->thirdPartySettings;
100 }
101
102 /**
103 * {@inheritdoc}
104 */
105 public function getThirdPartySetting($module, $key, $default = NULL) {
106 return isset($this->thirdPartySettings[$module][$key]) ? $this->thirdPartySettings[$module][$key] : $default;
107 }
108
109 /**
110 * {@inheritdoc}
111 */
112 public function setThirdPartySetting($module, $key, $value) {
113 $this->thirdPartySettings[$module][$key] = $value;
114 return $this;
115 }
116
117 /**
118 * {@inheritdoc}
119 */
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]);
126 }
127 return $this;
128 }
129
130 /**
131 * {@inheritdoc}
132 */
133 public function getThirdPartyProviders() {
134 return array_keys($this->thirdPartySettings);
135 }
136
137 /**
138 * {@inheritdoc}
139 */
140 public function calculateDependencies() {
141 if (!empty($this->thirdPartySettings)) {
142 // Create dependencies on any modules providing third party settings.
143 return [
144 'module' => array_keys($this->thirdPartySettings)
145 ];
146 }
147 return [];
148 }
149
150 /**
151 * {@inheritdoc}
152 */
153 public function onDependencyRemoval(array $dependencies) {
154 $changed = FALSE;
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);
159 }
160 return $changed;
161 }
162
163 }