Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blocksJSON and previewBlocksJSON missing ACF data #14

Open
gustavarrhenius opened this issue May 22, 2020 · 2 comments
Open

blocksJSON and previewBlocksJSON missing ACF data #14

gustavarrhenius opened this issue May 22, 2020 · 2 comments

Comments

@gustavarrhenius
Copy link

First of all, fantastic work on your plugins exposing Gutenberg information from Wordpress!

We feel like its easier to use the JSON fields exposed by blocks and preview to render our frontend to avoid long GraphQL queries and simplify the preview request , but we are missing the full ACF data in the response.

Do you have time to add the ACF data on the JSON fields aswell or guide me in the right direction to add that in the data layer.

@pristas-peter
Copy link
Owner

pristas-peter commented May 22, 2020

Thanks for the positive feedback.

Json will be getting some updates in the next version and will try to mirror the schema even more, I think the acf data will get there also.

Also I am working on being able to query all blocks at once as a flat structure with references to parent blocks and so (no more recursion problems), which should also help with your cause.

@gustavarrhenius
Copy link
Author

A great! I created a temporary solution to modify the output for the JSON data on request.

use WPGraphQLGutenberg\Blocks\PostMeta;
use WPGraphQLGutenberg\PostTypes\BlockEditorPreview;

/**
 * Add ACF Data to Graphql JSON
 */
add_filter('graphql_resolve_field', function($result, $source, $args, $context, $info, $type_name, $field_key, $field, $field_resolver)) {

    if (in_array($field_key, ['blocksJSON', 'previewBlocksJSON'])) {
        $root_id = $source->ID;
        $id = $field_key === 'previewBlocksJSON' ? BlockEditorPreview::get_preview_id($root_id, $root_id) : $root_id;
        $blocks = PostMeta::get_post( $id )['blocks'];
        if (!empty($blocks)) {
            foreach ($blocks as &$block) {
                if (substr($block['name'], 0, 3)  === 'acf') {
                    acf_setup_meta($block['attributes']['data'], $block['attributes']['id'], true);
                    $fields = get_fields();
                    $block['acf'] = $fields;
                }
            }
            $result = json_encode($blocks);
        }
    }
    return $result;

}, 100, 9);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants