forked from sulu/SuluFormBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicListAdmin.php
More file actions
96 lines (79 loc) · 3.11 KB
/
DynamicListAdmin.php
File metadata and controls
96 lines (79 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sulu\Bundle\FormBundle\Admin;
use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Admin\View\ListViewBuilderInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ToolbarAction;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
class DynamicListAdmin extends Admin
{
/**
* @param mixed[] $config
*/
public function __construct(
private ViewBuilderFactoryInterface $viewBuilderFactory,
private array $config,
) {
}
public function configureViews(ViewCollection $viewCollection): void
{
/** @var array<string, mixed> $sections */
foreach ($this->config as $parent => $sections) {
if (!$viewCollection->has($parent)) {
continue;
}
$counter = 0;
/** @var mixed[] $config */
foreach ($sections as $key => $config) {
++$counter;
$action = 'form-list';
if ($counter > 1) {
$action .= '-' . \str_replace('_', '-', $key);
}
$name = $parent . '.' . $action . '-key';
if (isset($config['name'])) {
$name = $config['name'];
}
if ($viewCollection->has($name)) {
throw new \RuntimeException(\sprintf('View "%s" does already exist.', $name));
}
$requestParameters = [
'type' => $config['type'],
];
/** @var ListViewBuilderInterface $view */
$view = $this->viewBuilderFactory->createListViewBuilder($name, '/' . $action)
->setResourceKey('dynamic_forms')
->setListKey('form_data')
->setTabTitle('sulu_form.data')
->addListAdapters(['table'])
->addRouterAttributesToListRequest(['id' => 'typeId'])
->addResourceStorePropertiesToListRequest([$config['property'] => 'form'])
->addResourceStorePropertiesToListMetadata([$config['property'] => 'id'])
->addToolbarActions([
new ToolbarAction('sulu_admin.delete'),
new ToolbarAction('sulu_admin.export'),
])
->setParent($parent);
if (isset($config['view'])) {
$requestParameters['view'] = $config['view'];
}
if (isset($config['position'])) {
$view->setTabOrder($config['position']);
}
if (isset($config['template'])) {
$view->setTabCondition(\sprintf('template == "%s"', $config['template']));
}
$view->addRequestParameters($requestParameters);
$viewCollection->add($view);
}
}
}
}