9

In admin class:

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
            ->add('name', 'text')
            ->add('description', 'text')
    ;
}

I don't know how I can before "name" add javascript, can you help me?

3
  • Do you need to add javascript for a specific field (name field in this case)? Can you tell me more about what do you want to do? Commented Jan 22, 2014 at 19:17
  • I want create autocomplete name from ajax. Commented Jan 22, 2014 at 20:01
  • Thanks, now I know what to answer. Hold on for a minute, I'm writing an answer for you. Commented Jan 22, 2014 at 20:05

3 Answers 3

25

Working for me:

In admin class src\PP\TestBundle\TestAdmin.php

public function configure() {
    $this->setTemplate('edit', 'PPTestBundle:CRUD:edit_javascript.html.twig');
}

In src\PP\TestBundle\Resources\views\edit_javascript.html.twig

{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('bundles/pptest/admin/js/myscripts.js') }}" type="text/javascript"></script>
{% endblock %}

When you do all this stuff and you have upload myscripts.js you should send this in command line:

app/console assets:install web

(possible that I forgot something)

Sorry for my bad English :<>

Sign up to request clarification or add additional context in comments.

2 Comments

If you need to install assets from the bundle, use app/console assets:install web --symlink instead, to avoid copying the resources...
setTemplate can also be called using the service configuration as described here
5

EDITED

You need to create a custom TWIG template for it (where you could place your javascript code just before the widget code).

Then you write inside ap/config/config.yml where your custom template is to allow Symfony and SonataAdmin to recognize it.

You have some info here Sonata Admin - Custom template

More info here customize field types

An example could be something like this:

Admin class

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
            ->add('name', 'ajax_autocomplete')
            ->add('description', 'text')
    ;
}

And, in the TWIG template you need to extend from the Sonata Admin field template that better fits your necessities. In this case maybe base_edit.html.twig or edit_text.html.twig

You have a list of templates to extend from, inside this Sonata Admin dir: vendor\sonata-project\admin-bundle\Sonata\AdminBundle\Resources\views\CRUD

Customization

Imagine you have placed your custom template inside XXXBundle:YYY:ajax_autocomplete.html.twig

I think it should work if you write a line here:

sonata_doctrine_orm_admin:
    templates:
        types:
            list:
                ajax_autocomplete: XXXBundle:YYY:ajax_autocomplete.html.twig

6 Comments

Yea but, now shows error: The option "template" does not exist. Known options are: "action", "attr", "auto_initialize" (...)
It's means that: We can't use custom template in create/edit page. ##EDIT## Maybe I should use filter?
Now I have an error: Could not load type "ajax_autocomplete". It works as I add in "configureListFields", but does not work in method "configureFormFields" where it is needed.
Maybe I was wrong, you don't have to add it to the sonata_doctrine_orm_admin config at /app/config/config.yml. Why? Because is not related to Sonata Admin, but Symfony2. Read the following documentation, please: symfony.com/doc/current/cookbook/form/…
Have you tried the solution from the documentation (the comment above this)? I'm interested on it.
|
1

Starting from sonata admin 3.x you can add/remove js/css to/from the page without extending it.

sonata_admin:
    ....
    assets:
        # javascript paths to add to the page in addition to the list above
        extra_javascripts:
            - 'your js file path'
        # javascript paths to remove from the page
        remove_javascripts: 
            - 'your js file path'

you can find more information here https://github.com/sonata-project/SonataAdminBundle/pull/4836/files?short_path=e252be0#diff-e252be027e26148c11d971dc969f4be0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.