-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow importing publish states (& any other toggle fields) (#30)
* We don't need to lookup the field for something we already have. * Ensure a "published" field for entries The "Published" toggle on entries isn't a traditional field, it's special. So, if we want it to be importable, we need to ensure it here. * Use the `destinationBlueprint` method * Add transformer for Toggle fieldtype * We don't want to filter out `false` values, only null values. * Fix styling * Change how we ensure the "Published" field By ensuring it inside the `destinationBlueprint()` method, it would cause the "Published" field to be saved when the buttons/sets were added inside the Bard transformer. * Fix styling * tweak copy --------- Co-authored-by: duncanmcclean <[email protected]>
- Loading branch information
1 parent
87592ec
commit 12121c0
Showing
6 changed files
with
156 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Statamic\Importer\Transformers; | ||
|
||
class ToggleTransformer extends AbstractTransformer | ||
{ | ||
public function transform(string $value): bool | ||
{ | ||
if ($this->config('format') === 'boolean') { | ||
return match ($value) { | ||
'1', 'true' => true, | ||
'0', 'false' => false, | ||
}; | ||
} | ||
|
||
if ($this->config('format') === 'string') { | ||
return match (true) { | ||
in_array($value, explode('|', $this->config('values.true'))) => true, | ||
in_array($value, explode('|', $this->config('values.false'))) => false, | ||
}; | ||
} | ||
} | ||
|
||
public function fieldItems(): array | ||
{ | ||
return [ | ||
'format' => [ | ||
'type' => 'select', | ||
'display' => __('Format'), | ||
'instructions' => __('How is the value stored?'), | ||
'options' => [ | ||
'boolean' => __('Booleans'), | ||
'string' => __('Strings'), | ||
], | ||
'validate' => 'required', | ||
], | ||
'values' => [ | ||
'type' => 'array', | ||
'display' => __('Values'), | ||
'instructions' => __('Specify the values that represent true and false in your data. You may separate multiple values with a pipe (`|`).'), | ||
'mode' => 'keyed', | ||
'keys' => [ | ||
['key' => 'true', 'value' => __('True')], | ||
['key' => 'false', 'value' => __('False')], | ||
], | ||
'validate' => 'required', | ||
'if' => [ | ||
'format' => 'string', | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Statamic\Importer\Tests\Transformers; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use Statamic\Facades\Collection; | ||
use Statamic\Importer\Facades\Import; | ||
use Statamic\Importer\Tests\TestCase; | ||
use Statamic\Importer\Transformers\ToggleTransformer; | ||
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk; | ||
|
||
class ToggleTransformerTest extends TestCase | ||
{ | ||
use PreventsSavingStacheItemsToDisk; | ||
|
||
public $collection; | ||
public $blueprint; | ||
public $import; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->collection = tap(Collection::make('pages'))->save(); | ||
|
||
$this->blueprint = $this->collection->entryBlueprint(); | ||
$this->blueprint->ensureField('featured', ['type' => 'toggle']); | ||
|
||
$this->import = Import::make(); | ||
} | ||
|
||
#[Test] | ||
public function it_transforms_booleans() | ||
{ | ||
$transformer = new ToggleTransformer( | ||
import: $this->import, | ||
blueprint: $this->blueprint, | ||
field: $this->blueprint->field('featured'), | ||
config: ['format' => 'boolean'] | ||
); | ||
|
||
$this->assertTrue($transformer->transform('1')); | ||
$this->assertTrue($transformer->transform('true')); | ||
|
||
$this->assertFalse($transformer->transform('0')); | ||
$this->assertFalse($transformer->transform('false')); | ||
} | ||
|
||
#[Test] | ||
public function it_transforms_strings() | ||
{ | ||
$transformer = new ToggleTransformer( | ||
import: $this->import, | ||
blueprint: $this->blueprint, | ||
field: $this->blueprint->field('featured'), | ||
config: ['format' => 'string', 'values' => ['true' => 'yes|aye|yep', 'false' => 'no']] | ||
); | ||
|
||
$this->assertTrue($transformer->transform('yes')); | ||
$this->assertTrue($transformer->transform('aye')); | ||
$this->assertTrue($transformer->transform('yep')); | ||
|
||
$this->assertFalse($transformer->transform('no')); | ||
} | ||
} |