-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPagePickerProvider.php
More file actions
174 lines (147 loc) · 4.82 KB
/
Copy pathPagePickerProvider.php
File metadata and controls
174 lines (147 loc) · 4.82 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2025 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2013-2025 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
namespace ContaoCommunityAlliance\DcGeneral\Contao\Picker;
use Contao\CoreBundle\Picker\AbstractPickerProvider;
use Contao\CoreBundle\Picker\DcaPickerProviderInterface;
use Contao\CoreBundle\Picker\PickerConfig;
use Contao\System;
use Knp\Menu\FactoryInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Provides the page picker.
*/
class PagePickerProvider extends AbstractPickerProvider implements DcaPickerProviderInterface
{
private Security $security;
/**
* @internal
*/
public function __construct(
FactoryInterface $menuFactory,
RouterInterface $router,
?TranslatorInterface $translator,
?Security $security
) {
if (null === $translator) {
$translator = System::getContainer()->get('translator');
assert($translator instanceof TranslatorInterface);
// @codingStandardsIgnoreStart
@trigger_error(
'Not passing the translator as argument to "' . __METHOD__ . '" is deprecated ' .
'and will cause an error in DCG 3.0',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
}
parent::__construct($menuFactory, $router, $translator);
if (null === $security) {
$security = System::getContainer()->get('security.helper');
assert($security instanceof Security);
// @codingStandardsIgnoreStart
@trigger_error(
'Not passing the security as argument to "' . __METHOD__ . '" is deprecated ' .
'and will cause an error in DCG 3.0',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
}
$this->security = $security;
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'ccaPagePicker';
}
/**
* {@inheritdoc}
*/
public function supportsContext(string $context): bool
{
return \in_array($context, ['cca_page', 'cca_link'], true)
&& $this->security->isGranted('contao_user.modules', 'page');
}
/**
* {@inheritdoc}
*/
public function supportsValue(PickerConfig $config): bool
{
if ('page' === $config->getContext()) {
return \is_numeric($config->getValue());
}
return false !== \strpos($config->getValue(), '{{link_url::');
}
/**
* {@inheritdoc}
*/
public function getDcaTable(?PickerConfig $config = null): string
{
return 'tl_page';
}
/**
* {@inheritdoc}
*/
public function getDcaAttributes(PickerConfig $config): array
{
$value = $config->getValue();
$attributes = ['fieldType' => 'radio'];
if ('page' === $config->getContext()) {
if ($fieldType = $config->getExtra('fieldType')) {
$attributes['fieldType'] = $fieldType;
}
if ($source = $config->getExtra('source')) {
$attributes['preserveRecord'] = $source;
}
if (\is_array($rootNodes = $config->getExtra('rootNodes'))) {
$attributes['rootNodes'] = $rootNodes;
}
if ($value) {
$intval = static function (mixed $val): int {
return (int) $val;
};
$attributes['value'] = \array_map($intval, \explode(',', $value));
}
return $attributes;
}
if ($value && false !== \strpos($value, '{{link_url::')) {
$attributes['value'] = \str_replace(['{{link_url::', '}}'], '', $value);
}
return $attributes;
}
/**
* {@inheritdoc}
*/
public function convertDcaValue(PickerConfig $config, mixed $value): int|string
{
if ('page' === $config->getContext()) {
return (int) $value;
}
return '{{link_url::' . $value . '}}';
}
/**
* {@inheritdoc}
*/
protected function getRouteParameters(PickerConfig|null $config = null): array
{
return ['do' => 'page'];
}
}