Skip to content
Snippets Groups Projects
Commit 8fe23f0b authored by christian.wiedemann's avatar christian.wiedemann Committed by Pierre Dureau
Browse files

Issue #3414291 by pdureau, Christian.wiedemann: Source plugins, methods naming and interfaces use

parent ef48f2df
No related branches found
No related tags found
1 merge request!68Resolve #3414291 "2.0.0 alpha1 source pluginsmethods"
Showing
with 209 additions and 162 deletions
......@@ -32,8 +32,7 @@ class ComponentBlock extends BlockBase {
* {@inheritdoc}
*/
public function build() {
$configuration = $this->getConfiguration();
$build = $this->buildComponentRenderable($configuration);
$build = $this->buildComponentRenderable();
return $build;
}
......@@ -41,15 +40,14 @@ class ComponentBlock extends BlockBase {
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$configuration = $this->getConfiguration();
return $this->buildComponentsForm($form_state, $configuration['ui_patterns'], NULL, TRUE, TRUE);
return $this->buildComponentsForm($form_state, NULL, TRUE, TRUE);
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['ui_patterns'] = $form_state->getValue('ui_patterns');
$this->submitComponentsForm($form_state);
}
}
......@@ -42,14 +42,9 @@ class ComponentLayout extends LayoutDefault implements PluginFormInterface {
* {@inheritdoc}
*/
public function build(array $regions) {
$build = parent::build($regions);
$configuration = $this->getConfiguration();
$data = $this->buildComponentRenderable($configuration, $this->getPluginId());
$build = $this->buildComponentRenderable($this->getPluginDefinition()->id());
$build['#layout'] = $this;
$build['#slots'] = $regions;
$build['#props'] = $data['#props'] ?? [];
$build['#type'] = $data['#type'];
$build['#component'] = $data['#component'];
$regions = parent::build($regions);
foreach ($this->getPluginDefinition()->getRegionNames() as $region_name) {
if (array_key_exists($region_name, $regions)) {
$build['#slots'][$region_name] = $regions[$region_name];
......@@ -70,9 +65,8 @@ class ComponentLayout extends LayoutDefault implements PluginFormInterface {
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$configuration = $this->getConfiguration();
$component_id = $this->getPluginDefinition()->id();
return $form + $this->buildComponentsForm($form_state, $configuration['ui_patterns'], $component_id, FALSE, TRUE);
return $form + $this->buildComponentsForm($form_state, $component_id, FALSE, TRUE);
}
/**
......@@ -85,7 +79,7 @@ class ComponentLayout extends LayoutDefault implements PluginFormInterface {
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state):void {
$this->configuration['ui_patterns'] = $form_state->getValue('ui_patterns');
$this->submitComponentsForm($form_state);
parent::submitConfigurationForm($form, $form_state);
}
......
......@@ -17,10 +17,9 @@ use Drupal\Core\Layout\LayoutDefinition;
* Implements hook_layout_alter().
*/
function ui_patterns_layouts_layout_alter(&$definitions) {
/** @var \Drupal\sdc\ComponentPluginManager $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.sdc');
/** @var \Drupal\sdc\Component\ComponentMetadata[] $components */
$components = $plugin_manager->getDefinitions();
$components = $plugin_manager->getSortedDefinitions();
foreach ($components as $component) {
$definition = [
'label' => $component['name'] ?? $component['id'],
......
......@@ -14,6 +14,10 @@ function ui_patterns_legacy_element_info_alter(array &$types) {
$types = _ui_patterns_legacy_clone_component_element($types, "pattern");
$types = _ui_patterns_legacy_clone_component_element($types, "pattern_preview");
}
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('ui_patterns_library')) {
array_unshift($types["pattern_preview"]['#pre_render'], 'ui_patterns_library.component_element_alter:alter');
}
}
/**
......@@ -22,10 +26,6 @@ function ui_patterns_legacy_element_info_alter(array &$types) {
function _ui_patterns_legacy_clone_component_element(array $types, string $element_id): array {
$types[$element_id] = $types['component'];
array_unshift($types[$element_id]['#pre_render'], 'ui_patterns.component_element_alter:alter');
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('ui_patterns_library')) {
array_unshift($types[$element_id]['#pre_render'], 'ui_patterns_library.component_element_alter:alter');
}
array_unshift($types[$element_id]['#pre_render'], 'ui_patterns_legacy.component_element_alter:convert');
return $types;
}
......@@ -170,7 +170,7 @@ class ComponentPluginManager extends SdcPluginManager implements CategorizingPlu
$label_key = 'name';
uasort($definitions, function ($a, $b) use ($label_key) {
if ((string) $a['group'] != (string) $b['group']) {
return strnatcasecmp($a['group'], $b['group']);
return strnatcasecmp($a['group'] ?? '', $b['group'] ?? '');
}
return strnatcasecmp($a[$label_key], $b[$label_key]);
});
......
......@@ -45,6 +45,9 @@ class ComponentElementAlter implements TrustedCallbackInterface {
elseif (is_string($slot)) {
$element['#slots'][$slot_id] = ['#markup' => $slot];
}
elseif (is_string($slot)) {
$element['#slots'][$slot_id] = ['#markup' => $slot];
}
// Because SDC validator is sometimes confused by a null slot.
if (is_null($slot)) {
......
......@@ -39,15 +39,16 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
* Build component data provided to the SDC element.
*/
public function build(array $element): array {
if (!isset($element['#configuration'])) {
if (!isset($element['#ui_patterns'])) {
return $element;
}
$configuration = $element['#configuration'];
$configuration = $element['#ui_patterns'];
$contexts = $element['#source_contexts'] ?? [];
$component = $this->componentPluginManager->find($element['#component']);
$element = $this->buildProps($element, $component, $configuration, $contexts);
$element = $this->buildSlots($element, $component, $configuration, $contexts);
return array_filter($element);
$element['#propsAlter'] = [];
return $element;
}
/**
......@@ -69,6 +70,10 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
* Add a single prop to the renderable.
*/
protected function buildProp(array $build, string $prop_id, array $definition, array $configuration, array $contexts): array {
if (isset($build["#props"][$prop_id])) {
// Keep existing props. No known use case yet.
return $build;
}
$prop_type = $definition['ui_patterns']['type_definition'];
$source_id = array_key_exists("source_id", $configuration) ? $configuration["source_id"] : $this->sourcesManager->getPropTypeDefault($prop_type->getPluginId(), $contexts);
if (!$source_id) {
......@@ -76,11 +81,7 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
}
$source = $this->sourcesManager->createInstance(
$source_id,
[
'prop_id' => $prop_id,
'prop_definition' => $definition,
'settings' => $configuration,
]
SourcePluginManager::buildPluginConfiguration($prop_id, $definition, $configuration)
);
if (!$source) {
return $build;
......@@ -122,6 +123,10 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
* Add a single slot to the renderable.
*/
protected function buildSlot(array $build, string $slot_id, array $definition, array $configuration, array $contexts): array {
if (isset($build["#slots"][$slot_id])) {
// Keep existing slots. Used by ComponentLayout for example.
return $build;
}
if (!isset($configuration["sources"])) {
return $build;
}
......@@ -130,8 +135,8 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
foreach ($configuration["sources"] as $source_configuration) {
$build = $this->addSlotSource($build, $slot_id, $definition, $source_configuration, $contexts);
}
if (count($build["#props"][$slot_id]) === 1) {
$build["#props"][$slot_id] = $build["#props"][$slot_id][0];
if (count($build["#slots"][$slot_id]) === 1) {
$build["#slots"][$slot_id] = $build["#slots"][$slot_id][0];
}
return $build;
}
......@@ -146,14 +151,10 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
}
$source = $this->sourcesManager->createInstance(
$source_id,
[
'prop_id' => $slot_id,
'prop_definition' => $definition,
'settings' => $configuration,
]
SourcePluginManager::buildPluginConfiguration($slot_id, $definition, $configuration)
);
$build = $source->alterComponent($build);
$build["#props"][$slot_id][] = $source->getData();
$build["#slots"][$slot_id][] = $source->getData();
return $build;
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\ui_patterns\Element;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
......@@ -75,6 +74,7 @@ class ComponentForm extends ComponentFormBase {
if ($input) {
$value = [
'component_id' => $input['component_id'] ?? NULL,
'variant_id' => $input['component_form']['variant_id'] ?? NULL,
'props' => $input['component_form']['props'] ?? [],
'slots' => $input['component_form']['slots'] ?? [],
];
......@@ -84,6 +84,7 @@ class ComponentForm extends ComponentFormBase {
else {
return [
'component_id' => NULL,
'variant_id' => NULL,
'props' => [],
'slots' => [],
];
......@@ -113,8 +114,7 @@ class ComponentForm extends ComponentFormBase {
$element["component_form"] = self::buildComponentForm(
$element,
$wrapper_id,
$component_id,
$form_state
$component_id
);
$element['#tree'] = TRUE;
return $element;
......@@ -127,70 +127,27 @@ class ComponentForm extends ComponentFormBase {
* The component form.
*/
private static function buildComponentForm(
&$element,
$wrapper_id,
$component_id,
FormStateInterface $form_state
array $element,
string $wrapper_id,
?string $component_id
): array {
if (!$component_id) {
return $element;
}
$form = [
'#type' => 'container',
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
];
if ($component_id !== NULL) {
$form['variant_id'] = self::buildComponentVariantSelectorForm(
$element,
$element['#default_value']['variant_id'] ?? NULL,
);
$form += self::buildComponentVariantForm(
$wrapper_id,
$element
);
}
$form['variant_id'] = self::buildComponentVariantSelectorForm(
$element,
$element['#default_value']['variant_id'] ?? NULL,
);
$form['slots'] = self::buildSlotsForm($element, $component_id);
$form['props'] = self::buildPropsForm($element, $component_id);
return $form;
}
/**
* Build the component variant form.
*
* @return array
* The component variant form.
*/
private static function buildComponentVariantForm(
$wrapper_id,
$element
): array {
$component_id = $element['#default_value']['component_id'] ?? $element['#component_id'] ?? NULL;
return [
'#type' => 'container',
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
'slots' => [
'#title' => 'Slots',
'#type' => 'component_slots_form',
'#component_id' => $component_id,
'#source_context' => $element['#source_context'],
'#ajax_url' => $element['#ajax_url'],
'#access' => $element['#render_slots'] ?? TRUE,
'#default_value' => [
'slots' => $element['#default_value']['slots'],
],
],
'props' => [
'#title' => 'Props',
'#type' => 'component_props_form',
'#component_id' => $component_id,
'#source_context' => $element['#source_context'],
'#ajax_url' => $element['#ajax_url'],
'#access' => $element['#render_props'] ?? TRUE,
'#default_value' => ['props' => $element['#default_value']['props']],
],
];
}
/**
* Build components selector widget.
*
......@@ -198,8 +155,8 @@ class ComponentForm extends ComponentFormBase {
* The component select.
*/
private static function buildComponentSelectorForm(
$wrapper_id,
$selected_component_id
?string $wrapper_id,
?string $selected_component_id
): array {
$definition_groups = \Drupal::service("plugin.manager.sdc")->getGroupedDefinitions();
$options = [];
......@@ -211,7 +168,6 @@ class ComponentForm extends ComponentFormBase {
}
$options[$definition_group_id] = $group_options;
}
return [
"#type" => "select",
"#title" => t("Component"),
......@@ -244,17 +200,46 @@ class ComponentForm extends ComponentFormBase {
foreach ($definition["variants"] as $variant_id => $variant) {
$options[$variant_id] = $variant["title"];
}
$id = strtr(Html::getId('variant_id'), '-', '_');
return self::expandAjax([
return [
"#type" => "select",
'#name' => $id,
'#id' => $id,
"#title" => t("Variant"),
"#options" => $options,
'#ui_patterns' => ['id' => 'variant_id'],
'#default_value' => $default_variant_id,
]);
];
}
/**
* Build slots form.
*/
private static function buildSlotsForm(array $element, string $component_id): array {
return [
'#title' => 'Slots',
'#type' => 'component_slots_form',
'#component_id' => $component_id,
'#source_context' => $element['#source_context'],
'#ajax_url' => $element['#ajax_url'],
'#access' => $element['#render_slots'] ?? TRUE,
'#default_value' => [
'slots' => $element['#default_value']['slots'],
],
];
}
/**
* Build props form.
*/
private static function buildPropsForm(array $element, string $component_id): array {
return [
'#title' => 'Props',
'#type' => 'component_props_form',
'#component_id' => $component_id,
'#source_context' => $element['#source_context'],
'#ajax_url' => $element['#ajax_url'],
'#access' => $element['#render_props'] ?? TRUE,
'#default_value' => [
'props' => $element['#default_value']['props'],
],
];
}
/**
......@@ -273,7 +258,7 @@ class ComponentForm extends ComponentFormBase {
/**
* Form element validation handler.
*/
public static function validateFormElement(&$element, FormStateInterface $form_state, &$complete_form) {
public static function validateFormElement(array &$element, FormStateInterface $form_state) {
$trigger_element = $form_state->getTriggeringElement();
if (isset($trigger_element['#ui_patterns']) === FALSE) {
$form_state->setValueForElement($element, $element['#value']);
......
......@@ -3,7 +3,6 @@
namespace Drupal\ui_patterns\Element;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\FormElement;
use Drupal\Core\Url;
......@@ -70,13 +69,11 @@ abstract class ComponentFormBase extends FormElement {
*
* @param array $button_base
* Button base render array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return array
* Button render array.
*/
protected static function expandComponentButton(array $button_base, FormStateInterface $form_state) {
protected static function expandComponentButton(array $button_base) {
// Do not expand elements that do not have submit handler.
if (empty($button_base['#submit'])) {
return $button_base;
......
......@@ -100,7 +100,7 @@ class ComponentPropsForm extends ComponentFormBase {
}
}
if ($selected_source) {
$element['value'] = $selected_source->settingsForm(
$element['plugin'] = $selected_source->settingsForm(
$element,
$form_state
);
......@@ -143,7 +143,7 @@ class ComponentPropsForm extends ComponentFormBase {
'effect' => 'fade',
],
];
$actions[$source_id] = self::expandComponentButton($button, $form_state);
$actions[$source_id] = self::expandComponentButton($button);
}
}
if (count($actions) === 0) {
......
......@@ -128,7 +128,7 @@ class ComponentSlotsForm extends ComponentFormBase {
$configuration['source_id'],
SourcePluginManager::buildPluginConfiguration($slot_id, $definition, $configuration)
);
$element['value'] = $source->settingsForm([], $form_state);
$element['plugin'] = $source->settingsForm([], $form_state);
$element['source_id'] = [
'#type' => 'hidden',
'#value' => $source->getPluginId(),
......@@ -173,10 +173,7 @@ class ComponentSlotsForm extends ComponentFormBase {
'#type' => 'ui_patterns_actions',
'#ui_patterns_header' => TRUE,
'dropdown_actions' => [
self::expandComponentButton(
$delete_action,
$form_state
),
self::expandComponentButton($delete_action),
],
];
}
......@@ -206,7 +203,7 @@ class ComponentSlotsForm extends ComponentFormBase {
'wrapper' => $wrapper_id,
'effect' => 'fade',
],
], $form_state);
]);
}
return self::buildComponentDropbutton($action_buttons);
}
......
......@@ -13,6 +13,32 @@ use Drupal\Core\Url;
*/
trait ComponentFormBuilderTrait {
/**
* Adapter function to get plugin configuration.
*
* Overwrite to return settings/options of the
* current plugin.
*
* @return array
* The plugin settings/options.
*/
protected function getComponentConfiguration(): array {
return $this->configuration['ui_patterns'] ?? [];
}
/**
* Adapter function to set plugin configuration.
*
* Overwrite to return settings/options of the
* current plugin.
*
* @param mixed $configuration
* The configuration to store.
*/
protected function setComponentConfiguration($configuration): void {
$this->configuration['ui_patterns'] = $configuration;
}
/**
* Get component form default.
*
......@@ -70,8 +96,6 @@ trait ComponentFormBuilderTrait {
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param array $configuration
* The stored configuration.
* @param string|null $initial_component_id
* The initial_component_id. If provided the component is changeable.
* @param bool $render_slots
......@@ -81,7 +105,6 @@ trait ComponentFormBuilderTrait {
*/
protected function buildComponentsForm(
FormStateInterface $form_state,
array $configuration,
string $initial_component_id = NULL,
bool $render_slots = TRUE,
bool $render_props = TRUE
......@@ -93,21 +116,33 @@ trait ComponentFormBuilderTrait {
'#component_id' => $initial_component_id,
'#ajax_url' => $this->getAjaxUrl($form_state),
'#source_context' => $this->getComponentSourceContexts(),
'#default_value' => $configuration,
'#default_value' => $this->getComponentConfiguration(),
'#render_slots' => $render_slots,
'#render_props' => $render_props,
],
];
}
/**
* Submit the component form.
*
* The form contains.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function submitComponentsForm(FormStateInterface $form_state) {
$this->setComponentConfiguration($form_state->getValue('ui_patterns'));
}
/**
* Build component renderable (a SDC render element).
*/
public function buildCompnentRenderable(array $configuration, $component_id = NULL) {
public function buildComponentRenderable($component_id = NULL) {
return [
'#component' => $component_id ?? $configuration['component_id'],
'#type' => 'component',
'#ui_patterns' => $configuration,
'#component' => $component_id ?? $this->getComponentConfiguration()['component_id'],
'#ui_patterns' => $this->getComponentConfiguration(),
];
}
......
......@@ -14,7 +14,7 @@ trait ComponentSettingsFormBuilderTrait {
use ComponentFormBuilderTrait;
/**
* PropTypeAdapter function for plugin settings/options.
* Adapter function for plugin settings/options.
*
* Overwrite to return settings/options of the
* current plugin.
......@@ -24,6 +24,13 @@ trait ComponentSettingsFormBuilderTrait {
*/
abstract protected function getComponentSettings(): array;
/**
* {@inheritdoc}
*/
protected function getComponentConfiguration(): array {
return $this->getComponentSettings()['ui_patterns'] ?? [];
}
/**
* {@inheritdoc}
*/
......@@ -31,8 +38,7 @@ trait ComponentSettingsFormBuilderTrait {
array $form,
FormStateInterface $form_state
): array {
$configuration = $this->getComponentSettings()['ui_patterns'] ?? [];
return $this->buildComponentsForm($form_state, $configuration);
return $this->buildComponentsForm($form_state);
}
}
......@@ -43,7 +43,7 @@ class AttributesWidget extends SourcePluginBase {
// Attributes are associative arrays, but this source plugin is storing
// them as string in config.
// It would be better to use something else than a textfield one day.
$form = [
$form['value'] = [
'#type' => 'textfield',
'#title' => $this->propDefinition['title'],
'#default_value' => $this->getSetting('value'),
......
......@@ -38,10 +38,12 @@ class ChecboxesWidget extends SourcePluginBase {
}
// @todo max & min
return [
'#type' => 'checkboxes',
'#title' => $this->propDefinition['title'],
'#default_value' => $this->getSetting('value') ?? [],
"#options" => $options,
'value' => [
'#type' => 'checkboxes',
'#title' => $this->propDefinition['title'],
'#default_value' => $this->getSetting('value') ?? [],
"#options" => $options,
],
];
}
......
......@@ -33,9 +33,11 @@ class CheckboxWidget extends SourcePluginBase {
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
return [
'#type' => 'checkbox',
'#title' => $this->propDefinition['title'],
'#default_value' => $this->getSetting('value'),
'value' => [
'#type' => 'checkbox',
'#title' => $this->propDefinition['title'],
'#default_value' => $this->getSetting('value'),
],
];
}
......
......@@ -26,9 +26,11 @@ class ColorWidget extends SourcePluginBase {
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
return [
'#type' => 'color',
'#title' => $this->propDefinition['title'],
'#default_value' => $this->getSetting('value'),
'value' => [
'#type' => 'color',
'#title' => $this->propDefinition['title'],
'#default_value' => $this->getSetting('value'),
],
];
}
......
......@@ -37,7 +37,7 @@ class ListTextareaWidget extends SourcePluginBase {
if (is_array($items)) {
$items = implode("\r", $items);
}
$form = [
$form['value'] = [
'#type' => 'textarea',
'#title' => $this->propDefinition['title'],
'#default_value' => $items,
......
......@@ -55,44 +55,67 @@ class MenuSource extends SourcePluginBase {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
$plugin = parent::create(
$container,
$configuration,
$plugin_id,
$plugin_definition
);
$plugin->menuLinkTree = $container->get('menu.link_tree');
$plugin->entityTypeManager = $container->get('entity_type.manager');
$plugin->moduleHandler = $container->get('module_handler');
return $plugin;
}
/**
* {@inheritdoc}
*/
public function defaultSettings() {
return [
'menu' => NULL,
'level' => 1,
'depth' => 0,
];
}
/**
* {@inheritdoc}
*/
public function getData(): mixed {
$value = parent::getData();
if (!$value) {
$menu_id = $this->getSetting('menu');
if (!$menu_id) {
return [];
}
$this->menuId = $value["menu"];
return $this->getMenuItems($value);
$this->menuId = $menu_id;
return $this->getMenuItems();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$value = $this->getSetting('value') ?? [];
public function settingsForm(
array $form,
FormStateInterface $form_state
): array {
$form = [];
$form["menu"] = [
'#type' => 'select',
'#title' => $this->propDefinition['title'] . ": " . $this->t("Menu"),
'#options' => $this->getMenuList(),
'#default_value' => \array_key_exists("menu", $value) ? $value["menu"] : "",
'#default_value' => $this->getSetting('menu'),
];
$options = range(0, $this->menuLinkTree->maxDepth());
unset($options[0]);
$form['level'] = [
'#type' => 'select',
'#title' => $this->t('Initial visibility level'),
'#default_value' => \array_key_exists("level", $value) ? $value["level"] : 1,
'#default_value' => $this->getSetting('level'),
'#options' => $options,
'#required' => TRUE,
];
......@@ -100,13 +123,14 @@ class MenuSource extends SourcePluginBase {
$form['depth'] = [
'#type' => 'select',
'#title' => $this->t('Number of levels to display'),
'#default_value' => \array_key_exists("depth", $value) ? $value["depth"] : 0,
'#default_value' => $this->getSetting('depth'),
'#options' => $options,
'#description' => $this->t('This maximum number includes the initial level and the final display is dependant of the pattern template.'),
'#description' => $this->t(
'This maximum number includes the initial level and the final display is dependant of the pattern template.'
),
'#required' => TRUE,
];
return $form;
}
/**
......@@ -133,10 +157,10 @@ class MenuSource extends SourcePluginBase {
* @return array
* List of items.
*/
private function getMenuItems($value): array {
private function getMenuItems(): array {
$menuLinkTree = $this->menuLinkTree;
$level = (int) \array_key_exists("level", $value) ? $value["level"] : 1;
$depth = (int) \array_key_exists("depth", $value) ? $value["depth"] : 0;
$level = (int) $this->getSetting('level');
$depth = (int) $this->getSetting('depth');
$parameters = new MenuTreeParameters();
$parameters->setMinDepth($level);
......@@ -145,7 +169,9 @@ class MenuSource extends SourcePluginBase {
// Hence this is a relative depth that we must convert to an actual
// (absolute) depth, that may never exceed the maximum depth.
if ($depth > 0) {
$parameters->setMaxDepth(min($level + $depth - 1, $menuLinkTree->maxDepth()));
$parameters->setMaxDepth(
min($level + $depth - 1, $menuLinkTree->maxDepth())
);
}
$tree = $menuLinkTree->load($this->menuId, $parameters);
......
......@@ -37,20 +37,20 @@ class NumberWidget extends SourcePluginBase {
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$form = [
$form['value'] = [
'#type' => 'number',
'#title' => $this->propDefinition['title'],
'#default_value' => $this->getSetting('value'),
'#step' => 0.01,
];
if ($this->propDefinition["type"] === "integer") {
$form['#step'] = 1;
$form['value']['#step'] = 1;
}
if (isset($this->propDefinition["minimum"])) {
$form['#min'] = $this->propDefinition["minimum"];
$form['value']['#min'] = $this->propDefinition["minimum"];
}
if (isset($this->propDefinition["maximum"])) {
$form['#max'] = $this->propDefinition["maximum"];
$form['value']['#max'] = $this->propDefinition["maximum"];
}
return $form;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment