diff --git a/src/Commands/ImportBlueprints.php b/src/Commands/ImportBlueprints.php index 147fbca7..d914c8c4 100644 --- a/src/Commands/ImportBlueprints.php +++ b/src/Commands/ImportBlueprints.php @@ -139,6 +139,11 @@ private function importFieldsets(): void $handle = Str::before($basename, '.yaml'); $handle = str_replace('/', '.', $handle); + // handle any add-on fieldsets + if (Str::startsWith($handle, 'vendor.')) { + $handle = Str::of($handle)->after('vendor.')->replaceFirst('.', '::'); + } + $fieldset = Fieldset::make($handle)->setContents(YAML::file($path)->parse()); $lastModified = Carbon::createFromTimestamp(File::lastModified($path)); @@ -161,6 +166,10 @@ private function getNamespaceAndHandle(string $blueprint): array $namespace = implode('.', $parts); $namespace = empty($namespace) ? null : $namespace; + if (Str::startsWith($namespace, 'vendor.')) { + $namespace = Str::after($namespace, 'vendor.'); + } + return [$namespace, $handle]; } diff --git a/tests/Commands/ImportBlueprintsTest.php b/tests/Commands/ImportBlueprintsTest.php index cd6b0201..f1751e42 100644 --- a/tests/Commands/ImportBlueprintsTest.php +++ b/tests/Commands/ImportBlueprintsTest.php @@ -220,4 +220,42 @@ public function it_imports_blueprints_and_fieldsets_with_force_argument() $this->assertCount(1, BlueprintModel::all()); $this->assertCount(1, FieldsetModel::all()); } + + #[Test] + public function it_imports_namespaced_blueprints_and_fieldsets() + { + BlueprintFacade::addNamespace('myaddon', __DIR__.'/__fixtures__/blueprints'); + FieldsetFacade::addNamespace('myaddon', __DIR__.'/__fixtures__/blueprints'); + + BlueprintFacade::make('test') + ->setNamespace('myaddon') + ->setContents([ + 'fields' => [ + ['handle' => 'name', 'field' => ['type' => 'text']], + ['handle' => 'email', 'field' => ['type' => 'text'], 'validate' => 'required'], + ], + ])->save(); + + FieldsetFacade::make('myaddon::test') + ->setContents([ + 'fields' => [ + ['handle' => 'foo', 'field' => ['type' => 'text']], + ['handle' => 'bar', 'field' => ['type' => 'textarea', 'validate' => 'required']], + ], + ])->save(); + + $this->assertCount(0, BlueprintModel::all()); + $this->assertCount(0, FieldsetModel::all()); + + $this->artisan('statamic:eloquent:import-blueprints', ['--force' => true]) + ->expectsOutputToContain('Blueprints imported successfully.') + ->expectsOutputToContain('Fieldsets imported successfully.') + ->assertExitCode(0); + + $this->assertCount(1, BlueprintModel::all()); + $this->assertCount(1, FieldsetModel::all()); + + $this->assertSame('myaddon', BlueprintModel::first()->namespace); + $this->assertStringContainsString('myaddon::', FieldsetModel::first()->handle); + } }