Skip to content
Snippets Groups Projects
Commit af53b6af authored by Mikael Meulle's avatar Mikael Meulle Committed by Pierre Dureau
Browse files

Issue #3437250 by just_like_good_vibes: Fix default value management in sources

parent 9ec32ba1
Branches
Tags
1 merge request!93Issue #3437250 Fix default value management in sources
Showing
with 97 additions and 71 deletions
......@@ -29,7 +29,7 @@ class AttributesWidget extends SourcePluginBase {
// In UI Patterns Settings, we built the Attribute object here. It is not
// possible anymore because SDC will not validate it against the prop
// type schema.
$value = parent::getPropValue();
$value = $this->getSetting('value');
if (!is_string($value)) {
return [];
}
......
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\SourcePluginBase;
use Drupal\ui_patterns\SourcePluginPropValue;
/**
* Plugin implementation of the source.
......@@ -19,7 +19,7 @@ use Drupal\ui_patterns\SourcePluginBase;
* }
* )
*/
class CheckboxWidget extends SourcePluginBase {
class CheckboxWidget extends SourcePluginPropValue {
/**
* {@inheritdoc}
......
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\SourcePluginBase;
use Drupal\ui_patterns\SourcePluginPropValue;
/**
* Plugin implementation of the source.
......@@ -19,13 +19,13 @@ use Drupal\ui_patterns\SourcePluginBase;
* }
* )
*/
class CheckboxesWidget extends SourcePluginBase {
class CheckboxesWidget extends SourcePluginPropValue {
/**
* {@inheritdoc}
*/
public function getPropValue(): mixed {
$value = $this->getSetting('value') ?? [];
$value = parent::getPropValue() ?? [];
$value = is_scalar($value) ? [$value] : $value;
return array_filter($value);
}
......
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\SourcePluginBase;
use Drupal\ui_patterns\SourcePluginPropValue;
/**
* Plugin implementation of the source.
......@@ -19,7 +19,7 @@ use Drupal\ui_patterns\SourcePluginBase;
* }
* )
*/
class ColorWidget extends SourcePluginBase {
class ColorWidget extends SourcePluginPropValue {
/**
* {@inheritdoc}
......
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\SourcePluginBase;
use Drupal\ui_patterns\SourcePluginPropValue;
/**
* Plugin implementation of the source.
......@@ -19,13 +19,13 @@ use Drupal\ui_patterns\SourcePluginBase;
* }
* )
*/
class NumberWidget extends SourcePluginBase {
class NumberWidget extends SourcePluginPropValue {
/**
* {@inheritdoc}
*/
public function getPropValue(): mixed {
$value = $this->getSetting('value');
$value = parent::getPropValue();
// Add 0 to automatically cast to a float OR an integer.
if (empty($value)) {
return $value;
......
......@@ -26,7 +26,7 @@ class PathSource extends SourcePluginBase {
* {@inheritdoc}
*/
public function getPropValue(): mixed {
$value = parent::getPropValue();
$value = $this->getSetting('value');
return $this->getUrlFromRoute($value);
}
......
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\SourcePluginBase;
use Drupal\ui_patterns\SourcePluginPropValue;
/**
* Plugin implementation of the source.
......@@ -19,14 +19,7 @@ use Drupal\ui_patterns\SourcePluginBase;
* }
* )
*/
class SelectWidget extends SourcePluginBase {
/**
* {@inheritdoc}
*/
public function getPropValue(): mixed {
return $this->getSetting('value');
}
class SelectWidget extends SourcePluginPropValue {
/**
* {@inheritdoc}
......
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\SourcePluginBase;
use Drupal\ui_patterns\SourcePluginPropValue;
/**
* Plugin implementation of the source.
......@@ -20,7 +20,7 @@ use Drupal\ui_patterns\SourcePluginBase;
* }
* )
*/
class TextfieldWidget extends SourcePluginBase {
class TextfieldWidget extends SourcePluginPropValue {
/**
* {@inheritdoc}
......
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\SourcePluginBase;
use Drupal\ui_patterns\SourcePluginPropValue;
/**
* Plugin implementation of the source.
......@@ -19,7 +19,7 @@ use Drupal\ui_patterns\SourcePluginBase;
* }
* )
*/
class UrlWidget extends SourcePluginBase {
class UrlWidget extends SourcePluginPropValue {
/**
* {@inheritdoc}
......
......@@ -19,14 +19,6 @@ interface PluginSettingsInterface {
*/
public function defaultSettings(): array;
/**
* Returns the array of settings, including defaults for missing settings.
*
* @return array
* The array of settings.
*/
public function getSettings(): array;
/**
* Returns the value of a setting, or its default value if absent.
*
......@@ -49,18 +41,6 @@ interface PluginSettingsInterface {
*/
public function setSettings(array $settings): PluginSettingsInterface;
/**
* Sets the value of a setting for the plugin.
*
* @param string $key
* The setting name.
* @param mixed $value
* The setting value.
*
* @return $this
*/
public function setSetting(string $key, mixed $value): PluginSettingsInterface;
/**
* Returns a form to configure settings for the source plugins.
*
......
......@@ -70,7 +70,7 @@ abstract class SourcePluginBase extends PluginBase implements
* {@inheritdoc}
*/
public function defaultSettings(): array {
return ['value' => $this->propDefinition['default'] ?? NULL];
return [];
}
/**
......@@ -111,17 +111,6 @@ abstract class SourcePluginBase extends PluginBase implements
return [];
}
/**
* {@inheritdoc}
*/
public function getSettings(): array {
// Merge defaults before returning the array.
if (!$this->defaultSettingsMerged) {
$this->mergeDefaults();
}
return $this->settings;
}
/**
* {@inheritdoc}
*/
......@@ -130,13 +119,14 @@ abstract class SourcePluginBase extends PluginBase implements
if (!$this->defaultSettingsMerged && !array_key_exists($key, $this->settings)) {
$this->mergeDefaults();
}
return $this->settings[$key] ?? NULL;
}
/**
* Merges default settings values into $settings.
*/
protected function mergeDefaults() {
protected function mergeDefaults() : void {
$this->settings += $this->defaultSettings();
$this->defaultSettingsMerged = TRUE;
}
......@@ -150,14 +140,6 @@ abstract class SourcePluginBase extends PluginBase implements
return $this;
}
/**
* {@inheritdoc}
*/
public function setSetting(string $key, mixed $value): PluginSettingsInterface {
$this->settings[$key] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -169,9 +151,7 @@ abstract class SourcePluginBase extends PluginBase implements
/**
* {@inheritdoc}
*/
public function getPropValue(): mixed {
return $this->getSetting('value');
}
abstract public function getPropValue(): mixed;
/**
* {@inheritdoc}
......
<?php
declare(strict_types=1);
namespace Drupal\ui_patterns;
/**
* This class covers a specific case of Source plugins.
*
* The key 'value' is used in the plugin settings (Form's settings).
* settings["value"] stores the prop value.
* the default property from the JSON schema can be used.
*/
abstract class SourcePluginPropValue extends SourcePluginBase {
/**
* {@inheritdoc}
*/
public function defaultSettings(): array {
return ["value" => $this->getDefaultFromPropDefinition()];
}
/**
* {@inheritdoc}
*/
public function getSetting(string $key): mixed {
$value = parent::getSetting($key);
if (("value" === $key) && (NULL === $value)) {
return $this->getDefaultFromPropDefinition();
}
return $value;
}
/**
* Return default value from prop definition.
*
* @return mixed
* Default value from prop definition if revelant.
*/
protected function getDefaultFromPropDefinition(): mixed {
if (is_array($this->propDefinition) &&
array_key_exists("default", $this->propDefinition)) {
return $this->propDefinition["default"];
}
return NULL;
}
/**
* Merges default settings values into $settings.
*/
protected function mergeDefaults() : void {
$defaultSettings = $this->defaultSettings();
// -> we prefer the prop definition default value.
if (array_key_exists("value", $defaultSettings)) {
$defaultValueProp = $this->getDefaultFromPropDefinition();
if (NULL !== $defaultValueProp) {
$defaultSettings["value"] = $defaultValueProp;
}
}
$this->settings += $defaultSettings;
$this->defaultSettingsMerged = TRUE;
}
/**
* {@inheritdoc}
*/
public function getPropValue(): mixed {
return $this->getSetting('value');
}
}
......@@ -26,7 +26,7 @@ final class ContextFoo extends SourcePluginBase {
/**
* {@inheritdoc}
*/
public function getData(): mixed {
public function getPropValue(): mixed {
return 'foo';
}
......
......@@ -39,7 +39,7 @@ final class Foo extends SourcePluginBase {
/**
* {@inheritdoc}
*/
public function getData(): mixed {
public function getPropValue(): mixed {
return 'foo';
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment