From e7e4b9f3b6c4d848dcfbf5b93efcf6b259eeedf1 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 5 Jan 2021 17:00:46 +0100 Subject: [PATCH] Add removeTab method --- src/Form/FormMapper.php | 41 -------------------- src/Mapper/BaseGroupedMapper.php | 66 ++++++++++++++++++++++++++++++++ src/Show/ShowMapper.php | 42 -------------------- tests/Form/FormMapperTest.php | 10 +++++ tests/Show/ShowMapperTest.php | 11 ++++++ 5 files changed, 87 insertions(+), 83 deletions(-) diff --git a/src/Form/FormMapper.php b/src/Form/FormMapper.php index 3f91bb099e8..c9379b38e8e 100644 --- a/src/Form/FormMapper.php +++ b/src/Form/FormMapper.php @@ -208,47 +208,6 @@ public function remove($key) 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 Tab should be deleted, when the deleted group leaves the tab empty after deletion - * - * @return static - */ - 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 ('default' !== $tab) { - $group = sprintf('%s.%s', $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'], true); - - if (false !== $key) { - unset($tabs[$tab]['groups'][$key]); - } - if ($deleteEmptyTab && 0 === \count($tabs[$tab]['groups'])) { - unset($tabs[$tab]); - } - - $this->setTabs($tabs); - $this->setGroups($groups); - - return $this; - } - /** * @return FormBuilderInterface */ diff --git a/src/Mapper/BaseGroupedMapper.php b/src/Mapper/BaseGroupedMapper.php index 531bde8ad74..f59db666157 100644 --- a/src/Mapper/BaseGroupedMapper.php +++ b/src/Mapper/BaseGroupedMapper.php @@ -263,6 +263,72 @@ public function hasOpenTab() return null !== $this->currentTab; } + /** + * 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 Tab should be deleted, when the deleted group leaves the tab empty after deletion + * + * @return static + */ + 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 ('default' !== $tab) { + $group = sprintf('%s.%s', $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'], true); + + if (false !== $key) { + unset($tabs[$tab]['groups'][$key]); + } + if ($deleteEmptyTab && 0 === \count($tabs[$tab]['groups'])) { + unset($tabs[$tab]); + } + + $this->setTabs($tabs); + $this->setGroups($groups); + + return $this; + } + + /** + * Removes a tab. + * + * @return static + */ + public function removeTab(string $tab): self + { + $groups = $this->getGroups(); + $tabs = $this->getTabs(); + + foreach ($tabs[$tab]['groups'] as $group) { + if (isset($groups[$group])) { + foreach ($groups[$group]['fields'] as $field) { + $this->remove($field); + } + } + + unset($groups[$group]); + } + + unset($tabs[$tab]); + + return $this; + } + /** * @return array> */ diff --git a/src/Show/ShowMapper.php b/src/Show/ShowMapper.php index 0100e01e887..cf5cb54120b 100644 --- a/src/Show/ShowMapper.php +++ b/src/Show/ShowMapper.php @@ -122,48 +122,6 @@ public function remove($key) 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 static - */ - 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 ('default' !== $tab) { - $group = sprintf('%s.%s', $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'], true); - - if (false !== $key) { - unset($tabs[$tab]['groups'][$key]); - } - if ($deleteEmptyTab && 0 === \count($tabs[$tab]['groups'])) { - unset($tabs[$tab]); - } - - $this->setTabs($tabs); - $this->setGroups($groups); - - return $this; - } - final public function keys() { return array_keys($this->list->getElements()); diff --git a/tests/Form/FormMapperTest.php b/tests/Form/FormMapperTest.php index 1094470314d..d4993f05471 100644 --- a/tests/Form/FormMapperTest.php +++ b/tests/Form/FormMapperTest.php @@ -494,6 +494,16 @@ public function testGroupRemovingWithTabAndWithTabRemoving(): void $this->assertSame([], $this->admin->getFormTabs()); } + public function testTabRemoving(): void + { + $this->formMapper->tab('mytab')->with('foobar'); + + $this->formMapper->removeTab('mytab'); + + $this->assertSame([], $this->admin->getFormGroups()); + $this->assertSame([], $this->admin->getFormTabs()); + } + public function testKeys(): void { $this->contractor diff --git a/tests/Show/ShowMapperTest.php b/tests/Show/ShowMapperTest.php index 333b82825fa..3b75e5fa440 100644 --- a/tests/Show/ShowMapperTest.php +++ b/tests/Show/ShowMapperTest.php @@ -513,6 +513,17 @@ public function testGroupRemovingWithTabAndWithTabRemoving(): void $this->assertSame([], $this->admin->getShowTabs()); } + public function testTabRemoving(): void + { + $this->cleanShowMapper(); + + $this->showMapper->tab('mytab2')->with('groupfoo4'); + $this->showMapper->removeTab('mytab2'); + + $this->assertSame([], $this->admin->getShowGroups()); + $this->assertSame([], $this->admin->getShowTabs()); + } + public function testEmptyFieldLabel(): void { $this->showMapper->add('foo', null, ['label' => false]);