Skip to content

Commit 0b15b88

Browse files
authored
Fix vendor namespaces and handle for blueprints and fieldsets not importing correctly (#310)
* Handle vendor namespaces and handle for blueprints and fieldsets * Test coverage
1 parent d05dea9 commit 0b15b88

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/Commands/ImportBlueprints.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ private function importFieldsets(): void
139139
$handle = Str::before($basename, '.yaml');
140140
$handle = str_replace('/', '.', $handle);
141141

142+
// handle any add-on fieldsets
143+
if (Str::startsWith($handle, 'vendor.')) {
144+
$handle = Str::of($handle)->after('vendor.')->replaceFirst('.', '::');
145+
}
146+
142147
$fieldset = Fieldset::make($handle)->setContents(YAML::file($path)->parse());
143148

144149
$lastModified = Carbon::createFromTimestamp(File::lastModified($path));
@@ -161,6 +166,10 @@ private function getNamespaceAndHandle(string $blueprint): array
161166
$namespace = implode('.', $parts);
162167
$namespace = empty($namespace) ? null : $namespace;
163168

169+
if (Str::startsWith($namespace, 'vendor.')) {
170+
$namespace = Str::after($namespace, 'vendor.');
171+
}
172+
164173
return [$namespace, $handle];
165174
}
166175

tests/Commands/ImportBlueprintsTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,42 @@ public function it_imports_blueprints_and_fieldsets_with_force_argument()
220220
$this->assertCount(1, BlueprintModel::all());
221221
$this->assertCount(1, FieldsetModel::all());
222222
}
223+
224+
#[Test]
225+
public function it_imports_namespaced_blueprints_and_fieldsets()
226+
{
227+
BlueprintFacade::addNamespace('myaddon', __DIR__.'/__fixtures__/blueprints');
228+
FieldsetFacade::addNamespace('myaddon', __DIR__.'/__fixtures__/blueprints');
229+
230+
BlueprintFacade::make('test')
231+
->setNamespace('myaddon')
232+
->setContents([
233+
'fields' => [
234+
['handle' => 'name', 'field' => ['type' => 'text']],
235+
['handle' => 'email', 'field' => ['type' => 'text'], 'validate' => 'required'],
236+
],
237+
])->save();
238+
239+
FieldsetFacade::make('myaddon::test')
240+
->setContents([
241+
'fields' => [
242+
['handle' => 'foo', 'field' => ['type' => 'text']],
243+
['handle' => 'bar', 'field' => ['type' => 'textarea', 'validate' => 'required']],
244+
],
245+
])->save();
246+
247+
$this->assertCount(0, BlueprintModel::all());
248+
$this->assertCount(0, FieldsetModel::all());
249+
250+
$this->artisan('statamic:eloquent:import-blueprints', ['--force' => true])
251+
->expectsOutputToContain('Blueprints imported successfully.')
252+
->expectsOutputToContain('Fieldsets imported successfully.')
253+
->assertExitCode(0);
254+
255+
$this->assertCount(1, BlueprintModel::all());
256+
$this->assertCount(1, FieldsetModel::all());
257+
258+
$this->assertSame('myaddon', BlueprintModel::first()->namespace);
259+
$this->assertStringContainsString('myaddon::', FieldsetModel::first()->handle);
260+
}
223261
}

0 commit comments

Comments
 (0)