Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add JS code to entity form?

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

like image 247
Ivan Fateev Avatar asked Apr 13 '12 14:04

Ivan Fateev


People also ask

How to add JavaScript to dynamics CRM?

Login to CRM then go to Settings=>Customization=>Customize the system. 6. The event tab shows all of the JavaScript libraries related to the form. To add a new JavaScript library click on Add.

Can you write JavaScript in Powerapps?

Both the basic form and advanced form step records contain a field named Custom JavaScript that can be used to store JavaScript code to allow you to extend or modify the form's visual display or function. The custom block of JavaScript will be added to the bottom of the page just before the closing form tag element.

What JavaScript library is included with power apps portals?

Power Apps Portals includes jQuery, so why would we get this error? Because of where jQuery is included. jQuery is included after the Header Web Template.


1 Answers

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>
like image 180
Amit Avatar answered Oct 09 '22 11:10

Amit