Search code examples
phpsymfonyeasyadmin

How do I apply a condition in my CrudController in symfony easyAdmin?


I have a BorrowingCrudController with an expected_return_date field and a final_return_date field.

What I want is to create a condition that check the value of the final_return_date field, and then call the setTemplatePath method to render expected_return_date using a custom template if the final_return_date field is null, or render it normally if not.

Here is my code:

    public function configureFields(string $pageName): iterable
    {
        yield IntegerField::new('id')->hideOnForm();
        yield AssociationField::new('user', 'Utilisateur');
        yield AssociationField::new('book', 'Titre du livre');
        yield AssociationField::new('book', 'ID du livre')->formatValue(function ($value) {
            return $value->getId();
        });

        yield BooleanField::new('prolongated', 'Prolongé')->renderAsSwitch(false);

        yield DateTimeField::new('borrowingDate', 'Date d\'emprunt')
            ->formatValue(function ($value) {
                return $value->format('d-m-Y');
            });

        yield DateTimeField::new('expectedReturnDate', 'Date de retour prévu')->formatValue(function ($value) {
            return $value->format('d-m-Y');
        })
            ->setTemplatePath('admin/field/date_expected_return.html.twig');


        yield DateTimeField::new('finalReturnDate', 'Date de retour finale')->formatValue(function ($value) {
            if ($value === null) {
                return null;
            } else {
                return $value->format('d-m-Y');
            }
        });
    }

I tried creating a bool variable and then call the formatValue method to retrieve the value of the final_return_date field and then modify the bool accordingly to display the corresponding template, but it didn't work.


Solution

  • Ok so I managed to what I wanted, here is how : so instead of rendering a custom template based on a condition in the crud controller, I had to override the index.html.twig template in the EasyAdmin Bundle. To do so I went to the index.html.twig file located at

    vendor\easycorp\easyadmin-bundle\src\Resources\views\crud\index.html.twig

    and in the {% block table_body %}

    I put the for loop that iterate throught the fields in entity.fields in another block like so :

                            {% block fieldsForLoop %}
                            {% for field in entity.fields %}
                                {% set is_searchable = null == ea.crud.searchFields or field.property in ea.crud.searchFields %}
                                <td data-column="{{ field.property }}" data-label="{{ field.label|trans|e('html_attr') }}" class="{{ is_searchable ? 'searchable' }} {{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}">
                                    {{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }}
                                </td>
                            {% endfor %}
                            {% endblock %}
    

    Then to override the base template, I created another index.html.twig file in :

    templates\bundles\EasyAdminBundle\crud\index.html.Twig

    and added the following code in it :

    {% extends '@!EasyAdmin/crud/index.html.twig' %}
    
    {% block fieldsForLoop %}
    
                            {% for field in entity.fields %}
    
    
                                {% set is_searchable = null == ea.crud.searchFields or field.property in ea.crud.searchFields %}
                                {% if (field.property == 'expectedReturnDate' and field.formattedValue|date("Y-m-d") < 'now'|date("Y-m-d") and isReturned == false) %}
                                <td class="text-danger" data-column="{{ field.property }}" data-label="{{ field.label|trans|e('html_attr') }}" class="{{ is_searchable ? 'searchable' }} {{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}">
                                    {{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }}
                                </td>
                                {% else %}
                                <td data-column="{{ field.property }}" data-label="{{ field.label|trans|e('html_attr') }}" class="{{ is_searchable ? 'searchable' }} {{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}">
                                    {{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }}
                                </td>
                                {% endif %}
    
                                {% if field.property == 'finalReturnDate' and field.value == null %} 
                                {% set isReturned = false %} 
                                {% else %} 
                                {% set isReturned = true %} 
                                {% endif %}
    
                            {% endfor %}
    
    
    {% endblock %}
    

    and now the expected return date field only turns red when the date is passed and the final return date is null, which was my initial goal.

    I had to modify my BorrowingCrudController a bit though, I needed to add the finalReturnDate field before the expectedReturnDate, like that the loop goes iterate over the finalReturnDate first and set the isReturned variable to false if it is null.