To create a new field type, you need :
- to use this type in your Sonata Admin
blast.yml
definitions - defining this field type in your
blast.yml
file - (if necessary) creating a new Twig template
- test it
e.g.:
- Use the new field type
Let's define the email
field type, which by-the-way is already defined in this bundle :
# Resources/config/blast.yml
parameters:
blast:
Librinfo\BaseEntitiesBundle\Entity\Traits\Emailable:
Sonata\AdminBundle\Datagrid\ListMapper:
_copy: Sonata\AdminBundle\Datagrid\DatagridMapper
Sonata\AdminBundle\Datagrid\DatagridMapper:
add:
email:
type: email
Sonata\AdminBundle\Show\ShowMapper:
_copy: Sonata\AdminBundle\Form\FormMapper
Sonata\AdminBundle\Form\FormMapper:
add:
General:
'':
email:
type: email
required: false
- Define the new field type
# Resources/config/blast.yml
parameters:
blast:
configuration:
templates:
show:
email: BlastCoreBundle:CRUD:list_field_email.html.twig
list:
email: BlastCoreBundle:CRUD:list_field_email.html.twig
You can see that the definition of a new field type is set within the configuration
blast parameter, using the templates
keyword. Then the subkey is set by the action it refers to (show
or list
), and it is componed by pairs of type
→ template
(written in the standard Symfony notation).
- Create a template (if needed) for it
In Resources/views/CRUD/list_field_email.html.twig
:
{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
{% block field %}
<a href="mailto:{{ value }}">{{ value }}</a>
{% endblock %}
- Test it
Go to your dashboard, find your list/show action, and verify that it's working...