diff --git a/src/ContentMigrator.php b/src/ContentMigrator.php index ce1fec2..2c75ed5 100644 --- a/src/ContentMigrator.php +++ b/src/ContentMigrator.php @@ -198,7 +198,7 @@ protected function migrateField($handle, $value, $config = null) $config = $config ?? $this->getFieldConfig($handle); $fieldtype = $this->getFieldtype($config); - $migrateMethod = 'migrate'.ucfirst(strtolower($fieldtype)).'Field'; + $migrateMethod = 'migrate'.str($fieldtype)->studly().'Field'; if (method_exists($this, $migrateMethod)) { return $this->{$migrateMethod}($handle, $value, $config); @@ -387,6 +387,27 @@ protected function migrateGridRow($row, $fieldConfigs) })->all(); } + protected function migrateLinkitField($handle, $value, $config) + { + if ($value['type'] === 'asset') { + // Use Statamic's migration logic which handles URLs, but they are + // returned without container id prefixes, and LinkIt expects them. + $asset = $this->migrateAssetsField(null, $value['asset'], ['container' => $value['container']]); + $value['asset'] = collect($asset)->map(fn ($a) => $value['container'].'::'.$a)->all(); + } + + if ($value['type'] === 'term') { + $value['term'] = collect($value['term'])->map(fn ($t) => str($t)->replace('/', '::')->toString())->all(); + } + + if ($value['type'] === 'page') { + $value['type'] = 'entry'; + $value['entry'] = Arr::pull($value, 'page'); + } + + return $value; + } + /** * Migrate fieldset to blueprint. * diff --git a/src/FieldsetMigrator.php b/src/FieldsetMigrator.php index 2ed069c..f0ee44f 100644 --- a/src/FieldsetMigrator.php +++ b/src/FieldsetMigrator.php @@ -181,7 +181,7 @@ protected function migrateFieldConfig($config, $handle) $fieldtype = $config['type'] ?? 'text'; - $migrateMethod = 'migrate'.ucfirst(strtolower($fieldtype)).'Field'; + $migrateMethod = 'migrate'.str($fieldtype)->studly().'Field'; if (method_exists($this, $migrateMethod)) { $config = $this->{$migrateMethod}($config, $handle); @@ -524,6 +524,19 @@ protected function migrateDateField($config, $handle) return $config; } + protected function migrateLinkitField($config, $handle) + { + // LinkIt in v2 always allowed pages without any extra config. + // In v3 it only deals with entries, so we'll add pages all the time. + if ($config->has('collections')) { + $config['collections'] = collect($config['collections'])->push('pages')->unique()->all(); + } else { + $config['collections'] = ['pages']; + } + + return $config; + } + /** * Convert partial field to import. * diff --git a/tests/ContentMigratorTest.php b/tests/ContentMigratorTest.php index 52bac16..147dc55 100644 --- a/tests/ContentMigratorTest.php +++ b/tests/ContentMigratorTest.php @@ -629,6 +629,34 @@ public function it_can_migrate_bards_with_no_sets() $this->assertEquals($expected, $content); } + /** @test */ + public function it_can_migrate_link_it_fields() + { + $content = $this + ->setFields([ + 'url' => ['type' => 'link_it'], + 'asset' => ['type' => 'link_it', 'containers' => ['main']], + 'term' => ['type' => 'link_it', 'taxonomies' => ['tags']], + 'page' => ['type' => 'link_it'], + ]) + ->migrateContent([ + 'url' => ['type' => 'url', 'url' => 'http://example.com'], + 'asset' => ['type' => 'asset', 'asset' => ['/assets/img/coffee-mug.jpg'], 'container' => 'main'], + 'term' => ['type' => 'term', 'term' => ['tags/coffee'], 'taxonomy' => 'tags'], + 'page' => ['type' => 'page', 'page' => ['abc']], + ]); + + $expected = [ + 'url' => ['type' => 'url', 'url' => 'http://example.com'], + 'asset' => ['type' => 'asset', 'asset' => ['main::img/coffee-mug.jpg'], 'container' => 'main'], + 'term' => ['type' => 'term', 'term' => ['tags::coffee'], 'taxonomy' => 'tags'], + 'page' => ['type' => 'entry', 'entry' => ['abc']], + 'blueprint' => 'speaker', + ]; + + $this->assertEquals($expected, $content); + } + /** @test */ public function it_can_migrate_custom_layout() { diff --git a/tests/MigrateFieldsetTest.php b/tests/MigrateFieldsetTest.php index a14947f..b3b0bcf 100644 --- a/tests/MigrateFieldsetTest.php +++ b/tests/MigrateFieldsetTest.php @@ -795,6 +795,41 @@ public function it_migrates_date_field() $this->assertEquals($expected, $fieldset); } + + /** @test */ + public function it_migrates_link_it_field() + { + $fieldset = $this->migrateFieldset([ + 'title' => 'Posts', + 'fields' => [ + 'url' => ['type' => 'link_it'], + 'entries' => ['type' => 'link_it', 'collections' => ['blog', 'products']], + ], + ]); + + $expected = [ + 'title' => 'Posts', + 'fields' => [ + [ + 'handle' => 'url', + 'field' => [ + 'type' => 'link_it', + 'collections' => ['pages'] + ], + ], + [ + 'handle' => 'entries', + 'field' => [ + 'type' => 'link_it', + 'collections' => ['blog', 'products', 'pages'] + ], + ], + ], + ]; + + $this->assertEquals($expected, $fieldset); + } + /** @test */ public function it_migrates_extention_validation() {