sonata-project / SonataNewsBundle

[Abandoned] Symfony SonataNewsBundle
https://docs.sonata-project.org/projects/SonataNewsBundle
MIT License
151 stars 131 forks source link

Cannot remove "Comments" from menu #649

Closed kyeno closed 3 years ago

kyeno commented 3 years ago

I'm using SonataNewsBundle, yet without Comments functionality. I managed to hide any occurences of it in Post's FormMapper and ListMapper, but I also wanted to hide it in Dashboard completely.

I wrote a custom CompilerPass that should inject show_in_dashboard: false tag to that service (and in debug:container sonata.news.admin.comment it does!), but it doesn't seem to affect Sonata's menu behavior at all.

Here's my CompilerPass:

<?php
namespace App\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ThirdPartyCleanupPass implements CompilerPassInterface
{
    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container)
    {
        $servicesToHide = ['sonata.news.admin.comment', 'sonata.classification.admin.category'];

        // Hide some SonataAdmin services dashboard
        foreach($servicesToHide as $serviceName) {

            $service = $container->findDefinition($serviceName);

            // Modify 'sonata_admin' tag
            if($tag = $service->getTag('sonata.admin')) {

                // Clear
                $service->clearTags();

                // Prepend hiding
                $tag[0]['show_in_dashboard'] = (bool)false;
                //$tag[0]['show_in_dashboard'] = "false";

                // Reinject
                $service->setTags(['sonata.admin' => $tag]);
            }
        }
    }
}

If this is not the right approach, how else do I force Sonata to hide unneeded services in Admin Dashboard?

kyeno commented 3 years ago

@VincentLanglet? :)

VincentLanglet commented 3 years ago

I don't know why I'm tagged here 😅

Sorry, but we try to keep github issues for feature requests and bugs only.

In your case, it does not seems to be a Sonata issue, but a Symfony one, I'm sure you'll get a lot of help on Symfony slack. ;) I never used CompilerPass but I would say that you need to set the service after the mofication.

$container->setDefinition($serviceName);