Skip to content

Commit

Permalink
Add removeTab method (#6746)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 8, 2021
1 parent b3f2de4 commit dc1fa9a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 83 deletions.
41 changes: 0 additions & 41 deletions src/Form/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
69 changes: 69 additions & 0 deletions src/Mapper/BaseGroupedMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,75 @@ 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]);

$this->setTabs($tabs);
$this->setGroups($groups);

return $this;
}

/**
* @return array<string, array<string, mixed>>
*/
Expand Down
42 changes: 0 additions & 42 deletions src/Show/ShowMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions tests/Form/FormMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/Show/ShowMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit dc1fa9a

Please sign in to comment.