Changing editable on Sonata list field based on conditional

24 Views Asked by At

I'm trying to make my published button editable in the sonata admin list if the admin approval field is set to true. Here's a picture of it:

listview

My admin code:

...
protected function configureListFields(ListMapper $list): void
    {
        $newsStory = new NewsStory();
        $isApproved = $newsStory->isAdminApproved();      
        // var_dump($newsStory);

        $list
            ->addIdentifier('title', null, [
                'route' => [
                    'name' => 'edit',
                ]
            ])
            ->add('adminApproved', null, [
                'editable' => $this->isGranted('ROLE_ADMIN'),
            ])
            ->add('published', null, [
                'editable' => true,
                'template' => 'publish_button_template.html.twig',
            ]);
...

My custom twig template:

{# templates/admin/NewsStoryAdmin/publish_button_template.html.twig #}

{% extends get_admin_template('base_list_field', admin.code) %}


{% set is_editable = field_description.option('editable', false) and admin.hasAccess('edit', object) and object.adminApproved %}
{% set x_editable_type = field_description.type|sonata_xeditable_type %}


{% block field_span_attributes %}
    {{ dump (is_editable) }}
    {% if is_editable and x_editable_type %}
        {% apply spaceless %}
            {{ parent() }}
            data-source="[{value: 0, text: '{%- trans from 'SonataAdminBundle' %}label_type_no{% endtrans -%}'},{value: 1, text: '{%- trans from 'SonataAdminBundle' %}label_type_yes{% endtrans -%}'}]"
        {% endapply %}
    {% endif %}
{% endblock %}

{% block field %}
    {%- include '@SonataAdmin/CRUD/display_boolean.html.twig' with {
        value: value,
        inverse: field_description.option('inverse'),
    } only -%}
{% endblock %}

My issue is that I can't seem to change the logic correctly from the twig file to have it dynamically make each published button editable based on the admin approval button's status. I'm not sure if figuring out how to change the field_descriptions.option editable to false or finding out how to set the correct classes in the twig file is the best way.

0

There are 0 best solutions below