pristas-peter / wp-graphql-gutenberg

Query gutenberg blocks with wp-graphql
https://wp-graphql-gutenberg.netlify.app
GNU General Public License v3.0
299 stars 61 forks source link

Add filter to modify the block attributes #132

Closed rodrigo-arias closed 2 years ago

rodrigo-arias commented 2 years ago

Hi @pristas-peter I'm adding this filter to be able to modify the block attributes.

I created an ACF Block that consists of a repeater of several fields including images. The problem I found is that the images returned the ID instead of the source URL. The same happens with the relationship fields.

Registering a custom GraphQL type and filtering it with graphql_gutenberg_block_attributes_fields was good enough for the attributes, but still the blocksJSON field returned the original format stored by ACF.

CleanShot 2021-12-30 at 15 20 01

This filter allows me to adjust the content that I finally get in blocksJSON to parse my data.

Let me know if this makes sense or is there a better way to do it. Thanks

dushakov92 commented 2 years ago

this is something i would like added as well.

seandavidrobbins commented 2 years ago

If at all possible while you are adding these gents, could you add me as well?

frawis commented 2 years ago

How can I use this filter. Do you have a example?

rodrigo-arias commented 2 years ago

@frawis below is an example of how I'm using it. Of course, it depends on your block configuration. In my case, I have a custom ACF block with a relationship field and I need more data about those related posts beyond the post ID, which is the only value that is included by default.

add_filter( 'graphql_gutenberg_block_attributes_fields', 'project_slug_graphql_block_attributes', 10, 2 );
/**
 * Filter the GraphQL block attributes.
 *
 * @param array $attributes Block attributes.
 * @param array $block_type Block type definition.
 *
 * @return array
 * @since 1.0.0
 */
function project_slug_graphql_block_attributes( array $attributes, array $block_type ) : array {
    if ( isset( $block_type['name'] ) && 'acf/quotes' !== $block_type['name'] ) {
        return $attributes;
    }

    if ( isset( $attributes ) && is_array( $attributes ) ) {
        $attributes = project_slug_transform_block_quotes_attributes( $attributes );
    }

    return $attributes;
}

/**
 * Transform the acf/quotes attributes.
 *
 * @param array $attributes Block attributes.
 *
 * @return array
 * @since 1.0.0
 */
function project_slug_transform_block_quotes_attributes( array $attributes ) : array {
    if ( empty( $attributes ) || ! isset( $attributes['data'] ) ) {
        return $attributes;
    }

    $data = $attributes['data'];

    $items = array();

    foreach ( $data['items'] as $key => $value ) {
        $post = get_post( $value );

        if ( ! empty( $post ) ) {
            $items[ $key ]['title'] = $post->post_title;
            $items[ $key ]['slug']  = $post->post_name;
            $items[ $key ]['date']  = $post->post_date;
        }
    }

    return array(
        'title' => $data['title'],
        'items' => $items,
    );
}