Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group fieldtype support #76

Open
carstenjaksch opened this issue Jan 5, 2025 · 2 comments
Open

Group fieldtype support #76

carstenjaksch opened this issue Jan 5, 2025 · 2 comments

Comments

@carstenjaksch
Copy link
Contributor

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;
    }
@duncanmcclean duncanmcclean changed the title Proposal: GroupTransformer Group fieldtype support Jan 6, 2025
@duncanmcclean
Copy link
Member

Good idea!

The structure of $value would be: field:value|field2:value1,value2,value3|field3:value

I don't love this syntax though. It might get pretty messy if one of the values actually contains a pipe (or whatever the separator is).

Maybe we could add a config option for each of the fields inside the Group, with a dropdown like we have for the other fields. 🤔

We'd need to have a think about the UI though, since we'd be dealing with "nested fields" which we don't currently support.

@carstenjaksch
Copy link
Contributor Author

Yeah, nested fields with dropdowns for sub-fields would be awesome.

That example worked for my current use-case. Feel free to close the issue or leave it open as a reference for nested fields support.

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

No branches or pull requests

2 participants