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

Gutenberg Category and Tag Link blocks don't work on front end - invalid use of ID attribute #52

Closed
bobbingwide opened this issue Feb 26, 2021 · 10 comments
Labels
bug Something isn't working Gutenberg A bug / enhancement to Gutenberg

Comments

@bobbingwide
Copy link
Owner

https://blocks.wp.a2z/block/navigation-link-core-navigation-link/category-link-core-navigation-link/

Warning: Attempt to read property "post_status" on null in C:\apache\htdocs\wordpress\wp-content\plugins\gutenberg\build\block-library\blocks\navigation-link.php on line 110

Warning: Attempt to read property "post_status" on null in C:\apache\htdocs\wordpress\wp-content\plugins\gutenberg\build\block-library\blocks\navigation-link.php on line 110

Editing the post produced the same warnings, then on interacting with the Core Navigation block ( horizontal ) I got "This block has encountered an error and cannot be previewed". 12 Errors in the console log.

There were 5 empty social links in the menu. I removed these and the warnings still appeared. No menu items were visible.
Both of the links appeared to be valid.
image

Neither link had a Description, Link title or Link rel

The problem was that the $attributes['id'] values ( 469 for Block News and 39 for News ) don't refer to posts
These are the values of the term_id for the categories.

@bobbingwide
Copy link
Owner Author

My primary menu worked since I didn't use the id attribute at all.

@bobbingwide
Copy link
Owner Author

bobbingwide commented Feb 26, 2021

I added a Category link for FSE.

The generated HTML is:

<!-- wp:navigation {"orientation":"horizontal"} -->
<!-- wp:navigation-link {"label":"Block news","type":"category","description":"Block news - news about blocks","id":39,"url":"https://blocks.wp.a2z/category/block-news/"} /-->

<!-- wp:navigation-link {"label":"News","type":"category","description":"News in general - not necessarily about blocks.","id":469,"url":"https://blocks.wp.a2z/category/news/"} /-->

<!-- wp:navigation-link {"label":"FSE","type":"category","id":613,"url":"https://blocks.wp.a2z/category/fse/"} /-->
<!-- /wp:navigation -->

The Warning was only produced twice. I'd expected to see 3 messages.
The reason I didn't see the warning for the new link was that the post found for the id was an attachment.
For the other two 39 was a revision and 469 didn't exist.

@bobbingwide
Copy link
Owner Author

As a quick hack I changed the code to check the value of $attributes['type']

if ( isset( $attributes['id'] ) && is_numeric( $attributes['id'] ) && isset( $attributes['type'] ) ) {
		if ( 'post' === $attributes['type'] ) {
			$post = get_post( $attributes['id'] );
			if ( 'publish' !== $post->post_status ) {
				return '';
			}
		}
	}

@bobbingwide bobbingwide changed the title Warning from Gutenberg viewing Category Link Gutenberg Category Link doesn't work on front end - invalid use of ID attribute Feb 26, 2021
@bobbingwide
Copy link
Owner Author

Workaround

  • Use the Code editor to remove the id attributes.

@bobbingwide
Copy link
Owner Author

As a quick hack I changed the code to check the value of $attributes['type']

This hack's no good for the long term....
When the navigation link is a "Link" variation the type attribute is not set. So the above code would not check the post status.

@bobbingwide bobbingwide changed the title Gutenberg Category Link doesn't work on front end - invalid use of ID attribute Gutenberg Category and Tag Link blocks don't work on front end - invalid use of ID attribute Feb 26, 2021
@bobbingwide
Copy link
Owner Author

bobbingwide commented Feb 26, 2021

Variation type attribute id attribute is?
Link probably a post
Post Link post post ID
Page Link page post ID
Category Link category term ID
Tag link tag term ID

It might make more sense if the type for Tag Link matched the taxonomy name ( post_tag ) rather than the default for the permalink base ( tag ).

@bobbingwide
Copy link
Owner Author

A simpler workaround for the Fizzie theme, which already overrides the navigation link is to update fizzie_fiddle_nav_atts() to unset the id attribute before calling gutenberg_render_block_core_navigation_link().

@bobbingwide bobbingwide transferred this issue from bobbingwide/bobbingwide Feb 26, 2021
@bobbingwide bobbingwide added bug Something isn't working Gutenberg A bug / enhancement to Gutenberg labels Feb 26, 2021
@bobbingwide
Copy link
Owner Author

The original issue for this is WordPress/gutenberg#28712
24814 is a follow on.

@bobbingwide
Copy link
Owner Author

24814 is a follow on.

PR 28999 is another follow on that addresses the $attribute['type'] is not set for Navigation Link.

@bobbingwide
Copy link
Owner Author

Delivered in v0.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Gutenberg A bug / enhancement to Gutenberg
Projects
None yet
Development

No branches or pull requests

1 participant