Search code examples
phpsymfonysonata-admin

Sonata Admin 4: Add an additional display to edit form


It appears to be possible to simply put a string / template to the list view, but is the same possible to the edit view of an entity in Sontata Admin 4?

I found https://docs.sonata-project.org/projects/SonataAdminBundle/en/4.x/reference/templates/#configuring-templates, but it does not give access to the form itself. This is the include I found in base_edit.html.twig:

{% block form %}
    {{ block('parentForm') }}
{% endblock %}

I would like to achieve this thou:
enter image description here

How would this be possible?


Solution

  • Ok, it looks like the help attribute of an input can be filled with markup:

    src/Admin/PageAdmin.php:

    class PageAdmin extends AbstractAdmin {
    
       // ...
       
        protected function configureFormFields(FormMapper $formMapper) : void
        {
    
            /** @var Page $page */
            $page = $this->getSubject();
            $adminHint = '';
            if ($page) {
                $adminHint = implode(', ',array_map(function (User $admin) {
                    return "<strong><a href='/admin/sso/user/{$admin->getId()}/show' target='_blank'>{$admin->getUsername()}</a></strong>";
                }, $page->getExplicitAdmins()->toArray()));
            }
    
            $formMapper
                ->add('title', TextType::class)
                // ....
                ->add('admins', ModelAutocompleteType::class, [
                    'multiple' => true,
                    'required' => false,
                    'property' => ['username', 'id'],
                    'btn_add' => false,
                    'help' => $adminHint ? "$adminHint have already explicit edit rights (might be inherited from parents). Admins entered here will also get access to all subpages." : "Admins entered here will also get access to all subpages.", // <-- some dynamic content
                    'help_html' => true, // <-- to enable html rendering
                ])
    }
    

    See also https://symfony.com/bundles/SonataAdminBundle/3.x/cookbook/recipe_image_previews.html.

    What bugs me a bit is, that this is rather messy. Rendering a template here would make it much cleaner.