Skip to content

Commit 18f35a6

Browse files
committed
feat: add possibility to edit the dialog box configuration of an areabrick
- Introduced `DialogBoxConfigurator` interface for custom dialog box configurations. - Enabled setting min/max constraints on `NumericItem` with validation. - Allowed dynamic store updates in `SelectItem` via `setStore`. - Enhanced `PanelItem` and `TabPanelItem` to manage editable items and tabs, including removal and access. - Added `HasDialogBox` functionality for integrating and configuring dialog boxes via container-registered configurators. - Expanded `DialogBoxBuilder` with richer tab and content management capabilities.
1 parent 4b1ff26 commit 18f35a6

File tree

8 files changed

+203
-4
lines changed

8 files changed

+203
-4
lines changed

src/DialogBoxBuilder.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ public function addContent(EditableItem ...$items): static
7070
return $this;
7171
}
7272

73+
public function hasContent(): bool
74+
{
75+
return isset($this->content);
76+
}
77+
78+
public function getContent(): PanelItem
79+
{
80+
if (!isset($this->content)) {
81+
throw new \LogicException('You cannot get content without adding content first.');
82+
}
83+
84+
return $this->content;
85+
}
86+
7387
/**
7488
* @return $this
7589
*/
@@ -85,6 +99,20 @@ public function addTab(string $title, EditableItem ...$items): static
8599
return $this;
86100
}
87101

102+
public function hasTab(string $title): bool
103+
{
104+
return isset($this->tabs) && $this->tabs->hasTab($title);
105+
}
106+
107+
public function getTab(string $title): PanelItem
108+
{
109+
if (!isset($this->tabs)) {
110+
throw new \LogicException('You cannot get a tab without adding tabs first.');
111+
}
112+
113+
return $this->tabs->getTab($title);
114+
}
115+
88116
public function createCheckbox(string $name): CheckboxItem
89117
{
90118
return new CheckboxItem($name);

src/DialogBoxConfigurator.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Neusta\Pimcore\AreabrickConfigBundle;
5+
6+
use Pimcore\Model\Document\Editable;
7+
use Pimcore\Model\Document\Editable\Area\Info;
8+
9+
/**
10+
* Allows configuring a {@see DialogBoxBuilder} instance.
11+
*
12+
* This is useful if you want to customize the default configuration,
13+
* for example, if the areabrick is used within the area of another areabrick.
14+
* In this case, you may, for example, want to change the default values of an item
15+
* or the possible values of a select item.
16+
*
17+
* ```
18+
* namespace App\Areabrick;
19+
*
20+
* use Neusta\Pimcore\AreabrickConfigBundle\DialogBoxBuilder;
21+
* use Neusta\Pimcore\AreabrickConfigBundle\DialogBoxConfigurator;
22+
* use Pimcore\Model\Document\Editable;
23+
* use Pimcore\Model\Document\Editable\Area\Info;
24+
*
25+
* final class MyAreabrickDialogBoxConfigurator implements DialogBoxConfigurator
26+
* {
27+
* public function configureDialogBox(DialogBoxBuilder $dialogBox, Editable $area, ?Info $info): void
28+
* {
29+
* $dialogBox->height(500);
30+
*
31+
* $dialogBox->getTab('General')
32+
* ->getEditableItem('my-select')
33+
* ->setStore([
34+
* 'option1' => 'Option 1',
35+
* 'option2' => 'Option 2',
36+
* ])
37+
* ->setDefaultValue('option2');
38+
*
39+
* $dialogBox->reloadOnClose(false);
40+
* }
41+
* }
42+
* ```
43+
*
44+
* After registering the configurator with its FQCN in the container,
45+
* the configurator can be used like this:
46+
*
47+
* ```
48+
* {{ pimcore_area('myfield', {
49+
* type: 'my-areabrick',
50+
* params: {
51+
* 'my-areabrick': {
52+
* dialogBoxConfigurator: 'App\Areabrick\MyAreabrickDialogBoxConfigurator',
53+
* }
54+
* },
55+
* }) }}
56+
* ```
57+
*/
58+
interface DialogBoxConfigurator
59+
{
60+
public function configureDialogBox(DialogBoxBuilder $dialogBox, Editable $area, ?Info $info): void;
61+
}

src/EditableDialogBox/EditableItem/NumericItem.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,42 @@ class NumericItem extends EditableItem
99
{
1010
private int $min;
1111
private int $max;
12+
private int $default;
1213

1314
public function __construct(string $name, int $min, int $max)
1415
{
1516
parent::__construct('numeric', $name);
1617
$this->min = $min;
1718
$this->max = $max;
18-
$this->setDefaultValue($this->min);
19+
$this->default = $min;
20+
}
21+
22+
/**
23+
* @return $this
24+
*/
25+
public function setMin(int $min): static
26+
{
27+
$this->min = $min;
28+
29+
if ($this->default < $min) {
30+
$this->default = $min;
31+
}
32+
33+
return $this;
34+
}
35+
36+
/**
37+
* @return $this
38+
*/
39+
public function setMax(int $max): static
40+
{
41+
$this->max = $max;
42+
43+
if ($this->default > $max) {
44+
$this->default = $max;
45+
}
46+
47+
return $this;
1948
}
2049

2150
/**
@@ -27,14 +56,17 @@ public function setDefaultValue(int $value): static
2756
throw new \InvalidArgumentException(\sprintf('Default value "%d" is out of bounds: [%d,%d]', $value, $this->min, $this->max));
2857
}
2958

30-
return $this->addConfig('defaultValue', (string) $value);
59+
$this->default = $value;
60+
61+
return $this;
3162
}
3263

3364
protected function getConfig(): array
3465
{
3566
return [
3667
'minValue' => $this->min,
3768
'maxValue' => $this->max,
69+
'defaultValue' => (string) $this->default,
3870
];
3971
}
4072
}

src/EditableDialogBox/EditableItem/SelectItem.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@ class SelectItem extends EditableItem
1616
public function __construct(string $name, array $store)
1717
{
1818
parent::__construct('select', $name);
19+
$this->setStore($store);
20+
}
21+
22+
/**
23+
* @param non-empty-array<array-key, string> $store
24+
*
25+
* @return $this
26+
*/
27+
public function setStore(array $store): static
28+
{
1929
$this->store = self::pack($store);
20-
$this->setDefaultValue(array_key_first($store));
30+
31+
return $this->setDefaultValue(array_key_first($store));
2132
}
2233

2334
/**

src/EditableDialogBox/LayoutItem.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ protected function addItem(DialogBoxItem $item): static
4646
return $this;
4747
}
4848

49+
/**
50+
* @return $this
51+
*/
52+
protected function removeItem(DialogBoxItem $item): static
53+
{
54+
unset($this->items[spl_object_id($item)]);
55+
56+
return $this;
57+
}
58+
4959
protected function getAttributes(): array
5060
{
5161
$items = [];

src/EditableDialogBox/LayoutItem/PanelItem.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Neusta\Pimcore\AreabrickConfigBundle\EditableDialogBox\LayoutItem;
55

66
use Neusta\Pimcore\AreabrickConfigBundle\EditableDialogBox\DialogBoxItem;
7+
use Neusta\Pimcore\AreabrickConfigBundle\EditableDialogBox\EditableItem;
78
use Neusta\Pimcore\AreabrickConfigBundle\EditableDialogBox\LayoutItem;
89

910
/**
@@ -13,24 +14,49 @@ class PanelItem extends LayoutItem
1314
{
1415
public readonly string $title;
1516

17+
/** @var array<string, EditableItem> */
18+
private array $editableItems;
19+
1620
/**
1721
* @param list<DialogBoxItem> $items
1822
*/
1923
public function __construct(string $title, array $items = [])
2024
{
21-
parent::__construct('panel', $items);
25+
parent::__construct('panel', []);
2226
$this->title = $title;
27+
$this->addItem(...$items);
2328
}
2429

2530
public function addItem(DialogBoxItem ...$items): static
2631
{
2732
foreach ($items as $item) {
2833
parent::addItem($item);
34+
35+
if ($item instanceof EditableItem) {
36+
$this->editableItems[$item->name()] = $item;
37+
}
2938
}
3039

3140
return $this;
3241
}
3342

43+
public function getEditableItem(string $name): EditableItem
44+
{
45+
return $this->editableItems[$name]
46+
?? throw new \InvalidArgumentException(\sprintf('Editable item with name "%s" not found.', $name));
47+
}
48+
49+
public function removeEditableItem(string $name): static
50+
{
51+
if (!$item = $this->editableItems[$name] ?? null) {
52+
throw new \InvalidArgumentException(\sprintf('Editable item with name "%s" not found.', $name));
53+
}
54+
55+
unset($this->editableItems[$name]);
56+
57+
return $this->removeItem($item);
58+
}
59+
3460
protected function getAttributes(): array
3561
{
3662
return ['title' => $this->title] + parent::getAttributes();

src/EditableDialogBox/LayoutItem/TabPanelItem.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,26 @@ public function getOrCreateTab(string $title): PanelItem
3030

3131
return $this->items[$title];
3232
}
33+
34+
public function hasTab(string $title): bool
35+
{
36+
return isset($this->items[$title]);
37+
}
38+
39+
public function getTab(string $title): PanelItem
40+
{
41+
return $this->items[$title]
42+
?? throw new \InvalidArgumentException(\sprintf('Tab with title "%s" not found.', $title));
43+
}
44+
45+
public function removeTab(string $title): static
46+
{
47+
if (!$item = $this->items[$title] ?? null) {
48+
throw new \InvalidArgumentException(\sprintf('Tab with title "%s" not found.', $title));
49+
}
50+
51+
unset($this->items[$title]);
52+
53+
return $this->removeItem($item);
54+
}
3355
}

src/HasDialogBox.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace Neusta\Pimcore\AreabrickConfigBundle;
44

5+
use Pimcore\Extension\Document\Areabrick\AbstractAreabrick;
56
use Pimcore\Extension\Document\Areabrick\EditableDialogBoxConfiguration;
67
use Pimcore\Model\Document\Editable;
78
use Pimcore\Model\Document\Editable\Area\Info;
89

910
/**
1011
* @template T of DialogBoxBuilder
12+
*
13+
* @mixin AbstractAreabrick
1114
*/
1215
trait HasDialogBox
1316
{
@@ -17,6 +20,12 @@ final public function getEditableDialogBoxConfiguration(Editable $area, ?Info $i
1720

1821
$this->buildDialogBox($builder, $area, $info);
1922

23+
if ($configurator = $info?->getParam('dialogBoxConfigurator')) {
24+
if ($this->container->has($configurator)) {
25+
$this->container->get($configurator)->configureDialogBox($builder, $area, $info);
26+
}
27+
}
28+
2029
return $builder->build();
2130
}
2231

0 commit comments

Comments
 (0)