acf-builder icon indicating copy to clipboard operation
acf-builder copied to clipboard

addFields to multiple options pages

Open eriteric opened this issue 7 years ago • 4 comments

Is there a way to prefix the key of all fields in addFields?

For example: I have multiple options pages and I'd like to reuse the same fields on all of them. Options pages need to each have unique keys.

I tried setting the ‘key’ on the FieldsBuilder and addFields. But, the result is always that if I save a change to the fields in one options page, the same change is reflected on all the other options pages (the keys apparently are the same)

To draw a comparison, this would be like the built in function of the ACF clone field where it can prefix all the fields for you.

Something like:

->addFields($myfields)
            ->prefixKey('myoptionspage_');

eriteric avatar Dec 18 '18 01:12 eriteric

This is a good idea, and I need to investigate further. Thinking if it should just prefix the key or also the field name. In the clone field, I assume it prefixes the field name rather than the key (since the key is randomly generated in ACF UI)

stevep avatar Dec 20 '18 15:12 stevep

yes, i think you're right. As far as i understand, it prefixes the field and thus a new key is generated.

eriteric avatar Dec 22 '18 07:12 eriteric

Hi @stevep I'd also love to see an AddClone field type.

As for my example, I have the following

$builder->headline = new FieldsBuilder('headline');
$builder->headline
	->addFields($builder->title)
;

$builder->block_image = new FieldsBuilder('block_image');
$builder->block_image
	->addFields($builder->title)
	->addFields($builder->image)
	->addFields($builder->button)
;

I then attempt to compose these into a configuration for the pages.

$builder->home = new FieldsBuilder('home');
$builder->home
  ->addFields($builder->headline)
  ->addFields($builder->block_image)
  ->setLocation('post_type', '==', 'page')

I get duplicate keys on Title since those fields are composed into the home field group without unique prefixes.

I imagine adding something like ->addClone($builder->headline) could prefix similarly to how clone works in the UI.

Or, am I doing this wrong? I see ->addGroup(), should that be used for this purpose?

Happy to help contribute to any solution needed. Let me know how to help. Thanks

jeremymoore avatar Jan 03 '19 22:01 jeremymoore

Did you guys figure this out?

losderpos avatar Dec 19 '19 12:12 losderpos