sonata-project / SonataMediaBundle

Symfony SonataMediaBundle
https://docs.sonata-project.org/projects/SonataMediaBundle
MIT License
451 stars 495 forks source link

The 'context' filter in MediaAdmin class is either expanded or completely disabled #1252

Closed vyshkant closed 4 years ago

vyshkant commented 7 years ago

Environment

Sonata packages

$ composer show sonata-project/*
sonata-project/admin-bundle              3.17.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.3.2  Symfony SonataBlockBundle
sonata-project/cache                     1.0.7  Cache library
sonata-project/core-bundle               3.3.0  Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.2.1  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.0.2  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.1.4  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.2.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.7.1  Lightweight Exporter library
sonata-project/media-bundle              3.5.1  Symfony SonataMediaBundle
sonata-project/notification-bundle       3.1.0  Symfony SonataNotificationBundle

Symfony packages

$ composer show symfony/*
symfony/assetic-bundle     v2.8.1 Integrates Assetic into Symfony2
symfony/monolog-bundle     v3.1.0 Symfony MonologBundle
symfony/phpunit-bridge     v3.2.7 Symfony PHPUnit Bridge
symfony/polyfill-apcu      v1.3.0 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-intl-icu  v1.3.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  v1.3.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56     v1.3.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.3.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util      v1.3.0 Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.0 Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v2.5.4 Symfony SwiftmailerBundle
symfony/symfony            v3.2.7 The Symfony PHP framework

PHP version

$ php -v
PHP 7.1.4-1+deb.sury.org~xenial+1 (cli) (built: Apr 11 2017 22:12:32) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.4-1+deb.sury.org~xenial+1, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.5.1, Copyright (c) 2002-2017, by Derick Rethans

Subject

I have a problem with filters in the overridden MediaAdmin class.

I want to disable the output of the 'context' filter by default, but I do not want to completely abandon the logic of the base class Sonata\MediaBundle\Admin\ORM\MediaAdmin.

The current logic of Sonata\MediaBundle\Admin\ORM\MediaAdmin class allows you to select one of the two:

So there is no way to let 'context' filter be available in the list of the filters (not disabled) and at the same time be not expanded.

Steps to reproduce

I have overridden the MediaAdmin class, which is providing the Media admin page, to my own:

#config.yml

parameters:
    sonata.media.admin.media.class: Overridden\Sonata\MediaBundle\Admin\MediaAdmin

Next I've created the Overridden\Sonata\MediaBundle\Admin\MediaAdmin class extending from the default Sonata\MediaBundle\Admin\ORM\MediaAdmin class:

<?php

namespace Overridden\Sonata\MediaBundle\Admin;

use Sonata\MediaBundle\Admin\ORM\MediaAdmin as Admin;

class MediaAdmin extends Admin
{
    public function getPersistentParameters()
    {
        $parameters = parent::getPersistentParameters();

        $parameters['context'] = null;
        $parameters['hide_context'] = true;

        return $parameters;
    }
}

Here overridden the getPersistentParameters method to override some parameters. Specifically, I did two things:

Expected results

I expected the 'context' filter will be hidden (won't be expanded, but will be still available in the 'Filters' dropdown menu). The keyword hide_context makes me think that the 'context' filter should be hidden.

Actual results

In the fact the 'context' filter was completely disabled (became unavailable in the 'Filters' dropdown menu).

Possible causes

I think this is caused by the fact, that the show_filter property of the $filterOptions parameter of the Sonata\AdminBundle\Datagrid\DatagridMapper::add method is always being set to a boolean value, whereas null value gives us the expected result.

<?php

namespace Sonata\MediaBundle\Admin\ORM;

use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\MediaBundle\Admin\BaseMediaAdmin as Admin;

class MediaAdmin extends Admin
{
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        ...
        $datagridMapper
            ...
            ->add('context', null, array(
                'show_filter' => $this->getPersistentParameter('hide_context') !== true,
            ), $choiceType, $options);
        ...
    }
}

As we can see, the show_filter is always being set to either true or false (which mean 'to be shown expanded' or 'to be disabled'), but never being set to null (which means 'to be neither expanded nor disabled').

I'm not sure if this is a bug of SonataMediaBundle or SonataAdminBundle.

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.