Skip to content

Commit 47934d7

Browse files
authored
Allow 'hidden' blueprints to be un-hidden
1 parent 29ceb65 commit 47934d7

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

src/Fields/BlueprintRepository.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,34 @@ protected function getNamespaceAndHandle($blueprint)
114114
return [$namespace, $handle];
115115
}
116116

117+
public function getModel($blueprint)
118+
{
119+
return app('statamic.eloquent.blueprints.blueprint_model')::where('namespace', $blueprint->namespace() ?? null)
120+
->where('handle', $blueprint->handle())
121+
->first();
122+
}
123+
117124
public function updateModel($blueprint)
118125
{
119126
$model = app('statamic.eloquent.blueprints.blueprint_model')::firstOrNew([
120127
'handle' => $blueprint->handle(),
121128
'namespace' => $blueprint->namespace() ?? null,
122129
]);
123130

124-
$model->data = $this->addOrderToBlueprintSections($blueprint->contents());
131+
$data = $this->addOrderToBlueprintSections($blueprint->contents());
132+
133+
if (! $blueprint->hidden()) {
134+
unset($data['hide']);
135+
}
136+
137+
$model->data = $data;
138+
125139
$model->save();
126140
}
127141

128142
public function deleteModel($blueprint)
129143
{
130-
$model = app('statamic.eloquent.blueprints.blueprint_model')::where('namespace', $blueprint->namespace() ?? null)
131-
->where('handle', $blueprint->handle())
132-
->first();
144+
$model = $this->getModel($blueprint);
133145

134146
if ($model) {
135147
$model->delete();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Tests\Data\Fields;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Statamic\Facades\Blueprint;
7+
use Tests\TestCase;
8+
9+
class BlueprintTest extends TestCase
10+
{
11+
use RefreshDatabase;
12+
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->app->singleton(
18+
'Statamic\Fields\BlueprintRepository',
19+
'Statamic\Eloquent\Fields\BlueprintRepository'
20+
);
21+
22+
$this->app->singleton(
23+
'Statamic\Fields\FieldsetRepository',
24+
'Statamic\Eloquent\Fields\FieldsetRepository'
25+
);
26+
27+
$this->app->bind('statamic.eloquent.blueprints.blueprint_model', function () {
28+
return \Statamic\Eloquent\Fields\BlueprintModel::class;
29+
});
30+
31+
$this->app->bind('statamic.eloquent.blueprints.fieldset_model', function () {
32+
return \Statamic\Eloquent\Fields\FieldsetModel::class;
33+
});
34+
}
35+
36+
/** @test */
37+
public function it_saves_and_removes_hidden_on_model()
38+
{
39+
$blueprint = Blueprint::make()
40+
->setHandle('test')
41+
->setHidden(true)
42+
->save();
43+
44+
$model = Blueprint::getModel($blueprint);
45+
46+
$this->assertTrue($model->data['hide']);
47+
48+
$blueprint->setHidden(false)->save();
49+
50+
$model = Blueprint::getModel($blueprint);
51+
52+
$this->assertFalse(isset($model->data['hide']));
53+
}
54+
55+
/** @test */
56+
public function it_deletes_the_model_when_the_blueprint_is_deleted()
57+
{
58+
$blueprint = Blueprint::make()
59+
->setHandle('test')
60+
->setHidden(true)
61+
->save();
62+
63+
$model = Blueprint::getModel($blueprint);
64+
65+
$this->assertNotNull($model);
66+
67+
$blueprint->delete();
68+
69+
$model = Blueprint::getModel($blueprint);
70+
71+
$this->assertNull($model);
72+
}
73+
}

0 commit comments

Comments
 (0)