简体   繁体   中英

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 only allows 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:
在此处输入图像描述

How would this be possible?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM