statamic / ideas

đŸ’¡Discussions on ideas and feature requests for Statamic
https://statamic.dev
30 stars 1 forks source link

Save fieldsets in local addon, not in overridden fieldset path #1055

Open jhhazelaar opened 9 months ago

jhhazelaar commented 9 months ago

We try to keep our fieldsets organised. For a pagebuilder(Bard field) with a lot of blocks and includes it can get messy fast.

The idea was to create a local addon to use as a container for the pagebuilder resources (fieldsets, views, etc).

In the development of the site we change the pagebuilder quite a lot and notice that it's saved in the overridden folder in de resources/fieldsets/vendor folder. We want is saved in the addon resources/fieldsets.

In the Statamic\Fields\FieldsetRepository::save function there is a if statement that checks if the fieldset isNamespaced. Then save puts the fieldset file in the /vendor/ folder in the resources/fieldsets. It would be great if the save method checks if the fieldset is in a local addon and saved it there.

The current save method is:

    public function save(Fieldset $fieldset)
    {
        $directory = $this->directory;

        if ($fieldset->isNamespaced()) {
            [$key, $handle] = explode('::', $fieldset->handle());
            $directory = $this->directory.'/vendor/'.$key;
        }

        $handle = Str::of($fieldset->handle())->after('::')->replace('.', '/');

        File::put(
            "{$directory}/{$handle}.yaml",
            YAML::dump($fieldset->contents())
        );
    }

My suggestion is to check if the fieldsets path starts with the base_path('addons') and if so then save it at the fieldset in the local addon resources/fieldsets path. Maybe the save function can look something like this:

    public function save(Fieldset $fieldset)
    {
        $directory = $this->directory;

        if ($fieldset->isNamespaced()) {
            [$key, $handle] = explode('::', $fieldset->handle());

            if(Str::startsWith($this->hints[$key], base_path('addons'))){
                $directory = $this->hints[$key];
            } else {
                $directory = $this->directory.'/vendor/'.$key;
            }
        }

        $handle = Str::of($fieldset->handle())->after('::')->replace('.', '/');

        File::put(
            "{$directory}/{$handle}.yaml",
            YAML::dump($fieldset->contents())
        );
    }

Love to hear your thoughts about this.