Sonata Media Bundle remove gallery

ぃ、小莉子 提交于 2019-12-23 21:14:16

问题


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 ] );
            }
        }
    }
}

回答1:


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



回答2:


I've achieved this by listing the allowed items for the dashboard in sonata.yaml, effectively hiding the gallery:

sonata_admin:
    dashboard:
        groups:
            sonata_media:
                label: "Media Library"
                label_catalogue: SonataMediaBundle
                items:
                    - sonata.media.admin.media



回答3:


why don't you just edit app\config\sonata\sonata_admin.yml file end comment or remove line containing sonata.media.admin.gallery?




回答4:


The quickest way - but - dirty way it to edit the sonata media config file:

vendor/sonata-project/media-bundle/Resources/config/doctrine_orm_admin.xml

You only need too add this

show_in_dashboard="false"

in the tag attribute of the service sonata.media.admin.gallery

    <tag name="sonata.admin" show_in_dashboard="false"  manager_type="orm" group="sonata_media" label="gallery" label_catalogue="%sonata.media.admin.gallery.translation_domain%" label_translator_strategy="sonata.admin.label.strategy.underscore"/>

If you use mongodb or phpcr then edit the corresponding file.

Also you can do the same for the sonata.media.admin.media service in the same file, so all the "Media" block in the admin will disapeared. But the services are still enable, so you manage your picture in your own entity admin as a sub-entity.

I hope this will help someone as it took me 30min to find the right file ...



来源:https://stackoverflow.com/questions/22080760/sonata-media-bundle-remove-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!