Open
Description
Description
I've tested around with a generic GroupTransformer.
The structure of $value
would be: field:value|field2:value1,value2,value3|field3:value
Would need some logic for special cases though, like the entries
fieldtype.
use Statamic\Importer\Transformers\AbstractTransformer;
use Statamic\Fields\Field;
use Statamic\Importer\Imports\Import;
use Statamic\Importer\Transformers\EntriesTransformer;
class GroupTransformer extends AbstractTransformer
{
public function transform(string $value)
{
$options = collect(explode("|", $value))->mapWithKeys(function ($item) {
list($key, $value) = explode(":", $item);
$fields = collect($this->field->get('fields'));
$field = $fields->where('handle', $key)->first();
if ($field['field']['type'] === 'entries') {
$newField = new Field($field['handle'], [ 'collections' => $field['field']['collections'] ]);
$transformer = new EntriesTransformer(
import: new Import(),
field: $newField,
config: [
'related_field' => 'slug',
'create_when_missing' => true
]
);
$value = $transformer->transform(str_replace(',', '|', $value));
}
return [$key => $value];
});
return $options->all();
}
}
Maybe add some options like delimiter for multiple values and sub fields. In this example, show some config options, if the sub field is of type entries
.
Could look like this – not tested and supports only one entries sub field, but should be possible with some enhancements:
public function fieldItems(): array
{
$fieldItems = [];
if ($this->entries) {
$collections = $this->entries->collections ?? Collection::all()->map->handle();
$fields = collect($collections)
->flatMap(fn (string $collection) => Collection::find($collection)->entryBlueprints())
->flatMap(fn ($blueprint) => $blueprint->fields()->all())
->unique(fn ($field) => $field->handle());
$fieldItems['related_field'] = [
'type' => 'select',
'display' => __('Related Field'),
'instructions' => __('importer::messages.entries_related_field_instructions'),
'default' => 'id',
'options' => $fields
->map(fn ($field) => ['key' => $field->handle(), 'value' => $field->display()])
->prepend(['key' => 'id', 'value' => __('ID')])
->values()
->all(),
'validate' => 'required',
];
$fieldItems['create_when_missing'] = [
'type' => 'toggle',
'display' => __('Create entry when missing?'),
'instructions' => __('importer::messages.entries_create_when_missing_instructions'),
'default' => false,
];
}
return $fieldItems;
}