StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
792 stars 61 forks source link

Feature Request - Update Default Values for Fields in Partials #178

Closed TimAtTeamlemke closed 1 year ago

TimAtTeamlemke commented 1 year ago

The feature I'd like to suggest is the ability to customize the default values of fields within a partial. While the current functionality enables us to include fields from a partial, there isn't a straightforward method to modify the default values of those fields directly within the context of the partial. This often requires manual adjustments to the partial's code whenever default values need to be changed, which can be cumbersome and less conducive to streamlined development.

Here's a common scenario I encounter: I frequently work with partials that encapsulate a set of fields, forming a cohesive component. However, each instance of this component might require slightly different default values for its fields. Having the flexibility to set and update these default values directly within the partial would significantly enhance code reusability and development efficiency.

Allow me to illustrate my suggestion with a concrete example from my use case:

Partial Definition 'sectionTitle' $partial ->addSelect('titleSize', ['label' => 'Title Size']) ->addChoice('h1', 'H1') ->addChoice('h2', 'H2') ->addChoice('h3', 'H3') ->addChoice('h4', 'H4') ->setDefaultValue('h2')

Option 1: Modifying Default Value in Component $services ->addFields(get_field_partial('components.partials.sectionTitle')) ->setFieldDefaultValue('titleSize', 'h4'); // Hypothetical method for modifying default value

Option 2: Directly Modifying Default Value in addFields $services ->addFields(get_field_partial('components.partials.sectionTitle'), [ 'titleSize' => [ 'default_value' => 'h4' // Direct modification of default value ] ])

Both of these options would enable developers to efficiently modify the default value of the "titleSize" field within components, without requiring adjustments to the original partial's definition.

TimAtTeamlemke commented 1 year ago

It turns out that the "modifyField" method is already present within the StoutLogic ACF Builder. This feature allows developers to make post-addition adjustments to the configuration settings of a field, including the modification of default values.

Here is a summary of the update:

Initial Feature Request: My original proposal aimed to introduce the ability to customize default values of fields within a partial, in order to enhance development flexibility.

Updated Proposal: Having discovered that the "modifyField" method already exists within the StoutLogic ACF Builder, I would like to highlight that my initial request is already fulfilled by this feature. Developers can adjust the default values of fields directly within component definitions by using the "modifyField" method.

Example: $services ->addFields(get_field_partial('components.partials.sectionTitle')) ->modifyField('titleSize', [ 'default_value' => 'h4' ])

I sincerely apologize for any confusion caused by my initial proposal. However, I am delighted to find that the "modifyField" function already provides a solution to my request.