|
- <?php
-
- namespace Lc\SovBundle\Definition\Field;
-
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
- use Lc\SovBundle\Field\CollectionField;
- use Lc\SovBundle\Translation\TranslatorAdmin;
- use PhpParser\Node\Expr\Throw_;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
-
- abstract class AbstractFieldDefinition
- {
- protected array $panels;
- protected array $fieldsByPanel;
- protected array $fieldList;
- protected bool $isPopulate = false;
- protected TranslatorAdmin $translatorAdmin;
-
- abstract public function configureFieldsIndex(): array;
- abstract public function configurePanels(): array;
- abstract public function configureFields(): array;
-
- public function __construct(TranslatorAdmin $translatorAdmin)
- {
- $this->translatorAdmin = $translatorAdmin;
- }
-
- protected function populate(): void
- {
- if ($this->isPopulate === false) {
- $this->fieldList = [] ;
- $allFieldArray = $this->configureFields();;
- $this->panels = $this->configurePanels();
-
- foreach ($this->panels as $panel) {
-
- $method = 'configureFieldsPanel' . ucfirst($panel);
- if (method_exists($this, $method)) {
-
- $fieldArray = $this->$method();
- foreach($fieldArray as $key => $field) {
- if(is_string($field)) {
- unset($fieldArray[$key]);
- $fieldArray[$field] = $allFieldArray[$field];
- }
- }
-
- $this->fieldsByPanel[$panel] = array_merge(
- ['panel_' . $panel => FormField::addPanel($panel)],
- $fieldArray
- );
- $this->fieldList = array_merge($this->fieldList, $this->fieldsByPanel[$panel]);
- }else{
- throw new \Exception($method . ' n\'existe pas ');
- }
- }
-
- $this->fieldList = array_merge($this->fieldList, $allFieldArray);
-
- $this->isPopulate = false;
- }
- }
-
- public function getFieldList(string $pageName = '')
- {
- $this->populate();
-
- if($pageName == Crud::PAGE_INDEX) {
- $fieldArray = [];
- foreach($this->configureFieldsIndex() as $fieldName) {
- $fieldArray[] = $this->fieldList[$fieldName];
- }
- return $fieldArray;
- }
- else {
- return $this->fieldList;
- }
- }
-
- public function getFieldListByPanel($panel)
- {
- $this->populate();
- return $this->fieldsByPanel[$panel];
- }
-
- public function getPanelList()
- {
- $this->populate();
- return $this->fieldsByPanel;
- }
-
- public function configureFieldsPanelSeo(): array
- {
- return array(
- 'metaTitle' => TextField::new('metaTitle')
- ->setLabel('Meta Title')
- ->setHelp('Affiché dans les résultats de recherche Google')
- ->hideOnIndex(),
- 'metaDescription' => TextareaField::new('metaDescription')
- ->setLabel('Meta description')
- ->setHelp('Affiché dans les résultats de recherche Google')
- ->hideOnIndex(),
- 'oldUrls' => CollectionField::new('oldUrls')
- ->setFormTypeOption('entry_type', TextType::class)
- ->setLabel('Anciennes urls du document')
- ->hideOnIndex(),
- );
- }
-
- public function configureFieldsPanelConf(): array
- {
- return [
- //FormField::addPanel('configuration')->setTemplateName('crud/field/generic'),
- 'devAlias' => TextField::new('devAlias')->hideOnIndex(),
- ];
- }
- }
|