Skip to content

Commit

Permalink
Add ability to blueprint section. (#2491)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite authored Sep 21, 2020
1 parent 0af2c7e commit 65997df
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Fields/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ public function hasField($field)
return $this->fields()->has($field);
}

public function hasSection($section)
{
return $this->sections()->has($section);
}

public function hasFieldInSection($field, $section)
{
if ($section = $this->sections()->get($section)) {
Expand Down Expand Up @@ -371,6 +376,17 @@ public function removeField($handle, $section = null)
}
}

public function removeSection($handle)
{
if (! $this->hasSection($handle)) {
return $this;
}

Arr::pull($this->contents['sections'], $handle);

return $this->resetFieldsCache();
}

public function removeFieldFromSection($handle, $section)
{
$fields = collect($this->contents['sections'][$section]['fields'] ?? []);
Expand Down
40 changes: 40 additions & 0 deletions tests/Fields/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,46 @@ public function it_removes_a_field_from_a_specific_section()
$this->assertTrue($blueprint->hasField('four'));
}

/** @test */
public function it_removes_a_specific_section()
{
$blueprint = (new Blueprint)->setHandle('test')->setContents($contents = [
'title' => 'Test',
'sections' => [
'section_one' => [
'fields' => [
['handle' => 'one', 'field' => ['type' => 'text']],
['handle' => 'two', 'field' => ['type' => 'text']],
],
],
'section_two' => [
'fields' => [
['handle' => 'three', 'field' => ['type' => 'text']],
['handle' => 'four', 'field' => ['type' => 'text']],
],
],
],
]);

$this->assertTrue($blueprint->hasSection('section_one'));
$this->assertTrue($blueprint->hasField('one'));
$this->assertTrue($blueprint->hasField('two'));
$this->assertTrue($blueprint->hasSection('section_two'));
$this->assertTrue($blueprint->hasField('three'));
$this->assertTrue($blueprint->hasField('four'));

$return = $blueprint->removeSection('section_two');

$this->assertEquals($blueprint, $return);

$this->assertTrue($blueprint->hasSection('section_one'));
$this->assertTrue($blueprint->hasField('one'));
$this->assertTrue($blueprint->hasField('two'));
$this->assertFalse($blueprint->hasSection('section_two'));
$this->assertFalse($blueprint->hasField('three'));
$this->assertFalse($blueprint->hasField('four'));
}

/** @test */
public function it_validates_unique_handles()
{
Expand Down

0 comments on commit 65997df

Please sign in to comment.