Skip to content

Commit

Permalink
LinkIt support (#119)
Browse files Browse the repository at this point in the history
* Use studly case

* wip

* bump

* tests
  • Loading branch information
jasonvarga authored Nov 22, 2024
1 parent 4d349b7 commit 86fe776
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/ContentMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 14 additions & 1 deletion src/FieldsetMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/ContentMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
35 changes: 35 additions & 0 deletions tests/MigrateFieldsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 86fe776

Please sign in to comment.