Skip to content

Group fieldtype support #76

Description

@carstenjaksch

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;
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions