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

Anchor tags are returning null for custom ACF blocks #268

Open
brysonchiu opened this issue Sep 3, 2024 · 1 comment
Open

Anchor tags are returning null for custom ACF blocks #268

brysonchiu opened this issue Sep 3, 2024 · 1 comment
Labels
needs: reproduction This issue needs to be reproduced independently

Comments

@brysonchiu
Copy link

I origonally opened this ticket, wp-graphql/wpgraphql-acf#204, but Jason Bahl directed me to open one here instead.

Steps to reproduce

  1. Create a ACF block type and enable anchor attribute in the block type settings under the "Supports" tab.
  2. Add the block to a page and include an anchor to the block.
  3. Query:
query NewQuery {
  page(id: "/[PAGE-PATH]", idType: URI) {
    editorBlocks {
      ... on [BLOCK] {
        attributes {
          anchor
        }
      }
    }
  }
}

Anchors work for core blocks, but not for ACF blocks

@josephfusco josephfusco added the needs: reproduction This issue needs to be reproduced independently label Sep 4, 2024
@EthanCPP
Copy link

In includes/Field/BlockSupports/Anchor.php::register the field is resolved by retrieving the HTML for the block and returning the ID attribute set by Wordpress. This is returning null.

Within the $block array you can access ['attrs']['anchor'] which gives us the anchor tag without the need for decomposing the block. To test this I have replaced the resolve function with:

return $block['attrs']['anchor'];

This seems to work.

As I don't like to overwrite plugin code I have defined a new field, blockAnchor in my theme's functions.php - this can be accessed the same way the standard anchor field would be.

register_graphql_field('BlockWithSupportsAnchor', 'blockAnchor', [
    'type'        => 'string',
    'description' => __('The anchor field for the block.', 'wp-graphql-content-blocks'),
    'resolve'     => static function ($block) {
        return $block['attrs']['anchor'] ?? null;
    }
]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: reproduction This issue needs to be reproduced independently
Projects
None yet
Development

No branches or pull requests

3 participants