Skip to content

Commit

Permalink
feat(admin): Make "Layout" a ChoiceField
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Dec 30, 2024
1 parent da46c08 commit 24872f2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Controller/Admin/GroupCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use App\Entity\Group;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

use function Symfony\Component\Translation\t;

class GroupCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
Expand All @@ -20,11 +23,26 @@ public static function getEntityFqcn(): string

public function configureFields(string $pageName): iterable
{
$availableLayouts = glob(__DIR__.'/../../../templates/overview/layout/*.html.twig');
if (false === $availableLayouts) {
$availableLayouts = [];
}

$availableLayouts = array_map(static fn ($path) => pathinfo($path, PATHINFO_BASENAME), $availableLayouts);
if (0 === \count($availableLayouts)) {
$availableLayouts = ['default.html.twig'];
}

// trim the .html.twig extension
$availableLayouts = array_map(static fn ($layout) => substr($layout, 0, -10), $availableLayouts);

return [
IdField::new('id')->hideOnForm(),
TextField::new('name', 'Name'),
TextEditorField::new('description', 'Description'),
TextField::new('layout', 'Layout'),
ChoiceField::new('layout', 'Layout')
->setChoices(array_combine($availableLayouts, $availableLayouts))
->setHelp(t('admin.group.layout.help')),
DateTimeField::new('created_at', 'Created at')->hideOnForm(),
DateTimeField::new('updated_at', 'Updated at')->hideOnForm(),
];
Expand Down
7 changes: 7 additions & 0 deletions translations/messages.en_US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ challenge:
column-different: The column names differ; please compare and modify according to the correct answer.
row-different: The %row% row you answered is different from the correct answer.
row-unmatched: The number of returned rows does not match the correct answer (the correct answer has %expected% rows, while you answered %actual% rows).

admin:
group:
layout:
help: >-
Layout name. Leave blank to use the default layout.
You can create your own layout in the <code>templates/overview/layout</code> directory in the code.
7 changes: 7 additions & 0 deletions translations/messages.zh_TW.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,10 @@ email-kind:
transactional: 通知型信件
marketing: 行銷型信件
test: 測試用信件

admin:
group:
layout:
help: >-
版面名稱。留空代表使用預設版面。
可以在程式碼的 <code>templates/overview/layout</code> 目錄製作自己想要的版面。

0 comments on commit 24872f2

Please sign in to comment.