Skip to content

Commit 587015e

Browse files
Allow hiding of layout elements
1 parent e8bad9a commit 587015e

File tree

6 files changed

+180
-50
lines changed

6 files changed

+180
-50
lines changed

README.md

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,23 @@ An example configuration can be found in `config/packages/example.custom_layouts
3030
## Configuration
3131

3232
```
33-
Pimcore\Model\DataObject\TestObject:
34-
# layouts
35-
custom_layout:
36-
label: "My custom layout"
37-
mode: !php/const Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig::MODE_EDIT
38-
auto_apply_roles: []
39-
auto_apply_workflow_states: []
40-
fields:
41-
title:
42-
title: "Overwritten Titel"
43-
editable: false
44-
visible: false
33+
advanced_custom_layout:
34+
Pimcore\Model\DataObject\TestObject:
35+
# layouts
36+
custom_layout:
37+
label: "My custom layout"
38+
mode: !php/const Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig::MODE_EDIT
39+
auto_apply_roles: []
40+
auto_apply_workflow_states: []
41+
fields:
42+
title:
43+
title: "Overwritten Titel"
44+
editable: false
45+
visible: false
46+
layoutElements:
47+
Data Quality:
48+
title: "Overwritten Titel"
49+
visible: false
4550
```
4651

4752
- `Pimcore\Model\DataObject\TestObject:` is the class name of the object you want to configure the custom layout for.
@@ -55,6 +60,9 @@ Pimcore\Model\DataObject\TestObject:
5560
- `fields.title.title` is the new label of the field which will be displayed in the object edit mode.
5661
- `fields.title.editable` is a boolean value which defines if the field is editable or not.
5762
- `fields.title.visible` is a boolean value which defines if the field is visible or not.
63+
- `layoutElements.Data Quality` will apply on all LayoutElements which have the name `Data Quality`.
64+
- `layoutElements.Data Quality.title` overwrites the visible label of the layout element.
65+
- `layoutElements.Data Quality.visible` determines if the layout element will be visible/added to the custom layout
5866

5967
## Custom Layout Modes
6068

@@ -64,16 +72,17 @@ A custom layout with this setting will consist of all fields of the layout defin
6472
The provided settings will then overwrite the default settings.
6573

6674
```
67-
Pimcore\Model\DataObject\TestObject:
68-
# layouts
69-
custom_layout:
70-
label: "My custom layout"
71-
mode: !php/const Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig::MODE_EDIT
72-
fields:
73-
title:
74-
title: "Overwritten Titel"
75-
editable: false
76-
visible: false
75+
advanced_custom_layout:
76+
Pimcore\Model\DataObject\TestObject:
77+
# layouts
78+
custom_layout:
79+
label: "My custom layout"
80+
mode: !php/const Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig::MODE_EDIT
81+
fields:
82+
title:
83+
title: "Overwritten Titel"
84+
editable: false
85+
visible: false
7786
```
7887

7988
In this example we will keep all layout and field definitions but overwrite the title, lock and hide the field.
@@ -84,15 +93,16 @@ A custom layout with this setting will hide all fields which are not defined in
8493
Additionally, the provided settings will overwrite the default settings regarding being editable or visible.
8594

8695
```
87-
Pimcore\Model\DataObject\TestObject:
88-
# layouts
89-
custom_layout:
90-
label: "My custom layout"
91-
mode: !php/const Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig::MODE_SHOW
92-
fields:
93-
title:
94-
title: "Overwritten Titel"
95-
editable: false
96+
advanced_custom_layout:
97+
Pimcore\Model\DataObject\TestObject:
98+
# layouts
99+
custom_layout:
100+
label: "My custom layout"
101+
mode: !php/const Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig::MODE_SHOW
102+
fields:
103+
title:
104+
title: "Overwritten Titel"
105+
editable: false
96106
```
97107

98108
In this example we will only show the title attribute which is not editable anymore.

src/Config/Configuration.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88

99
class Configuration implements ConfigurationInterface
1010
{
11-
public const FIELDS = 'fields';
1211
public const MODE = 'mode';
1312
public const LABEL = 'label';
1413
public const AUTO_APPLY_ROLES = 'auto_apply_roles';
14+
public const AUTO_APPLY_WORKFLOW_STATES = 'auto_apply_workflow_states';
15+
16+
public const FIELDS = 'fields';
1517
public const FIELD_TITLE = 'title';
1618
public const FIELD_EDITABLE = 'editable';
1719
public const FIELD_VISIBLE = 'visible';
18-
public const AUTO_APPLY_WORKFLOW_STATES = 'auto_apply_workflow_states';
20+
21+
public const LAYOUT_ELEMENTS = 'layoutElements';
22+
public const LAYOUT_ELEMENT_TITLE = 'title';
23+
public const LAYOUT_ELEMENT_VISIBLE = 'visible';
1924

2025
public function getConfigTreeBuilder(): TreeBuilder
2126
{
@@ -50,6 +55,16 @@ public function getConfigTreeBuilder(): TreeBuilder
5055
->end()
5156
->end()
5257
->end()
58+
->arrayNode(self::LAYOUT_ELEMENTS)
59+
->normalizeKeys(false)
60+
->useAttributeAsKey('layoutElement')
61+
->arrayPrototype()
62+
->children()
63+
->scalarNode(self::LAYOUT_ELEMENT_TITLE)->defaultNull()->end()
64+
->booleanNode(self::LAYOUT_ELEMENT_VISIBLE)->defaultNull()->end()
65+
->end()
66+
->end()
67+
->end()
5368
->end()
5469
->end()
5570
->end();

src/Factory/CustomLayoutConfigFactory.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,43 @@ public function create(string $className, string $layoutName, array $layoutData)
2222
$layoutData[Configuration::FIELDS],
2323
array_keys($layoutData[Configuration::FIELDS])
2424
)
25+
),
26+
array_filter(
27+
array_map(
28+
fn($configData, $key) => $this->createLayoutElementConfig($key, $configData),
29+
$layoutData[Configuration::LAYOUT_ELEMENTS],
30+
array_keys($layoutData[Configuration::LAYOUT_ELEMENTS])
31+
)
2532
)
2633
);
2734
}
2835

29-
private function createFieldConfig(string $fieldId, array $fieldConfigData): ?CustomLayoutConfig\FieldConfig
36+
private function createFieldConfig(string $fieldId, array $configData): ?CustomLayoutConfig\FieldConfig
3037
{
31-
$filteredConfigData = array_filter($fieldConfigData, fn($data) => !is_null($data));
38+
$filteredConfigData = array_filter($configData, fn($data) => !is_null($data));
3239
if (empty($filteredConfigData)) {
3340
return null;
3441
}
3542

3643
return new CustomLayoutConfig\FieldConfig(
3744
$fieldId,
38-
$fieldConfigData[Configuration::FIELD_TITLE],
39-
$fieldConfigData[Configuration::FIELD_EDITABLE],
40-
$fieldConfigData[Configuration::FIELD_VISIBLE]
45+
$configData[Configuration::FIELD_TITLE],
46+
$configData[Configuration::FIELD_EDITABLE],
47+
$configData[Configuration::FIELD_VISIBLE]
48+
);
49+
}
50+
51+
private function createLayoutElementConfig(string $elementId, array $configData): ?CustomLayoutConfig\LayoutElementConfig
52+
{
53+
$filteredConfigData = array_filter($configData, fn($data) => !is_null($data));
54+
if (empty($filteredConfigData)) {
55+
return null;
56+
}
57+
58+
return new CustomLayoutConfig\LayoutElementConfig(
59+
$elementId,
60+
$configData[Configuration::LAYOUT_ELEMENT_TITLE],
61+
$configData[Configuration::LAYOUT_ELEMENT_VISIBLE]
4162
);
4263
}
4364
}

src/Model/CustomLayoutConfig.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Basilicom\AdvancedCustomLayoutBundle\Model;
44

55
use Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig\FieldConfig;
6+
use Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig\LayoutElementConfig;
67

78
class CustomLayoutConfig
89
{
@@ -19,15 +20,18 @@ class CustomLayoutConfig
1920
private array $autoApplyForRoles;
2021
/** @var string[] */
2122
private array $autoApplyForWorkflowStates;
23+
/** @var LayoutElementConfig[] */
24+
private array $layoutElements;
2225

2326
/**
24-
* @param string $className
25-
* @param string $layoutIdentifier
26-
* @param string $label
27-
* @param string $mode
28-
* @param string[] $autoApplyForRoles
29-
* @param string[] $autoApplyForWorkflowStates
30-
* @param FieldConfig[] $fields
27+
* @param string $className
28+
* @param string $layoutIdentifier
29+
* @param string $label
30+
* @param string $mode
31+
* @param string[] $autoApplyForRoles
32+
* @param string[] $autoApplyForWorkflowStates
33+
* @param FieldConfig[] $fields
34+
* @param LayoutElementConfig[] $layoutElements
3135
*/
3236
public function __construct(
3337
string $className,
@@ -36,7 +40,8 @@ public function __construct(
3640
string $mode,
3741
array $autoApplyForRoles,
3842
array $autoApplyForWorkflowStates,
39-
array $fields
43+
array $fields,
44+
array $layoutElements
4045
) {
4146
$this->className = $className;
4247
$this->layoutIdentifier = $layoutIdentifier;
@@ -45,6 +50,7 @@ public function __construct(
4550
$this->autoApplyForRoles = $autoApplyForRoles;
4651
$this->autoApplyForWorkflowStates = $autoApplyForWorkflowStates;
4752
$this->fields = $fields;
53+
$this->layoutElements = $layoutElements;
4854
}
4955

5056
public function getClassName(): string
@@ -95,4 +101,12 @@ public function getFields(): array
95101
{
96102
return $this->fields;
97103
}
104+
105+
/**
106+
* @return LayoutElementConfig[]
107+
*/
108+
public function getLayoutElements(): array
109+
{
110+
return $this->layoutElements;
111+
}
98112
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig;
4+
5+
class LayoutElementConfig
6+
{
7+
private string $elementId;
8+
private ?string $title;
9+
private ?bool $isVisible;
10+
11+
public function __construct(string $elementId, ?string $title, ?bool $isVisible)
12+
{
13+
$this->elementId = $elementId;
14+
$this->title = $title;
15+
$this->isVisible = $isVisible;
16+
}
17+
18+
public function getElementId(): string
19+
{
20+
return $this->elementId;
21+
}
22+
23+
public function getTitle(): ?string
24+
{
25+
return $this->title;
26+
}
27+
28+
public function getIsVisible(): ?bool
29+
{
30+
return $this->isVisible;
31+
}
32+
}

src/Service/CustomLayoutService.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Basilicom\AdvancedCustomLayoutBundle\Service;
44

55
use Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig;
6+
use Basilicom\AdvancedCustomLayoutBundle\Model\CustomLayoutConfig\LayoutElementConfig;
67
use Exception;
78
use Pimcore\Cache\RuntimeCache;
89
use Pimcore\Model\DataObject\ClassDefinition;
@@ -67,7 +68,7 @@ public function importCustomLayout(CustomLayoutConfig $layoutConfig): void
6768

6869
private function overwriteLayout(?ClassDefinition $classDefinition, CustomLayoutConfig $layoutConfig): ?Layout
6970
{
70-
$layoutDefinitions = $classDefinition->getLayoutDefinitions();
71+
$layoutDefinition = $classDefinition->getLayoutDefinitions();
7172
$fieldDefinitions = $classDefinition->getFieldDefinitions();
7273

7374
foreach ($layoutConfig->getFields() as $fieldConfig) {
@@ -80,12 +81,16 @@ private function overwriteLayout(?ClassDefinition $classDefinition, CustomLayout
8081
}
8182
}
8283

83-
return $layoutDefinitions;
84+
foreach ($layoutConfig->getLayoutElements() as $layoutElementConfig) {
85+
$this->adaptLayoutDefinition($layoutElementConfig, $layoutDefinition);
86+
}
87+
88+
return $layoutDefinition;
8489
}
8590

8691
private function editLayout(?ClassDefinition $classDefinition, CustomLayoutConfig $layoutConfig): ?Layout
8792
{
88-
$layoutDefinitions = $classDefinition->getLayoutDefinitions();
93+
$layoutDefinition = $classDefinition->getLayoutDefinitions();
8994
$fieldDefinitions = $classDefinition->getFieldDefinitions();
9095

9196
foreach ($layoutConfig->getFields() as $fieldConfig) {
@@ -97,7 +102,40 @@ private function editLayout(?ClassDefinition $classDefinition, CustomLayoutConfi
97102
}
98103
}
99104

100-
return $layoutDefinitions;
105+
foreach ($layoutConfig->getLayoutElements() as $layoutElementConfig) {
106+
$this->adaptLayoutDefinition($layoutElementConfig, $layoutDefinition);
107+
}
108+
109+
return $layoutDefinition;
110+
}
111+
112+
private function adaptLayoutDefinition(LayoutElementConfig $config, Layout $layoutDefinition): Layout
113+
{
114+
if ($layoutDefinition->getName() === $config->getElementId()) {
115+
if ($config->getTitle() !== null) {
116+
$layoutDefinition->setTitle($config->getTitle());
117+
}
118+
}
119+
120+
$children = $layoutDefinition->getChildren();
121+
if (!empty($children)) {
122+
$newChildren = [];
123+
foreach ($children as $index => $layoutChild) {
124+
if ($layoutChild instanceof Layout) {
125+
if ($layoutChild->getName() === $config->getElementId()) {
126+
if ((bool)$config->getIsVisible() === false) {
127+
continue;
128+
}
129+
}
130+
$newChildren[] = $this->adaptLayoutDefinition($config, $layoutChild);
131+
} else {
132+
$newChildren[] = $layoutChild;
133+
}
134+
}
135+
$layoutDefinition->setChildren($newChildren);
136+
}
137+
138+
return $layoutDefinition;
101139
}
102140

103141
private function adaptFieldDefinition(Data $fieldDefinition, CustomLayoutConfig\FieldConfig $fieldConfig): void

0 commit comments

Comments
 (0)