Andrew Rutter
Andrew Rutter

Reputation: 1297

How to setup a custom form/page within EasyAdminBundle

I have been able to build a simple CRUD app for a project using the Symfony EasyAdminBundle and it has worked great for the normal entity based use cases. I have some additional use cases though where I want to do things like rebuilding data. For these I have to capture certain request attributes, pass over to a controller and then delegate to a backend API call to a remote service.

This can all be done in Symfony but I am running into trouble with how to wire this into the EasyAdmin view/method of working. Ideally I want this to be a page inside easy admin and not lose the left menu etc. So far, the only way I have found to do this is to create a Model class what is using one of the existing tables but has only some properties I would need to drive into the API. I then override the controller actions so rather than do a default save, I handle against that remote API.

The problem with this approach is that obviously I am now bound to Doctrine entities and that would be problematic for requests that were not mappable to the database.

Is there a way to define a logical entity that would let me leverage associations so I can have lookups etc, that will wire into the bundle seamlessly, but are not actually tied to a backend database table or view?

Upvotes: 6

Views: 11768

Answers (3)

Javier Eguiluz
Javier Eguiluz

Reputation: 4107

I'd solve this problem creating a custom action as explained here (probably you want a route-based action) and then use a template that extends from @EasyAdmin\default\layout.html.twig or any other default template similar to what you want to achieve.

Upvotes: 4

SuperMalang
SuperMalang

Reputation: 104

I am adding my response for people who may still face this issue in the future. How I solved this without creating an entity :

  • Create a custom controller: symfony console make:controller

  • Edit the controller's view to inherit the EasyAdmin layout :

{# ./src/templates/home/index.html.twig #}
{% extends '@EasyAdmin/Default/layout.html.twig' %}

{# Let\'s remove/empty the header #}
{% block content_header_wrapper %} {% endblock content_header_wrapper %}

{# The main page content block #}
{% block main %}
   **PUT YOUR CODE HERE**
{% endblock main %}

{# Let\'s remove/empty the footer #}
{% block content_footer_wrapper %} {% endblock content_footer_wrapper %}
  • Add your page to the side navigation
design:
      menu:
        - {route: 'home', label: 'Home', default: true, icon: 'home'}
        - {entity: 'MyEntity', label: 'My Relevant Entity', icon: 'briefcase'}

Upvotes: 6

flik
flik

Reputation: 3623

Here is solution:

{# easy_admin/form.html.twig #}
{% block _product_custom_title_widget %}
    {# ... #}
    <a href="...">More information</a>
{% endblock %}

Finally, add this custom theme to the list of themes used to render backend forms:

easy_admin:
    # ...
    design:
        form_theme:
            - 'horizontal'
            # the following Twig template can be located anywhere in the application.
            # it can also be added to the twig.form_themes option to use it in the
            # entire application, not only the backend
            - 'easy_admin/form.html.twig'

Here is link for more information: https://symfony.com/doc/master/bundles/EasyAdminBundle/book/edit-new-configuration.html

Easyadmin is a bundle and you can customize all the pages of any bundle.

This logic applies to any template that lives in a bundle: just follow the convention: app/Resources/{BUNDLE_NAME}/views/{PATH/TO/TEMPLATE.html.twig}.

Suppose you've installed an imaginary open-source AcmeBlogBundle in your project. And while you're really happy with everything, you want to override the template for a blog list page. Inside the bundle, the template you want to override lives at Resources/views/Blog/index.html.twig.

To override the bundle template, just copy the index.html.twig template from the bundle to app/Resources/AcmeBlogBundle/views/Blog/index.html.twig (the app/Resources/AcmeBlogBundle directory won't exist, so you'll need to create it). You're now free to customize the template.

Reference: https://symfony.com/doc/3.4/templating/overriding.html

Upvotes: 1

Related Questions