Field Name Collisions
Field names do not get namespaced by the group they reside within. I was wondering how other people handle this issue, without specifying 'name' => 'uniquename' for every collision.
I have multiple field groups residing on a single page, all with a text field named heading.
E.g.
use StoutLogic\AcfBuilder\FieldsBuilder;
$hero = new FieldsBuilder('hero');
$hero->addText('heading')
->setLocation('page', '==', 10)
->build();
$gallery = new FieldsBuilder('gallery');
$gallery->addText('heading')
->setLocation('page', '==', 10)
->build();
Both try to use the field name heading and collide, resulting in get_field('field_hero_heading') returning gallery's heading field value.
Is there anyway to get FieldsBuilder to namespace these for me? Or does anyone have an elegant way to handle this?
@clams4shoes Even when specifying the key in get_field you get the other field's value?
Specifying the field name like get_field('heading') will definitely yield a collision. I end up prefixing each name with a namespace, and I end up just setting a custom label so it displays as "Heading" to the user in the admin.
$hero = new FieldsBuilder('hero');
$hero->addText('hero_heading', ['label' => 'Heading'])
->setLocation('page', '==', 10)
->build();
@clams4shoes To give another idea, I use groups a ton to solve this problem.
->addTab('communities', ['placement' => 'left'])
->addGroup('community', ['label' => ''])
->addTab('archive')
->addGroup('archive', ['label' => ''])
->addImage('default_image', [
'label' => 'Default Community Image',
'preview_size' => 'thumbnail',
'library' => 'all'
])
->addButtonGroup('default_view', [
'choices' => [
'grid' => 'Grid',
'list' => 'List',
'map' => 'Map',
],
'default_value' => 'Grid'
])
->endGroup()
// endTab archive
->endGroup() //community
//endTab community
->addTab('developments', ['placement' => 'left'])
->addGroup('development', ['label' => ''])
->addTab('archive')
->addGroup('archive', ['label' => ''])
->addImage('default_image', [
'label' => 'Default Development Image',
'preview_size' => 'thumbnail',
'library' => 'all'
])
->addButtonGroup('default_view', [
'choices' => [
'list' => 'List',
'map' => 'Map',
],
'default_value' => 'List'
])
->endGroup()
// endTab archive
->endGroup() //development
//endTab development
I actually can't speak to how the native API looks because I've been using my own custom API since the beginning, but my resulting JSON from the above is:
{
"community": {
"archive": {
"default_image": "/wp-content/uploads/2019/05/mic-interview-blog-e1561571918123.jpg",
"default_view": "grid"
}
},
"development": {
"archive": {
"default_image": "/wp-content/uploads/2019/06/23-Partridge.jpg",
"default_view": "list"
}
}
}