Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new Twig component to create Action Menus #6649

Open
wants to merge 5 commits into
base: 4.x
Choose a base branch
from

Conversation

javiereguiluz
Copy link
Collaborator

This is the first anonymous Twig Component that we added to the project. It's modeled after GitHub's ActionMenu component (https://primer.style/components/action-menu) and allows to create dropdowns.

Sadly, using this component means that we lost the {% block user_menu %}...{% endblock %} TWig block in the template. This is because I can't make it work with the new Twig component. If anybody knows how to keep that block, please tell me. Thanks!


Again, this was possible thanks to the excellent article written by @yceruto in https://dev.to/yceruto/bundling-your-symfony-ux-twig-components-4997

Just a quick comment for @yceruto. In this section: https://dev.to/yceruto/bundling-your-symfony-ux-twig-components-4997#bundling-uionly-components you propose to use this:

        $builder->prependExtensionConfig('twig', [
            'paths' => [
                'templates/bundles/AcmeBundle/' => null,
                dirname(__DIR__).'/templates/' => null,
            ],
        ]);

This didn't work for me because the application that installs the bundle might not have defined the templates/bundles/AcmeBundle/ dir. Maybe I'm doing something wrong ... but in any case, I updated it as follows:

        $bundleTemplatesOverrideDir = $builder->getParameter('kernel.project_dir').'/templates/bundles/EasyAdminBundle/';
        $builder->prependExtensionConfig('twig', [
            'paths' => is_dir($bundleTemplatesOverrideDir)
                ? [
                    'templates/bundles/EasyAdminBundle/' => null,
                    dirname(__DIR__).'/../templates/' => 'ea',
                ]
                : [
                    dirname(__DIR__).'/../templates/' => 'ea',
                ],
        ]);

@javiereguiluz javiereguiluz added this to the 4.x milestone Dec 15, 2024
@yceruto
Copy link
Collaborator

yceruto commented Dec 15, 2024

Hey Javier! just a heads-up: after symfony/ux#2019, anonymous components now have native bundle support. This might make the workaround mentioned in the article unnecessary. Let me know if there's anything else I can help with.

@javiereguiluz
Copy link
Collaborator Author

@yceruto thanks for your tip. I tried removing the config in prepend() but I see this error:

An exception has been thrown during the rendering of a template ("Unknown component 
"ea:ActionMenu:ActionList". And no matching anonymous component template was found.").

I think it's related to having defined ea as the prefix of EA components. Maybe it would work if I create the components in templates/components/ea/... instead of just templates/components/... I don't know if that would be a better practice. It might make overriding easier in apps, because right now you hae to use that ea/ dir when you override EA's components. MAybe having the ea/ dir explicitly in templates/components/ would be better 🤔

@yceruto
Copy link
Collaborator

yceruto commented Dec 16, 2024

Well, the default convention uses the Twig EasyAdmin namespace for EasyAdminBundle to discover any template. In this case, it should be <twig:EasyAdmin:ActionMenu:ActionList>, which should look into EasyAdminBundle/templates/components/ActionMenu/ActionList.html.twig directory.

Could you elaborate a bit more on why you want to create a new ea namespace?

@yceruto
Copy link
Collaborator

yceruto commented Dec 16, 2024

On the application side, if you want to override that component, you can create a new template at the following path:
<project_dir>/templates/components/EasyAdmin/ActionMenu/ActionList.html.twig

@javiereguiluz
Copy link
Collaborator Author

Could you elaborate a bit more on why you want to create a new ea namespace?

Becuase EasyAdmin is too long ... and error-prone because of the uppercase and lowercase mixing. We use ea in many places of the bundle, so using the components as <twig:ea:... /> feels natural. I'll explain in the docs that you have to create templates/components/ea/... dir and I thin kit will be fine 🙏

@yceruto
Copy link
Collaborator

yceruto commented Dec 16, 2024

In that case, adding an extra Twig namespace ea for EasyAdminBundle/templates/ dir during the prepending phase would be okay 👍

@yceruto
Copy link
Collaborator

yceruto commented Dec 16, 2024

By the way, in the description, you set 'templates/bundles/EasyAdminBundle/' => null, if you want it to be the overridden path, it should instead be 'templates/bundles/EasyAdminBundle/' => 'ea', right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants