bobbingwide / fizzie

Fizzie theme - a Full Site Editing theme using Gutenberg blocks
9 stars 1 forks source link

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

Closed bobbingwide closed 1 year ago

bobbingwide commented 3 years ago

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 commented 3 years ago

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

bobbingwide commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Workaround

bobbingwide commented 3 years ago

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 commented 3 years ago
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 commented 3 years ago

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 commented 3 years ago

The original issue for this is https://github.com/WordPress/gutenberg/issues/28712 24814 is a follow on.

bobbingwide commented 3 years ago

24814 is a follow on.

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

bobbingwide commented 1 year ago

Delivered in v0.4.0