|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CMS Kernel package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LIN3S\CMSKernel\Infrastructure\Symfony\Form\Type; |
|
13
|
|
|
|
|
14
|
|
|
use LIN3S\CMSKernel\Application\Query\Menu\MenuOfIdHandler; |
|
15
|
|
|
use LIN3S\CMSKernel\Application\Query\Menu\MenuOfIdQuery; |
|
16
|
|
|
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException; |
|
17
|
|
|
use Symfony\Component\Form\AbstractType; |
|
18
|
|
|
use Symfony\Component\Form\CallbackTransformer; |
|
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
|
20
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Beñat Espiña <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class MenuTreeType extends AbstractType |
|
27
|
|
|
{ |
|
28
|
|
|
private $menuOfIdHandler; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(MenuOfIdHandler $menuOfIdHandler) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->menuOfIdHandler = $menuOfIdHandler; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
36
|
|
|
{ |
|
37
|
|
|
$locale = $options['locale']; |
|
38
|
|
|
$menuId = isset($options['menu_id']) ? $options['menu_id'] : null; |
|
39
|
|
|
|
|
40
|
|
|
$builder |
|
41
|
|
|
->add('tree', HiddenType::class) |
|
42
|
|
|
->addModelTransformer(new CallbackTransformer( |
|
43
|
|
|
function ($value) use ($locale, $menuId) { |
|
|
|
|
|
|
44
|
|
|
if (null === $menuId) { |
|
45
|
|
|
return ['tree' => []]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
$menu = $this->menuOfIdHandler->__invoke( |
|
50
|
|
|
new MenuOfIdQuery( |
|
51
|
|
|
$menuId, |
|
52
|
|
|
$locale |
|
53
|
|
|
) |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
} catch (TranslationDoesNotExistException $exception) { |
|
57
|
|
|
return ['tree' => []]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return ['tree' => json_encode($menu['tree'])]; |
|
61
|
|
|
}, |
|
62
|
|
|
function ($value) { |
|
63
|
|
|
return $value['tree'] ? json_decode($value['tree'], true) : []; |
|
64
|
|
|
} |
|
65
|
|
|
)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
69
|
|
|
{ |
|
70
|
|
|
$resolver |
|
71
|
|
|
->setRequired('locale') |
|
72
|
|
|
->setDefined('menu_id'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.