Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonata Media Bundle remove gallery

I have the Sonata Media Bundle installed but I don't use the gallery portion of the bundle.

How do I disable the Gallery?

I am using Symfony 2.3 and I have the standard Media Bundle install as per the documentation.

Solution thus far:

If you look at this issue https://github.com/sonata-project/SonataAdminBundle/issues/460 from the admin bundle you can disable a admin by adding the show_in_dashboard: false tag to the yaml file.

To do this I simply add my own compiler that adds this flag for me then:

  1. Create your compiler: http://symfony.com/doc/current/components/dependency_injection/tags.html

  2. Add your compiler to your bundle: http://symfony.com/doc/2.3/cookbook/service_container/compiler_passes.html

And you are done. If there is a better solution I'd love to hear about it.

Example of compiler:

namespace YourBundle\DependencyInjection\Compiler;

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

class OverrideMediaGalleryCompilerPass implements CompilerPassInterface
{

    /**
     * You can modify the container here before it is dumped to PHP code.
     *
     * @param ContainerBuilder $container
     *
     * @api
     */
    public function process( ContainerBuilder $container )
    {
        $definition = $container->getDefinition( 'sonata.media.admin.gallery' );
        if ( $definition ) {
            /**
             * The purpose here is to disable the sonata admin gallery from showing up
             * in the dashboard. This goes through and adds show_in_dashboard parameter
             * that disables this.
             */
            if ( $definition->hasTag( 'sonata.admin' ) ) {
                $tags                             = $definition->getTag( 'sonata.admin' );
                $tags[ 0 ][ 'show_in_dashboard' ] = false;
                $definition->clearTag( 'sonata.admin' );
                $definition->addTag( 'sonata.admin', $tags[ 0 ] );
            }
        }
    }
}
like image 457
nienaber Avatar asked Dec 27 '25 20:12

nienaber


1 Answers

Just add below service configuration into your config.yml or sonata_admin.yml file to disable gallery and media menu from admin panel or use services.yml file in config directory and load it from dependencyInjection class

#Application/Sonata/MediaBundle/DependencyInjection/ApplicationSonataMediaExtension.php
<?php

namespace Application\Sonata\MediaBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class ApplicationSonataMediaExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

Use only sonata.media.admin.gallery: service if you want to remove only gallery menu

#Application/Sonata/MediaBundle/Resources/config/services.yml
#Disable gallery & media menu from admin panel
services:
    sonata.media.admin.media:
        class: %sonata.media.admin.media.class%
        tags:
            - { name: sonata.admin, manager_type: orm, show_in_dashboard: false, label_catalogue: %sonata.media.admin.media.translation_domain% , label_translator_strategy: sonata.admin.label.strategy.underscore }
        arguments:
            - ~
            - %sonata.media.admin.media.entity%
            - %sonata.media.admin.media.controller%
            - "@sonata.media.pool"
        calls:
            - [setModelManager, ["@sonata.media.admin.media.manager"]]
            - [setTranslationDomain, [%sonata.media.admin.media.translation_domain%]]
            - [setTemplates, [{ inner_list_row: SonataMediaBundle:MediaAdmin:inner_row_media.html.twig , base_list_field: SonataAdminBundle:CRUD:base_list_flat_field.html.twig , list: SonataMediaBundle:MediaAdmin:list.html.twig , edit: SonataMediaBundle:MediaAdmin:edit.html.twig }]]

    sonata.media.admin.gallery:
        class: %sonata.media.admin.gallery.class%
        tags:
            - { name: sonata.admin, manager_type: orm, show_in_dashboard: false, label_catalogue: %sonata.media.admin.media.translation_domain% , label_translator_strategy: sonata.admin.label.strategy.underscore }
        arguments:
            - ~
            - %sonata.media.admin.gallery.entity%
            - %sonata.media.admin.gallery.controller%
            - "@sonata.media.pool"
        calls:
            - [setTranslationDomain, [%sonata.media.admin.media.translation_domain%]]
            - [setTemplates, [{ list: SonataMediaBundle:GalleryAdmin:list.html.twig }]]

then clear your cache to reflect changes

php app/console cache:clear
like image 152
tzafar Avatar answered Dec 31 '25 00:12

tzafar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!