3

So I've an EmailAdmin configuration to manage a NewsletterBundle.

Everything works i've got the edit , list , and show default actions which works.

I looked in Vendor/SonataAdminBundle where was the template to edit/add an entity (with the "create" / "create and add" button) and i found this template : "base_edit_form.html.twig" which contains the buttons.

So i do this :

//Configure a new route to sendemail and add action to configure form fields

namespace Jade\NewsletterBundle\Admin;

use Doctrine\Common\Collections\Collection;

use Sonata\AdminBundle\Route\RouteCollection;

//use Jade\ReveBundle\Entity\Thematique;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;

class EmailAdmin extends Admin
{

    protected function configureRoutes(RouteCollection $collection) {
        $collection->add('sendemail','/sendemail'); 
    }

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('objet')
            //->add('corps')
            ->add('corps', null, array('required' => false, 'attr' => array('class' => 'ckeditor')))   


            ->add('groupes')  

         ->add('groupes', 'sonata_type_model',
                           array(
                                   'by_reference' => false,
                                   'expanded' => true,
                                   'label' => 'Groupes',
                                   'compound' => true,
                                   'multiple' => true,
                           )
                   )
       ->add('_action', 'actions', array( 'actions' => array(  
        'sendemail' => array('template' =>
        'NewsletterBundle:Admin:sendemail.html.twig'),)));


    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('objet')
            ->add('corps')
        ;
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('objet')
            ->add('corps')



        ;
    }

    public function validate(ErrorElement $errorElement, $object)
    {
        $errorElement
            ->with('objet')
                ->assertMaxLength(array('limit' => 255))
            ->end()
        ;
    }

// Here is my EmailAdminBundle :

namespace Jade\NewsletterBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;


class EmailAdminController extends Controller
{
    /**
     * @Route("/sendemail")
     * @Template()
     */
    public function sendemailAction()
    {
        return $this->render('NewsletterBundle:Admin:sendemail.html.twig');
    }


}

//in NewsletterBundle/Resources/views/Admin/sendemail.html.twig

{% block form %}
Override
    {% set url = admin.id(object) ? 'edit' : 'create' %}

    {% if not admin.hasRoute(url)%}
        <div>
            {{ "form_not_available"|trans({}, "SonataAdminBundle") }}
        </div>
    {% else %}
        <form class="form-horizontal" action="{{ admin.generateUrl(url, {'id': admin.id(object), 'uniqid': admin.uniqid, 'subclass': app.request.get('subclass')}) }}" {{ form_enctype(form) }} method="POST">
            {% if form.vars.errors|length > 0 %}
                <div class="sonata-ba-form-error">
                    {{ form_errors(form) }}
                </div>
            {% endif %}

            {% block sonata_pre_fieldsets %}{% endblock %}

            {% for name, form_group in admin.formgroups %}
                <fieldset {% if form_group.collapsed %}class="sonata-ba-fielset-collapsed"{% endif %}>
                    <legend>
                        {% if form_group.collapsed %}
                            <a href="" class="sonata-ba-collapsed" title="{{ 'link_expand'|trans({}, 'SonataAdminBundle') }}">{{ name|trans({}, admin.translationdomain) }}</a>
                        {% else %}
                            {{ name|trans({}, admin.translationdomain) }}
                        {% endif %}
                    </legend>

                    <div class="sonata-ba-collapsed-fields">
                        {% for field_name in form_group.fields %}
                            {% if admin.formfielddescriptions[field_name] is defined %}
                                {{ form_row(form[field_name])}}
                            {% endif %}
                        {% endfor %}
                    </div>
                </fieldset>
            {% endfor %}

            {% block sonata_post_fieldsets %}{% endblock %}

            {{ form_rest(form) }}

            {% block formactions %}
                <div class="well form-actions">
                    {% if app.request.isxmlhttprequest %}
                        {% if admin.id(object) %}
                            <input type="submit" class="btn btn-primary" name="btn_update" value="{{ 'btn_update'|trans({}, 'SonataAdminBundle') }}"/>
                        {% else %}
                            <input type="submit" class="btn" name="btn_create" value="{{ 'btn_create'|trans({}, 'SonataAdminBundle') }}"/>
                        {% endif %}
                    {% else %}
                        {% if admin.supportsPreviewMode %}
                            <input class="btn btn-info persist-preview" name="btn_preview" type="submit" value="{{ 'btn_preview'|trans({}, 'SonataAdminBundle') }}"/>
                        {% endif %}
                        {% if admin.id(object) %}
                            <input type="submit" class="btn btn-primary" name="btn_update_and_edit" value="{{ 'btn_update_and_edit_again'|trans({}, 'SonataAdminBundle') }}"/>
                            <input type="submit" class="btn" name="btn_update_and_list" value="{{ 'btn_update_and_return_to_list'|trans({}, 'SonataAdminBundle') }}"/>

                            {% if admin.hasroute('delete') and admin.isGranted('DELETE', object) %}
                                {{ 'delete_or'|trans({}, 'SonataAdminBundle') }}
                                <a class="btn btn-danger" href="{{ admin.generateObjectUrl('delete', object) }}">{{ 'link_delete'|trans({}, 'SonataAdminBundle') }}</a>
                            {% endif %}
                        {% else %}
                            <input class="btn btn-primary" type="submit" name="btn_create_and_edit" value="{{ 'btn_create_and_edit_again'|trans({}, 'SonataAdminBundle') }}"/>
                            <input class="btn" type="submit" name="btn_create_and_create" value="{{ 'btn_create_and_create_a_new_one'|trans({}, 'SonataAdminBundle') }}"/>
                      {% endif %}
                    {% endif %}
                </div>
            {% endblock formactions %}
        </form>
    {% endif%}

{% endblock %}

And i've got the error : Could not load type "actions", so i don't know why i've got this error ? And if it's the good way to override a specific view of a specific entity ?

Thank you for your help

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
psylo66
  • 608
  • 11
  • 26
  • did u saw it http://stackoverflow.com/questions/11725476/add-custom-button-to-edit-page-of-sonata-admin-bundle ? thought it helps. – Vladimir Ch Oct 24 '16 at 04:45

0 Answers0