Skip to content

Commit

Permalink
Cascade form groups on FormMapper::remove call
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelVella committed Jan 7, 2014
1 parent 217a3f3 commit 088967e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,20 @@ public function setFormGroups(array $formGroups)
$this->formGroups = $formGroups;
}

/**
* {@inheritdoc}
*/
public function removeFieldFromFormGroup($key)
{
foreach ($this->formGroups as $name => $formGroup) {
unset($this->formGroups[$name]['fields'][$key]);

if (empty($this->formGroups[$name]['fields'])) {
unset($this->formGroups[$name]);
}
}
}

/**
* @param array $group
* @param array $keys
Expand Down
18 changes: 18 additions & 0 deletions Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,15 @@ public function getFormGroups();
*/
public function setFormGroups(array $formGroups);

/**
* Remove a form group field
*
* @param $key
*
* @return void
*/
public function removeFieldFromFormGroup($key);

/**
* Returns the show groups
*
Expand Down Expand Up @@ -855,6 +864,15 @@ public function reorderShowGroup($group, array $keys);
*/
public function addFormFieldDescription($name, FieldDescriptionInterface $fieldDescription);

/**
* Remove a FieldDescription
*
* @param string $name
*
* @return void
*/
public function removeFormFieldDescription($name);

/**
* Returns true if this admin uses ACL
*
Expand Down
1 change: 1 addition & 0 deletions Form/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function has($key)
public function remove($key)
{
$this->admin->removeFormFieldDescription($key);
$this->admin->removeFieldFromFormGroup($key);
$this->formBuilder->remove($key);

return $this;
Expand Down
27 changes: 27 additions & 0 deletions Tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1139,4 +1139,31 @@ private function createTagAdmin(Post $post)

return $tagAdmin;
}

public function testRemoveFieldFromFormGroup()
{
$formGroups = array(
'foobar' => array(
'fields' => array(
'foo' => 'foo',
'bar' => 'bar',
),
)
);

$admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
$admin->setFormGroups($formGroups);

$admin->removeFieldFromFormGroup('foo');
$this->assertEquals($admin->getFormGroups(), array(
'foobar' => array(
'fields' => array(
'bar' => 'bar',
),
)
));

$admin->removeFieldFromFormGroup('bar');
$this->assertEquals($admin->getFormGroups(), array());
}
}
10 changes: 10 additions & 0 deletions Tests/Form/FormMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,14 @@ public function testWithFieldsCascadeTranslationDomain()
->add('foo', 'bar')
->end();
}

public function testRemoveCascadeRemoveFieldFromFormGroup()
{
$this->admin->expects($this->once())
->method('removeFieldFromFormGroup')
->with('foo')
;

$this->formMapper->remove('foo');
}
}

0 comments on commit 088967e

Please sign in to comment.