PluginSettingsBase.php in Drupal 8
File
core/lib/Drupal/Core/Field/PluginSettingsBase.php
View source
<?php
namespace Drupal\Core\Field;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Plugin\PluginBase;
abstract class PluginSettingsBase extends PluginBase implements PluginSettingsInterface, DependentPluginInterface {
protected $settings = [];
protected $thirdPartySettings = [];
protected $defaultSettingsMerged = FALSE;
public static function defaultSettings() {
return [];
}
public function getSettings() {
if (!$this->defaultSettingsMerged) {
$this
->mergeDefaults();
}
return $this->settings;
}
public function getSetting($key) {
if (!$this->defaultSettingsMerged && !array_key_exists($key, $this->settings)) {
$this
->mergeDefaults();
}
return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
}
protected function mergeDefaults() {
$this->settings += static::defaultSettings();
$this->defaultSettingsMerged = TRUE;
}
public function setSettings(array $settings) {
$this->settings = $settings;
$this->defaultSettingsMerged = FALSE;
return $this;
}
public function setSetting($key, $value) {
$this->settings[$key] = $value;
return $this;
}
public function getThirdPartySettings($module = NULL) {
if ($module) {
return isset($this->thirdPartySettings[$module]) ? $this->thirdPartySettings[$module] : [];
}
return $this->thirdPartySettings;
}
public function getThirdPartySetting($module, $key, $default = NULL) {
return isset($this->thirdPartySettings[$module][$key]) ? $this->thirdPartySettings[$module][$key] : $default;
}
public function setThirdPartySetting($module, $key, $value) {
$this->thirdPartySettings[$module][$key] = $value;
return $this;
}
public function unsetThirdPartySetting($module, $key) {
unset($this->thirdPartySettings[$module][$key]);
if (empty($this->thirdPartySettings[$module])) {
unset($this->thirdPartySettings[$module]);
}
return $this;
}
public function getThirdPartyProviders() {
return array_keys($this->thirdPartySettings);
}
public function calculateDependencies() {
if (!empty($this->thirdPartySettings)) {
return [
'module' => array_keys($this->thirdPartySettings),
];
}
return [];
}
public function onDependencyRemoval(array $dependencies) {
$changed = FALSE;
if (!empty($this->thirdPartySettings) && !empty($dependencies['module'])) {
$old_count = count($this->thirdPartySettings);
$this->thirdPartySettings = array_diff_key($this->thirdPartySettings, array_flip($dependencies['module']));
$changed = $old_count != count($this->thirdPartySettings);
}
return $changed;
}
}