Skip to content

Make fieldtype config extendable - #5077

Merged
jasonvarga merged 11 commits into
statamic:3.2from
aerni:feature/extend-fieldtype-config
Jan 27, 2022
Merged

Make fieldtype config extendable#5077
jasonvarga merged 11 commits into
statamic:3.2from
aerni:feature/extend-fieldtype-config

Conversation

@aerni

Copy link
Copy Markdown
Contributor

This PR lets you extend the default config fields of a fieldtype as requested in statamic/ideas#206. This can be useful for extra fields used by addons.

useStatamic\Fieldtypes\Text;
Text::extendConfigFields([
'group' => [
'display' => 'Group',
'instructions' => 'The name of the group by which to group this field by.',
'type' => 'text',
'width' => 50,
],
]);

One thing that might be handy is the ability to only add certain fields depending on context. For instance, a autocomplete config field would be useful for the Text fieldtype. But only in form blueprints.

@jsblair9

Copy link
Copy Markdown
Contributor

Would love this!

@jasonvarga

Copy link
Copy Markdown
Member

One thing that might be handy is the ability to only add certain fields depending on context.

I don't think you'd be able to that in this PR's current state.

@aerni

Copy link
Copy Markdown
ContributorAuthor

Yes, I figured that much. What do you think about the idea though?

I think the most obvious use case is regular blueprints vs. form blueprints. The field settings in regular blueprints affect the CP, while field settings in form blueprints affect the frontend. You might have field configuration options that don't make sense for both. Like input_type on the Text fieldtype makes perfect sense for the frontend but not the CP. Or clearable on the Select fieldtype makes perfect sense for the CP but not the frontend.

@jasonvarga

jasonvarga commented Jan 20, 2022

Copy link
Copy Markdown
Member

In that case I'd be more inclined to have something like 'except' => ['forms'] and only => ['entries'] and we'd filter appropriately in the core.

Can you give an example of your original idea? I don't really understand why a user would want to push additional config fields to a fieldtype they don't have control over.

@aerni

Copy link
Copy Markdown
ContributorAuthor

The idea is to make it possible for addon developers to add their own fields if needed. I've got two addons that would benefit from this right now: Factory and Livewire Forms.

Both addons hook into the config of blueprint fields. The Livewire Forms addon for instance does some stuff depending on a group key. Or the Factory addon does stuff depending on the factory key. It would be nice if the end user could edit these additional config items through the CP and not just in yaml.

@jasonvarga

Copy link
Copy Markdown
Member

Ok cool I gotcha. That's fine!

I'd say let's leave the "only render on form blueprint" thing to a separate feature.

But for your Factory/Livewire example, I think this works nicely.

@jasonvargajasonvarga left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling AnyFieldtype::extendConfigFields() will add those fields to all fieldtypes. It should only go into the one you intended.

Also, can we change it to extendConfigField() (singular) and pass in a single field that gets pushed onto an array. It's possible multiple developer would be using this and in its current state they'd just stomp over the first.

Finally, if possible please add a test for this into FieldtypeTest.php

Thanks!

@aerni

Copy link
Copy Markdown
ContributorAuthor

Good call! The fields are now only added to the intended fieldtype.

I settled on appendConfigFields instead of extendConfigFields as this makes it clear that the fields will be added after existing ones.

Every time you call appendConfigFields, it will merge it with existing extra fields. So multiple devs can add fields without overriding previously added ones.

There's also a new method appendConfigField if you wanted to simply add one field.

I also added tests as requested.

@aerni
aerni requested a review from jasonvargaJanuary 27, 2022 11:35
@jasonvarga

Copy link
Copy Markdown
Member

Thank you!

@jasonvarga
jasonvarga merged commit 7537a89 into statamic:3.2Jan 27, 2022
@ryanmitchell

Copy link
Copy Markdown
Contributor

@aerni do you think you could do a PR on the docs to explain this a bit?

jasonvarga added a commit to statamic/docs that referenced this pull request Jan 31, 2022
@jasonvarga

Copy link
Copy Markdown
Member

superstar1205 added a commit to superstar1205/lc that referenced this pull request Aug 21, 2022
@aerni
aerni deleted the feature/extend-fieldtype-config branch December 8, 2023 20:11
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@aerni@jsblair9@jasonvarga@ryanmitchell