StoutLogic / acf-builder

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

Duplicated fields values showing up in admin. #115

Closed waustin closed 4 years ago

waustin commented 4 years ago

I have 2 different field groups on a page, both have the field named title

protected function create_page_header() {
        $page_header = new FieldsBuilder('page_header', ['menu_order' => 0]);
        $page_header
            ->addText('title')->setInstructions('Overides the WP Page Title. If blank the page title is used.')
            ->addText('parent_title')->setInstructions('Override parent page title that displays above the title.')
            ->addImage('background_image')
            ->addColorPicker('header_color')->setInstructions('Background overlay and title line color')
            ->setLocation('post_type', '==', 'page');

        // Build it
        acf_add_local_field_group($page_header->build());

    }
protected function create_resource_page_template_fields() {
 $featured_resources
            ->addText('title')->setRequired()
            ->addRepeater('resources', ['layout' => 'block'])
                ->addText('resource_name')->setRequired()
                ->addImage('logo')->setRequired()
                ->addTextarea('description')
                ->addUrl('link')
            ->endRepeater()
            ->setLocation('post_type', '==', 'page')->and('page_template', '==', 'page-templates/resources.php');

            acf_add_local_field_group($featured_resources->build());
}

If I var_dump the block the field is being created with a unique keys

["key"]=> string(23) "field_page_header_title" 

However when I edit the page in the admin. WP / ACF is only using the "last" defined field on the page. It acts like the keys are not unique.

I am running a new WP / Bedrock install.

ACF Builder "1.9.0" ACF Pro: 5.8.11

stevep commented 4 years ago

@waustin I believe the issue is with ACF itself. When saving values to the post_meta table in the database, it uses the ACF field name as the meta_key, not the ACF field key.

I just tried this using the ACF UI to double check, two field groups with 1 field each named title and it only saves the last value.

waustin commented 4 years ago

@stevep I think you are correct.

Reading the StoutLogic docs on Generated Keys made me think it there would not be field collision. I've started wrapping fields in a Group Field and that seems to prevent ACF overwriting stuff.

Thank You for the response and the awesome library. It makes working with ACF some much nicer.

stevep commented 4 years ago

@waustin I've started to do the same thing in my projects in some cases

matteodriussi commented 1 year ago

i had the same issue. i was able to fix this by adding the key to the FieldsBuilder instantiation, like this:

$project_data = new FieldsBuilder('Project data', ['key' => 'project_data']);