Giter Club home page Giter Club logo

Comments (13)

VincentLanglet avatar VincentLanglet commented on July 18, 2024 1

https://github.com/sonata-project/SonataAdminBundle/blob/4.x/src/Datagrid/ListMapper.php#L193

Might be interesting to add docs explaining how to use it then

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

In this case if I want to overwrite the PageAdmin I need to do like this right?

<?php

namespace App\Admin;

use Knp\Menu\ItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Show\ShowMapper;

class PageAdmin extends AbstractAdmin
{
    private AdminInterface $admin;

    public function __construct(AbstractAdmin $admin)
    {
        parent::__construct();

        $this->admin = $admin;
    }

    protected function configureRoutes(RouteCollectionInterface $collection): void
    {
        $this->admin->configureRoutes($collection);
    }

    protected function preUpdate(object $object): void
    {
        $this->admin->preUpdate($object);
    }

    protected function prePersist(object $object): void
    {
        $this->admin->prePersist($object);
    }

    protected function getAccessMapping(): array
    {
        return $this->admin->getAccessMapping();
    }

    protected function configureBatchActions(array $actions): array
    {
        return $this->admin->configureBatchActions($actions);
    }

    protected function alterNewInstance(object $object): void
    {
        $this->admin->alterNewInstance($object);
    }

    protected function configurePersistentParameters(): array
    {
        return $this->admin->configurePersistentParameters();
    }

    protected function configureShowFields(ShowMapper $show): void
    {
        $this->admin->configureShowFields($show);
    }

    protected function configureListFields(ListMapper $list): void
    {
        $this->admin->configureListFields($list);
    }

    protected function configureDatagridFilters(DatagridMapper $filter): void
    {
        $this->admin->configureDatagridFilters($filter);
    }

    protected function configureFormFields(FormMapper $form): void
    {
        $this->admin->configureFormFields($form);
    }

    protected function configureTabMenu(ItemInterface $menu, string $action, ?AdminInterface $childAdmin = null): void
    {
        $this->admin->configureTabMenu($menu, $action, $childAdmin);
    }
}
App\Admin\PageAdmin:
        decorates: sonata.page.admin.page
        arguments: [ '@App\Admin\PageAdmin.inner' ]
        tags:
            - {name: 'sonata.admin', model_class: 'App\Entity\SonataPagePage', controller: 'sonata.page.controller.admin.page', manager_type: 'orm', group: 'sonata_page', translation_domain: 'SonataPageBundle', label: 'page', label_translator_strategy: 'sonata.admin.label.strategy.underscore', icon: 'fa fa-sitemap' }

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

When I try to decorate the admin as I did above, I got this exception :'(

InvalidArgumentException:
Template named "tree" doesn't exist.

  at vendor/sonata-project/admin-bundle/src/Templating/AbstractTemplateRegistry.php:47
  at Sonata\AdminBundle\Templating\AbstractTemplateRegistry->getTemplate('tree')
     (vendor/sonata-project/page-bundle/src/Controller/PageAdminController.php:122)
  at Sonata\PageBundle\Controller\PageAdminController->treeAction(object(Request))
     (vendor/symfony/http-kernel/HttpKernel.php:153)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:75)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:25) 

from sonatapagebundle.

VincentLanglet avatar VincentLanglet commented on July 18, 2024

When I try to decorate the admin as I did above, I got this exception :'(

InvalidArgumentException:
Template named "tree" doesn't exist.

  at vendor/sonata-project/admin-bundle/src/Templating/AbstractTemplateRegistry.php:47
  at Sonata\AdminBundle\Templating\AbstractTemplateRegistry->getTemplate('tree')
     (vendor/sonata-project/page-bundle/src/Controller/PageAdminController.php:122)
  at Sonata\PageBundle\Controller\PageAdminController->treeAction(object(Request))
     (vendor/symfony/http-kernel/HttpKernel.php:153)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:75)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:25) 

It's because the template is automatically injected for the admin
https://github.com/sonata-project/SonataPageBundle/blob/4.x/src/DependencyInjection/SonataPageExtension.php#L225

would be interesting to see if it's possible to do the same for decorated services.

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

hmmmm ok, I'll check :D

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

Hey @VincentLanglet is there a other solution But I don't know if it's ok

App\Admin\PageAdmin:
        decorates: sonata.page.admin.page
        arguments:
            - '@App\Admin\PageAdmin.inner'
+        calls:
+            - setTemplate: ['tree', '@@SonataPage/PageAdmin/tree.html.twig']
        tags:
            - {name: 'sonata.admin', model_class: 'App\Entity\SonataPagePage', controller: 'sonata.page.controller.admin.page', manager_type: 'orm', group: 'sonata_page', translation_domain: 'SonataPageBundle', label: 'page', label_translator_strategy: 'sonata.admin.label.strategy.underscore', icon: 'fa fa-sitemap' }

I found others PR related with it, using a new tag

sonata-project/SonataAdminBundle#6566
sonata-project/SonataAdminBundle#6766

But it's not really clean how I can use it :/

I tested something like this

tags:
    - {name: sonata.template_registry, type: show, template:  '@@FooAdmin/CRUD/show.html.twig'}

But it doesn't work, do you have any idea how could I use it?

from sonatapagebundle.

VincentLanglet avatar VincentLanglet commented on July 18, 2024

Hey @VincentLanglet is there a other solution But I don't know if it's ok

Sure it can be done with the configuration, but you shouldn't have to know every templates to add in the configuration.
Especially one day if you want to change the tree template, you'll have to update the sonata page config and all of your admins.

I would expect something from the config here
https://github.com/sonata-project/SonataPageBundle/blob/4.x/src/DependencyInjection/SonataPageExtension.php#L223-L230
to do the job.

$container->getDefinition('sonata.page.admin.page'); is done,
I dunno if we can also get the definition of all the service which decorate this one in order to add the setTemplate call too.

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

but you shouldn't have to know every templates to add in the configuration.

That's true

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

I know it's a huge change to do in 4.x, But what do you think we split the admin in small piece of code?

for example, we extract the methods from the PageAdmin to small classes/interfaces and inject it in constructor like

https://github.com/sonata-project/SonataPageBundle/blob/4.x/src/Admin/PageAdmin.php

final class PageAdmin extends AbstractAdmin
{
    public function __construct(private AdminListFields $adminListFields) {}
    
    ....
    
    protected function configureListFields(ListMapper $list): void
    {
        $this->adminListFields-> configureListFields($list);
    }
}
interface AdminListFields
{
    public function configureListFields(ListMapper $list): void;
}

final class PageAdminListFields implements AdminListFields
{
    public function configureListFields(ListMapper $list): void
    {
        $list
            ->add('hybrid', null, ['template' => '@SonataPage/PageAdmin/field_hybrid.html.twig'])
            ->addIdentifier('name')
            ->add('type')
            ->add('pageAlias')
            ->add('site', null, [
                'sortable' => 'site.name',
            ])
            ->add('decorate', null, ['editable' => true])
            ->add('enabled', null, ['editable' => true])
            ->add('edited', null, ['editable' => true])
            ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
                'translation_domain' => 'SonataAdminBundle',
                'actions' => [
                    'edit' => [],
                ],
            ]);
    }
}

This way you just need to decorate/customize what is really necessary, and you don't need to implements too many methods that you don't want to touch like in the current solution: #1639 (comment)

in this case I will decorate like this

#[AsDecorator(decorates: PageAdminListFields::class)]
class YourCustomPageAdminListFields implements AdminListFields
{
    public function __construct(#[MapDecorated] private AdminListFields $currentPageAdminListFields) {}

    public function configureListFields(ListMapper $list): void
    {
        $this->currentPageAdminListFields-> configureListFields($list);
        $list->add('YourCustomField');
    }
}

https://symfony.com/doc/current/service_container/service_decoration.html

@VincentLanglet @jordisala1991

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

I know it's a huge change to do in 4.x, But what do you think we split the admin in small piece of code?

for example, we extract the methods from the PageAdmin to small classes/interfaces and inject it in constructor like

https://github.com/sonata-project/SonataPageBundle/blob/4.x/src/Admin/PageAdmin.php

final class PageAdmin extends AbstractAdmin
{
    public function __construct(private AdminListFields $adminListFields) {}
    
    ....
    
    protected function configureListFields(ListMapper $list): void
    {
        $this->adminListFields-> configureListFields($list);
    }
}
interface AdminListFields
{
    public function configureListFields(ListMapper $list): void;
}

final class PageAdminListFields implements AdminListFields
{
    public function configureListFields(ListMapper $list): void
    {
        $list
            ->add('hybrid', null, ['template' => '@SonataPage/PageAdmin/field_hybrid.html.twig'])
            ->addIdentifier('name')
            ->add('type')
            ->add('pageAlias')
            ->add('site', null, [
                'sortable' => 'site.name',
            ])
            ->add('decorate', null, ['editable' => true])
            ->add('enabled', null, ['editable' => true])
            ->add('edited', null, ['editable' => true])
            ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
                'translation_domain' => 'SonataAdminBundle',
                'actions' => [
                    'edit' => [],
                ],
            ]);
    }
}

This way you just need to decorate/customize what is really necessary, and you don't need to implements too many methods that you don't want to touch like in the current solution: #1639 (comment)

in this case I will decorate like this

#[AsDecorator(decorates: PageAdminListFields::class)]
class YourCustomPageAdminListFields implements AdminListFields
{
    public function __construct(#[MapDecorated] private AdminListFields $currentPageAdminListFields) {}

    public function configureListFields(ListMapper $list): void
    {
        $this->currentPageAdminListFields-> configureListFields($list);
        $list->add('YourCustomField');
    }
}

https://symfony.com/doc/current/service_container/service_decoration.html

@VincentLanglet @jordisala1991

Extension works better then my suggestion :D

I'll test that :)

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

Using extension works!

<?php

namespace App\Admin\Extension;

use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Datagrid\ListMapper;

class PageAdminExtension extends AbstractAdminExtension
{
    public function configureListFields(ListMapper $list): void
    {
        $list->add('id');
    }
}

But it added the fields in the end of the list like this

Screenshot 2022-11-07 at 09 10 29

are there anyway to put the field in the begin of table

from sonatapagebundle.

VincentLanglet avatar VincentLanglet commented on July 18, 2024

are there anyway to put the field in the begin of table

You can play with the reorder method

from sonatapagebundle.

eerison avatar eerison commented on July 18, 2024

I didn't find anything in sonata docs about reorder method 👀

from sonatapagebundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.