6

I have an entity. I need to execute some JS code when entity loads in a popup. Is there any way to add a custom JS/HTML code to an entity form via admin class configuration. E.g. to pass a template as an option

Ivan Fateev
  • 1,032
  • 1
  • 10
  • 26

2 Answers2

7

You can do it this way:-

  1. Add a class parameter in your FormMapper like this:-

    protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('description', null, array('attr' => array('class' => 'for_popup'), 'required' => false)) }

  2. Extend the edit.html.twig / base_edit.html.twig from Sonata CRUD Templates

---edit.html.twig----

{% extends 'YourBundle:YourAdminClass:base_edit.html.twig' %}

---base_edit.html.twig---

{% block javascripts %}
    {{ parent() }}
    <script type="text/javascript">
        // Your JS code here
    </script>
{% endblock %}

Use your edit.html.twig instead of Sonata CRUD's by defining it in the getEditTemplate function (within your Admin class).

public function getEditTemplate() 
{
    return 'YourAdminBundle:ControllerName:edit.html.twig'; 
}

You can also set the custom edit template when you inject the admin service.

<service id="sonata.admin.bf" class="Wyzbiz\Bundle\MainBundle\Admin\BfAdmin">
    <tag name="sonata.admin" manager_type="orm" group="Content" label="BFs"/>
    <argument />
    <argument>Wyzbiz\Bundle\MainBundle\Entity\Bf</argument>
    <argument>WyzbizMainBundle:CRUD</argument>
    <call method="setTranslationDomain"><argument>WyzbizMainBundle</argument></call>
    <call method="setTemplate"><argument>list</argument>                       
    <argument>WyzbizMainBundle:CRUD/Bf:list.html.twig</argument></call>
</service>
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
Amit
  • 3,644
  • 9
  • 38
  • 49
  • base_edit should extend sonata crud template? I should specify extending tag in this template or I just can place this template same path as in sonata bundle? Thank you for your help – Ivan Fateev May 27 '12 at 08:35
  • Don't touch anything in Sonata. Just copy sonata's base_edit.html.twig into your YourBundle/Resources/YourController/ folder. I'll update the answer to explain how. – Amit May 28 '12 at 09:25
  • Sonata has apparently replaced the getEditTemplate() function with getTemplate('edit'), making this more difficult. What I ended up doing was: $this->setTemplate('edit', 'MyBundle:Admin:edit_user.html.twig'); inside my admin's configureFormFields() – Jessica Aug 13 '13 at 21:04
  • Also, your edit.html.twig file can extend sonata admin's CRUD edit.html.twig and can contain the javascript block (in other words, you don't need to create your own version of base_edit.html.twig and extend it, just extend Sonata admin's edit.html.twig directly) – Tocacar Nov 26 '13 at 14:54
3

@Jessica Instead of using $this->setTemplate() inside the configureFormFields method of your admin class, you can instead add your own implementation of the getTemplate method, mine looks like this:

/**
 * Override core method to display custom template(s)
 */
public function getTemplate($name)
{
    switch ($name) {
        case 'edit':
            return 'YourAdminBundle:YourAdminEntity:edit.html.twig';
            break;
        default:
            return parent::getTemplate($name);
            break;
    }
}
Tocacar
  • 475
  • 3
  • 12