vinkla / extended-acf

Register advanced custom fields with object-oriented PHP
MIT License
455 stars 61 forks source link

Feature idea : set(key, value) on Field #44

Closed Grafikart closed 4 years ago

Grafikart commented 4 years ago

A generic option setter

Sometimes, especially with plugins, I need to set some specific key that aren't registered within ACF API. $config is protected and there is no way (or I missed something) to create an arbitrary configuration.

Before doing a PR I want to discuss the idea :

set() proposition

A set() method could serve as a bridge to add new config keys.

FlexibleContent::make(__('Composants', THEME_TEXTDOMAIN), 'flexibles')
                    ->layouts([
                        $this->block('banner-homepage')
                    ])
                   ->set('btn-icon-only', true)
                   ->set('collapse_all_flexible', true)
                   ->set('hide_collapse', true)

append() proposition

An append() method could be used to add multiple keys

FlexibleContent::make(__('Composants', THEME_TEXTDOMAIN), 'flexibles')
                    ->layouts([
                        $this->block('banner-homepage')
                    ])
                   ->append([
                      'btn-icon-only' => true,
                      'collapse_all_flexible' => true,
                      'hide_collapse' => true
                  ])
vinkla commented 4 years ago

Thanks for the suggestion!

I wonder, is it common to add custom configuration to core fields? What is the use case?

I guess we could add a section to the README on writing custom fields which extends the base Field class.

Grafikart commented 4 years ago

You are right extending Field to create a custom FlexibleContent (or any required specific configuration) is a better practice. This idea could solve some use case but could lead to a more bloated code.

vinkla commented 4 years ago

I've added a new issue to keep track of the new documentation section: #46

Grafikart commented 4 years ago

I reopen it since I have discovered new use case that are not in the WordPlate ACF API :

Use case

I use class to define fields and I need to retrieve the class name use for rendering

Layout::make($label, $name)
           ->set('controller_class', 'MyController')
            ->set('rendering_class', self::class);

I'll do a PR for this

vinkla commented 4 years ago

It could fill non existing API like setting menu_order.

Maybe we could add a menuOrder method. Are there any other non-existing settings hidden in the ACF interface but accessible through code?

It could also be used to pass arbitrary information with the fields

Will this data be saved to the database and is it accessible by the the_field function=

Grafikart commented 4 years ago

No, this information is persisted in the configuration that can be accessed using

acf_get_field($field)

It retrieves the config option defined with the field

vinkla commented 4 years ago

We've discussed this internally and decided to not add this feature for now. This can be solved by users of this package by extending the default fields. We'll document custom fields (#46) before releasing the next major version. We're very thankful for the pull request and might revisit this in the future if more people request this feature.

Again, thanks for all pull requests you send us Jonathan! Keep up the good work 🙌