pristas-peter / wp-graphql-gutenberg-acf

Expose acf blocks through graphql
MIT License
62 stars 29 forks source link

Register fields via PHP? #9

Open brandslikeus opened 4 years ago

brandslikeus commented 4 years ago

When registering a field via PHP the fields appear correctly in the schema under pages but not blocks...

blocksBy.AcfMyBlock.(acf fields MISSING) pages.nodes.PHPTestGroup.testfield pages.nodes.blocks.AcfMyBlock.(acf fields MISSING)

Creating the same testField via the ACF GUI works as expected with the testField available in both pages and blocks (as set in the location rules)

blocksBy.AcfMyBlock.acf.testfield pages.nodes.PHPTestGroup.testfield pages.nodes.blocks.AcfMyBlock.acf.testfield

Example php...

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_5df4cef437a15',
    'title' => 'PHP Test Group',
    'fields' => array(
        array(
            'key' => 'field_5dfbb904f92f1',
            'label' => 'TestField',
            'name' => 'testfield',
            'type' => 'text',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'show_in_graphql' => 1,
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'page',
            ),
        ),
        array(
            array(
                'param' => 'block',
                'operator' => '==',
                'value' => 'all',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'show_in_graphql' => 1,
    'graphql_field_name' => 'PHPTestGroup',
));

endif;
pristas-peter commented 4 years ago

I guess the problem is the block 'all' param. There should be a name with the corresponding block.

brandslikeus commented 4 years ago

Thanks for taking a look at this, I'm not sure if I'm missing something obvious but I've registered the demo block from the ACF instructions and then added the test field to the block.

As before the field does not appear in the schema under blocks but does appear correctly under pages. Adding the same field via the GUI appears in the schema under both pages and blocks as expected.


function register_acf_block_types() {

        // register a testimonial block.
        acf_register_block_type(array(
            'name'              => 'testimonial',
            'title'             => __('Testimonial'),
            'description'       => __('A custom testimonial block.'),
            'render_template'   => 'template-parts/blocks/testimonial/testimonial.php',
            'category'          => 'formatting',
            'icon'              => 'admin-comments',
            'keywords'          => array( 'testimonial', 'quote' ),
        ));
    }

    // Check if function exists and hook into setup.
    if( function_exists('acf_register_block_type') ) {
        add_action('acf/init', 'register_acf_block_types');
    }

    if( function_exists('acf_add_local_field_group') ):

        acf_add_local_field_group(array(
            'key' => 'group_5df4cef437a15',
            'title' => 'PHP Test Group',
            'fields' => array(
                array(
                    'key' => 'field_5dfbb904f92f1',
                    'label' => 'TestField',
                    'name' => 'testfield',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'show_in_graphql' => 1,
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'maxlength' => '',
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'page',
                    ),
                ),
                array(
                    array(
                        'param' => 'block',
                        'operator' => '==',
                        'value' => 'acf/testimonial',
                    ),
                ),
            ),
            'menu_order' => 0,
            'position' => 'normal',
            'style' => 'default',
            'label_placement' => 'top',
            'instruction_placement' => 'label',
            'hide_on_screen' => '',
            'active' => true,
            'description' => '',
            'show_in_graphql' => 1,
            'graphql_field_name' => 'PHPTestGroup',
        ));

    endif;
pristas-peter commented 4 years ago

Can you try to export your group as php code through ACF admin and paste it here?

pristas-peter commented 4 years ago

Also the field groups are quried like this:

acf_get_field_groups([
     'block' => $block_type['name']
]);

where block_type['name'] is the name of the block => 'acf/testimonial'

brandslikeus commented 4 years ago

Here's the export of the group as php code (same as above):

if( function_exists('acf_add_local_field_group') ):

        acf_add_local_field_group(array(
            'key' => 'group_5df4cef437a15',
            'title' => 'PHP Test Group',
            'fields' => array(
                array(
                    'key' => 'field_5dfbb904f92f1',
                    'label' => 'TestField',
                    'name' => 'testfield',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'show_in_graphql' => 1,
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'maxlength' => '',
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'page',
                    ),
                ),
                array(
                    array(
                        'param' => 'block',
                        'operator' => '==',
                        'value' => 'acf/testimonial',
                    ),
                ),
            ),
            'menu_order' => 0,
            'position' => 'normal',
            'style' => 'default',
            'label_placement' => 'top',
            'instruction_placement' => 'label',
            'hide_on_screen' => '',
            'active' => true,
            'description' => '',
            'show_in_graphql' => 1,
            'graphql_field_name' => 'PHPTestGroup',
        ));

    endif;

When created via the GUI the field appears in the schema as expected: GraphiQL-GUI

When created via the exported php code the fields are missing from the schema in the block location but appear as expected in the page location: GraphiQL-PHP

This is after updating the blocks content , saving the page and running WP GraphQL Gutenberg Admin.