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 · 3 comments
Open

Anchor tags are returning null for custom ACF blocks #268

brysonchiu opened this issue Sep 3, 2024 · 3 comments
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

@github-project-automation github-project-automation bot moved this to 🆕 Backlog in Headless OSS Sep 3, 2024
@josephfusco josephfusco moved this from 🆕 Backlog to ✅ Needs Reproduction in Headless OSS Sep 4, 2024
@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;
    }
]);

@martinmcdonnell martinmcdonnell moved this from ✅ Needs Reproduction to 🆕 Backlog in Headless OSS Dec 2, 2024
@martinmcdonnell martinmcdonnell moved this from 🆕 Backlog to ✅ Needs Reproduction in Headless OSS Jan 7, 2025
@colinmurphy
Copy link
Contributor

Just noting this might be resolved in the future as per discussion in the PR #328

@theodesp
Copy link
Member

@brysonchiu I think you need to add this to your block.json

supports: {
    anchor: true
}

Then you should be able to extract the "anchor" attribute from the Block.

Let us know if that worked.

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
Status: ✅ Needs Reproduction
Development

No branches or pull requests

5 participants