瀏覽代碼

Added removeGroup() method to ShowMapper (#4097)

Marcos Bezerra de Menezes 8 年之前
父節點
當前提交
17bb9e9655
共有 3 個文件被更改,包括 120 次插入0 次删除
  1. 27 0
      Resources/doc/reference/action_show.rst
  2. 42 0
      Show/ShowMapper.php
  3. 51 0
      Tests/Show/ShowMapperTest.php

+ 27 - 0
Resources/doc/reference/action_show.rst

@@ -58,6 +58,33 @@ To specify options, do as follow:
             ;
     }
 
+When extending an existing Admin, you may want to remove some fields, groups or tabs.
+Here is an example of how to achieve this :
+
+.. code-block:: php
+
+    <?php
+    // src/AppBundle/Admin/PersonAdmin.php
+
+    class PersonAdmin extends ParentAdmin
+    {
+        public function configureShowFields(ShowMapper $showMapper)
+        {
+            parent::configureShowFields($showMapper);
+
+            // remove just one field
+            $showMapper->remove('field_to_remove');
+
+            // remove a group from the "default" tab
+            $showMapper->removeGroup('GroupToRemove1');
+
+            // remove a group from a specific tab
+            $showMapper->removeGroup('GroupToRemove2', 'Tab2');
+
+            // remove a group from a specific tab and also remove the tab if it ends up being empty
+            $showMapper->removeGroup('GroupToRemove3', 'Tab3', true);
+    }
+
 Customising the query used to show the object from within your Admin class
 --------------------------------------------------------------------------
 

+ 42 - 0
Show/ShowMapper.php

@@ -113,6 +113,48 @@ class ShowMapper extends BaseGroupedMapper
         return $this;
     }
 
+    /**
+     * Removes a group.
+     *
+     * @param string $group          The group to delete
+     * @param string $tab            The tab the group belongs to, defaults to 'default'
+     * @param bool   $deleteEmptyTab Whether or not the parent Tab should be deleted too,
+     *                               when the deleted group leaves the tab empty after deletion
+     *
+     * @return $this
+     */
+    public function removeGroup($group, $tab = 'default', $deleteEmptyTab = false)
+    {
+        $groups = $this->getGroups();
+
+        // When the default tab is used, the tabname is not prepended to the index in the group array
+        if ($tab !== 'default') {
+            $group = $tab.'.'.$group;
+        }
+
+        if (isset($groups[$group])) {
+            foreach ($groups[$group]['fields'] as $field) {
+                $this->remove($field);
+            }
+        }
+        unset($groups[$group]);
+
+        $tabs = $this->getTabs();
+        $key = array_search($group, $tabs[$tab]['groups']);
+
+        if (false !== $key) {
+            unset($tabs[$tab]['groups'][$key]);
+        }
+        if ($deleteEmptyTab && count($tabs[$tab]['groups']) == 0) {
+            unset($tabs[$tab]);
+        }
+
+        $this->setTabs($tabs);
+        $this->setGroups($groups);
+
+        return $this;
+    }
+
     /**
      * {@inheritdoc}
      */

+ 51 - 0
Tests/Show/ShowMapperTest.php

@@ -15,6 +15,7 @@ use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
 use Sonata\AdminBundle\Builder\ShowBuilderInterface;
 use Sonata\AdminBundle\Show\ShowMapper;
+use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin;
 use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
 
 /**
@@ -404,6 +405,56 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
             ), ), true), print_r($this->admin->getShowGroups(), true));
     }
 
+    public function testGroupRemovingWithoutTab()
+    {
+        $this->cleanShowMapper();
+
+        $this->showMapper->with('groupfoo1');
+        $this->showMapper->removeGroup('groupfoo1');
+
+        $this->assertSame(array(), $this->admin->getShowGroups());
+    }
+
+    public function testGroupRemovingWithTab()
+    {
+        $this->cleanShowMapper();
+
+        $this->showMapper->tab('mytab')->with('groupfoo2');
+        $this->showMapper->removeGroup('groupfoo2', 'mytab');
+
+        $this->assertSame(array(), $this->admin->getShowGroups());
+    }
+
+    public function testGroupRemovingWithoutTabAndWithTabRemoving()
+    {
+        $this->cleanShowMapper();
+
+        $this->showMapper->with('groupfoo3');
+        $this->showMapper->removeGroup('groupfoo3', 'default', true);
+
+        $this->assertSame(array(), $this->admin->getShowGroups());
+        $this->assertSame(array(), $this->admin->getShowTabs());
+    }
+
+    public function testGroupRemovingWithTabAndWithTabRemoving()
+    {
+        $this->cleanShowMapper();
+
+        $this->showMapper->tab('mytab2')->with('groupfoo4');
+        $this->showMapper->removeGroup('groupfoo4', 'mytab2', true);
+
+        $this->assertSame(array(), $this->admin->getShowGroups());
+        $this->assertSame(array(), $this->admin->getShowTabs());
+    }
+
+    private function cleanShowMapper()
+    {
+        $this->showBuilder = $this->getMock('Sonata\AdminBundle\Builder\ShowBuilderInterface');
+        $this->fieldDescriptionCollection = new FieldDescriptionCollection();
+        $this->admin = new CleanAdmin('code', 'class', 'controller');
+        $this->showMapper = new ShowMapper($this->showBuilder, $this->fieldDescriptionCollection, $this->admin);
+    }
+
     private function getFieldDescriptionMock($name = null, $label = null)
     {
         $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');