瀏覽代碼

AbstractFieldDefinition

develop
Guillaume 3 年之前
父節點
當前提交
3092f8ebd9
共有 1 個檔案被更改,包括 110 行新增74 行删除
  1. +110
    -74
      Definition/Field/AbstractFieldDefinition.php

+ 110
- 74
Definition/Field/AbstractFieldDefinition.php 查看文件

@@ -13,108 +13,144 @@ 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
abstract public function configureFields(): array;

public function configureFieldsBase(): array
{
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 ');
}
}
return [
'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(),
'devAlias' => TextField::new('devAlias')->hideOnIndex(),
];
}

$this->fieldList = array_merge($this->fieldList, $allFieldArray);
public function configureDefaultFields(): array
{
return array_keys($this->configureFields());
}

$this->isPopulate = false;
}
public function configureIndex(): array
{
return $this->configureDefaultFields();
}

public function configureDetail(): array
{
return $this->configureDefaultFields();
}

public function getFieldList(string $pageName = '')
public function configureForm(): array
{
$this->populate();
return $this->configureDefaultFields();
}

public function configurePanels(): array
{
return [];
}

public function configurePanelSeo(): array
{
return [
'metaTitle',
'metaDescription',
'oldUrls'
];
}

public function configurePanelConf(): array
{
return [
'devAlias',
];
}

public function getFields(string $pageName = ''): array
{
if($pageName == Crud::PAGE_INDEX) {
$fieldArray = [];
foreach($this->configureFieldsIndex() as $fieldName) {
$fieldArray[] = $this->fieldList[$fieldName];
}
return $fieldArray;
return $this->buildFieldArray($this->configureIndex());
}
else {
return $this->fieldList;
elseif($pageName == Crud::PAGE_DETAIL) {
return $this->buildFieldArray($this->configureDetail());
}
elseif($pageName == Crud::PAGE_EDIT || $pageName == Crud::PAGE_NEW) {
return $this->buildFieldArrayForm();
}

return [];
}

public function getFieldListByPanel($panel)
public function getAllFields(): array
{
$this->populate();
return $this->fieldsByPanel[$panel];
return array_merge(
$this->configureFieldsBase(),
$this->configureFields()
);
}

public function getPanelList()
public function buildFieldArray(array $configureFieldArray): array
{
$this->populate();
return $this->fieldsByPanel;
$allFieldArray = $this->getAllFields();

$fieldArray = [];
foreach($configureFieldArray as $fieldName) {
$fieldArray[] = $allFieldArray[$fieldName];
}

return $fieldArray;
}

public function configureFieldsPanelSeo(): array
public function buildFieldArrayForm(): 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(),
);
$fieldArray = [];
$panelArray = $this->configurePanels();

if($panelArray && count($panelArray) > 0) {
foreach($panelArray as $panel) {
$fieldArray = array_merge(
$fieldArray,
$this->buildFieldArrayFormByPanel($panel)
);
}
}
else {
$fieldArray = $this->buildFieldArray($this->configureForm());
}

return $fieldArray;
}

public function configureFieldsPanelConf(): array
public function buildFieldArrayFormByPanel(string $panel): array
{
return [
//FormField::addPanel('configuration')->setTemplateName('crud/field/generic'),
'devAlias' => TextField::new('devAlias')->hideOnIndex(),
];
$method = 'configurePanel' . ucfirst($panel);
if(method_exists($this, $method)) {
$panelFieldArray = $this->$method();
}
else{
throw new \Exception($method . ' n\'existe pas ');
}

return array_merge(
['panel_' . $panel => FormField::addPanel($panel)],
$this->buildFieldArray($panelFieldArray)
);
}

}

Loading…
取消
儲存