Skip to content

Commit 86fe776

Browse files
authored
LinkIt support (#119)
* Use studly case * wip * bump * tests
1 parent 4d349b7 commit 86fe776

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

src/ContentMigrator.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ protected function migrateField($handle, $value, $config = null)
198198
$config = $config ?? $this->getFieldConfig($handle);
199199
$fieldtype = $this->getFieldtype($config);
200200

201-
$migrateMethod = 'migrate'.ucfirst(strtolower($fieldtype)).'Field';
201+
$migrateMethod = 'migrate'.str($fieldtype)->studly().'Field';
202202

203203
if (method_exists($this, $migrateMethod)) {
204204
return $this->{$migrateMethod}($handle, $value, $config);
@@ -387,6 +387,27 @@ protected function migrateGridRow($row, $fieldConfigs)
387387
})->all();
388388
}
389389

390+
protected function migrateLinkitField($handle, $value, $config)
391+
{
392+
if ($value['type'] === 'asset') {
393+
// Use Statamic's migration logic which handles URLs, but they are
394+
// returned without container id prefixes, and LinkIt expects them.
395+
$asset = $this->migrateAssetsField(null, $value['asset'], ['container' => $value['container']]);
396+
$value['asset'] = collect($asset)->map(fn ($a) => $value['container'].'::'.$a)->all();
397+
}
398+
399+
if ($value['type'] === 'term') {
400+
$value['term'] = collect($value['term'])->map(fn ($t) => str($t)->replace('/', '::')->toString())->all();
401+
}
402+
403+
if ($value['type'] === 'page') {
404+
$value['type'] = 'entry';
405+
$value['entry'] = Arr::pull($value, 'page');
406+
}
407+
408+
return $value;
409+
}
410+
390411
/**
391412
* Migrate fieldset to blueprint.
392413
*

src/FieldsetMigrator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected function migrateFieldConfig($config, $handle)
181181

182182
$fieldtype = $config['type'] ?? 'text';
183183

184-
$migrateMethod = 'migrate'.ucfirst(strtolower($fieldtype)).'Field';
184+
$migrateMethod = 'migrate'.str($fieldtype)->studly().'Field';
185185

186186
if (method_exists($this, $migrateMethod)) {
187187
$config = $this->{$migrateMethod}($config, $handle);
@@ -524,6 +524,19 @@ protected function migrateDateField($config, $handle)
524524
return $config;
525525
}
526526

527+
protected function migrateLinkitField($config, $handle)
528+
{
529+
// LinkIt in v2 always allowed pages without any extra config.
530+
// In v3 it only deals with entries, so we'll add pages all the time.
531+
if ($config->has('collections')) {
532+
$config['collections'] = collect($config['collections'])->push('pages')->unique()->all();
533+
} else {
534+
$config['collections'] = ['pages'];
535+
}
536+
537+
return $config;
538+
}
539+
527540
/**
528541
* Convert partial field to import.
529542
*

tests/ContentMigratorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,34 @@ public function it_can_migrate_bards_with_no_sets()
629629
$this->assertEquals($expected, $content);
630630
}
631631

632+
/** @test */
633+
public function it_can_migrate_link_it_fields()
634+
{
635+
$content = $this
636+
->setFields([
637+
'url' => ['type' => 'link_it'],
638+
'asset' => ['type' => 'link_it', 'containers' => ['main']],
639+
'term' => ['type' => 'link_it', 'taxonomies' => ['tags']],
640+
'page' => ['type' => 'link_it'],
641+
])
642+
->migrateContent([
643+
'url' => ['type' => 'url', 'url' => 'http://example.com'],
644+
'asset' => ['type' => 'asset', 'asset' => ['/assets/img/coffee-mug.jpg'], 'container' => 'main'],
645+
'term' => ['type' => 'term', 'term' => ['tags/coffee'], 'taxonomy' => 'tags'],
646+
'page' => ['type' => 'page', 'page' => ['abc']],
647+
]);
648+
649+
$expected = [
650+
'url' => ['type' => 'url', 'url' => 'http://example.com'],
651+
'asset' => ['type' => 'asset', 'asset' => ['main::img/coffee-mug.jpg'], 'container' => 'main'],
652+
'term' => ['type' => 'term', 'term' => ['tags::coffee'], 'taxonomy' => 'tags'],
653+
'page' => ['type' => 'entry', 'entry' => ['abc']],
654+
'blueprint' => 'speaker',
655+
];
656+
657+
$this->assertEquals($expected, $content);
658+
}
659+
632660
/** @test */
633661
public function it_can_migrate_custom_layout()
634662
{

tests/MigrateFieldsetTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,41 @@ public function it_migrates_date_field()
795795
$this->assertEquals($expected, $fieldset);
796796
}
797797

798+
799+
/** @test */
800+
public function it_migrates_link_it_field()
801+
{
802+
$fieldset = $this->migrateFieldset([
803+
'title' => 'Posts',
804+
'fields' => [
805+
'url' => ['type' => 'link_it'],
806+
'entries' => ['type' => 'link_it', 'collections' => ['blog', 'products']],
807+
],
808+
]);
809+
810+
$expected = [
811+
'title' => 'Posts',
812+
'fields' => [
813+
[
814+
'handle' => 'url',
815+
'field' => [
816+
'type' => 'link_it',
817+
'collections' => ['pages']
818+
],
819+
],
820+
[
821+
'handle' => 'entries',
822+
'field' => [
823+
'type' => 'link_it',
824+
'collections' => ['blog', 'products', 'pages']
825+
],
826+
],
827+
],
828+
];
829+
830+
$this->assertEquals($expected, $fieldset);
831+
}
832+
798833
/** @test */
799834
public function it_migrates_extention_validation()
800835
{

0 commit comments

Comments
 (0)