1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProjetNormandie\ArticleBundle\Admin; |
6
|
|
|
|
7
|
|
|
use A2lix\TranslationFormBundle\Form\Type\TranslationsType; |
8
|
|
|
use ProjetNormandie\ArticleBundle\Enum\ArticleStatus; |
9
|
|
|
use ProjetNormandie\ArticleBundle\Form\Type\RichTextEditorType; |
10
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
11
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
12
|
|
|
use Sonata\AdminBundle\Show\ShowMapper; |
13
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
14
|
|
|
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType; |
15
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
16
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
17
|
|
|
use Sonata\AdminBundle\Route\RouteCollectionInterface; |
18
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter; |
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
20
|
|
|
use Symfony\Component\Form\Extension\Core\Type\DateTimeType; |
21
|
|
|
use Symfony\Component\Form\Extension\Core\Type\EnumType; |
22
|
|
|
|
23
|
|
|
class ArticleAdmin extends AbstractAdmin |
24
|
|
|
{ |
25
|
|
|
protected function generateBaseRouteName(bool $isChildAdmin = false): string |
26
|
|
|
{ |
27
|
|
|
return 'pna_article_admin'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function configureRoutes(RouteCollectionInterface $collection): void |
31
|
|
|
{ |
32
|
|
|
$collection->remove('export'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function configureDefaultSortValues(array &$sortValues): void |
36
|
|
|
{ |
37
|
|
|
$sortValues['_page'] = 1; |
38
|
|
|
$sortValues['_sort_order'] = 'DESC'; |
39
|
|
|
$sortValues['_sort_by'] = 'id'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface |
43
|
|
|
{ |
44
|
|
|
$query = parent::configureQuery($query); |
45
|
|
|
$query->leftJoin($query->getRootAliases()[0] . '.translations', 't') |
|
|
|
|
46
|
|
|
->addSelect('t'); |
47
|
|
|
return $query; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function configureFormFields(FormMapper $form): void |
51
|
|
|
{ |
52
|
|
|
$form |
53
|
|
|
->add('id', TextType::class, [ |
54
|
|
|
'label' => 'article.form.id', |
55
|
|
|
'attr' => ['readonly' => true], |
56
|
|
|
'required' => false, |
57
|
|
|
'disabled' => true |
58
|
|
|
]) |
59
|
|
|
->add('status', EnumType::class, [ |
60
|
|
|
'label' => 'article.form.status', |
61
|
|
|
'required' => true, |
62
|
|
|
'class' => ArticleStatus::class, |
63
|
|
|
'choice_label' => fn(ArticleStatus $status) => $status->value, |
64
|
|
|
'expanded' => false, |
65
|
|
|
'multiple' => false, |
66
|
|
|
]) |
67
|
|
|
->add('publishedAt', DateTimeType::class, [ |
68
|
|
|
'label' => 'article.form.published_at', |
69
|
|
|
'required' => false, |
70
|
|
|
'years' => range(2004, date('Y')) |
71
|
|
|
]) |
72
|
|
|
->add('translations', TranslationsType::class, [ |
73
|
|
|
'label' => false, |
74
|
|
|
'fields' => [ |
75
|
|
|
'title' => [ |
76
|
|
|
'field_type' => TextType::class, |
77
|
|
|
'label' => 'Title', |
78
|
|
|
], |
79
|
|
|
'content' => [ |
80
|
|
|
'field_type' => RichTextEditorType::class, |
81
|
|
|
'label' => 'Content', |
82
|
|
|
] |
83
|
|
|
] |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function configureDatagridFilters(DatagridMapper $filter): void |
88
|
|
|
{ |
89
|
|
|
$filter |
90
|
|
|
->add( |
91
|
|
|
'author', |
92
|
|
|
ModelFilter::class, |
93
|
|
|
[ |
94
|
|
|
'label' => 'article.filter.author', |
95
|
|
|
'field_type' => ModelAutocompleteType::class, |
96
|
|
|
'field_options' => ['property' => 'username'], |
97
|
|
|
] |
98
|
|
|
) |
99
|
|
|
->add('translations.title', null, ['label' => 'article.filter.title']) |
100
|
|
|
->add('status', null, ['label' => 'article.filter.status']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function configureListFields(ListMapper $list): void |
104
|
|
|
{ |
105
|
|
|
$list->addIdentifier('id', null, ['label' => 'article.list.id']) |
106
|
|
|
->add('getDefaultTitle', null, ['label' => 'article.list.title']) |
107
|
|
|
->add('author', null, ['label' => 'article.list.author']) |
108
|
|
|
->add('status', null, ['label' => 'article.list.status']) |
109
|
|
|
->add('createdAt', null, ['label' => 'article.list.created_at']) |
110
|
|
|
->add('publishedAt', 'datetime', ['label' => 'article.list.published_at']) |
111
|
|
|
->add('_action', 'actions', [ |
112
|
|
|
'actions' => [ |
113
|
|
|
'show' => [], |
114
|
|
|
'edit' => [], |
115
|
|
|
'groups' => [ |
116
|
|
|
'template' => '@ProjetNormandieArticle/admin/article_comments_link.html.twig' |
117
|
|
|
], |
118
|
|
|
] |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function configureShowFields(ShowMapper $show): void |
123
|
|
|
{ |
124
|
|
|
$show |
125
|
|
|
->add('id', null, ['label' => 'article.show.id']) |
126
|
|
|
->add('status', null, ['label' => 'article.show.status']) |
127
|
|
|
->add('title', null, ['label' => 'article.show.title']) |
128
|
|
|
->add('content', null, ['label' => 'article.show.content', 'safe' => true]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|