Repository navigation
Closed
Labels
[Block] NavigationAffects the Navigation Block[Status] In ProgressTracking issues with work in progress[Type] BugAn existing feature does not function as intended
Description
Description
Stack trace:
PHP Fatal error: Uncaught TypeError:
iterator_to_array(): Argument #1 ($iterator) must be of type Traversable|array, string given in /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/navigation.php:376
Stack trace:
#0 /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/navigation.php(376): iterator_to_array('')
#1 /srv/htdocs/wp-content/plugins/gutenberg/lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php(273): gutenberg_block_core_navigation_get_post_ids('')
#2 /srv/htdocs/wp-content/plugins/gutenberg/lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php(628): WP_Navigation_Block_Renderer::get_inner_blocks(Array, Object(WP_Block))
#3 /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/navigation.php(413): WP_Navigation_Block_Renderer::render(Array, '', Object(WP_Block))
#4 /wordpress/core/6.4.1/wp-includes/class-wp-block.php(258): gutenberg_render_block_core_navigation(Array, '', Object(WP_Block))
#5 /wordpress/core/6.4.1/wp-includes/class-wp-block.php(244): WP_Block->render()
#6 /wordpress/core/6.4.1/wp-includes/blocks.php(1484): WP_Block->render()
#7 /wordpress/core/6.4.1/wp-includes/blocks.php(1522): render_block(Array)
#8 /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/template-part.php(144): do_blocks('<!-- wp:group {...')
#9 /srv/htdocs/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/BlockTemplatesController.php(171): gutenberg_render_block_core_template_part(Array)
#10 /wordpress/core/6.4.1/wp-includes/class-wp-block.php(258): Automattic\WooCommerce\Blocks\BlockTemplatesController->render_woocommerce_template_part(Array, '', Object(WP_Block))
#11 /wordpress/core/6.4.1/wp-includes/blocks.php(1484): WP_Block->render()
#12 /wordpress/core/6.4.1/wp-includes/blocks.php(1522): render_block(Array)
#13 /wordpress/core/6.4.1/wp-includes/block-template.php(263): do_blocks('<!-- wp:templat...')
#14 /wordpress/core/6.4.1/wp-includes/template-canvas.php(12): get_the_block_template_html()
#15 /wordpress/core/6.4.1/wp-includes/template-loader.php(106): include('/wordpress/core...')
#16 /wordpress/core/6.4.1/wp-blog-header.php(19): require_once('/wordpress/core...')
#17 /wordpress/core/6.4.1/index.php(17): require('/wordpress/core...')
#18 {main} thrown in /srv/htdocs/wp-content/plugins/gutenberg/build/block-library/blocks/navigation.php on line 376TL:DR We have a site in the latest Gutenberg release (17.1.3) that's throwing a fatal through the use of __unstableLocation in a navigation block in a template part.
Here's the block markup:
<!-- wp:navigation {"isResponsive":true,"__unstableLocation":"primary","style":{"typography":{"fontStyle":"normal","fontWeight":"500"}}} /-->
Investigation so far:
- The fatal appears when trying to get navigation post IDs as the
iterator_to_array()function is expecting a WP_Block_List but is given an empty string. - At the
block_core_navigation_render_inner_blocksfilter,$inner_blocksis just an empty string. - I believe the issue is caused here
where an empty menu returns an empty string, or perhaps it's returning\n\nas noted by this navigation function.
I appreciate this is quite a vague report that I cannot currently re-produce on a standard theme, any insight appreciated.
This PR seems related to a similar area, but not the root cause of this issue: #56459
Step-by-step reproduction instructions
I can't currently reproduce this issue.
Screenshots, screen recording, code snippet
Current theme patch that's resolving this issue:
/**
* Fixes a fatal error if inner_blocks is empty.
*
* This function addresses a potential issue where empty inner_blocks can cause a fatal error. It ensures that the inner_blocks parameter always returns a valid array.
*
* @param array $inner_blocks The array of inner blocks provided to the navigation block. These are the blocks nested within the navigation block.
*
* @return array Returns an empty array if $inner_blocks is empty, or the unmodified $inner_blocks array otherwise.
*/
function prefix_navigation_render_inner_blocks( $inner_blocks ) {
if ( empty( $inner_blocks ) ) {
return array();
}
return $inner_blocks;
}
add_filter( 'block_core_navigation_render_inner_blocks', 'prefix_navigation_render_inner_blocks', 10 );Environment info
- WordPress 6.4.1
- Gutenberg Plugin 17.1.3
- Custom FSE child theme of blockbase
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
Metadata
Metadata
Assignees
Labels
[Block] NavigationAffects the Navigation Block[Status] In ProgressTracking issues with work in progress[Type] BugAn existing feature does not function as intended
Activity
talldan commentedon Nov 29, 2023
That looks like it! I think I spotted a few other places with the same issue. It looks like the code was refactored recently, and the empty string previously meant the navigation block should show no content.
Since being refactored, this code should return something iterable, as the calling code expects a block list.
I've put together a bug fix in #56629. Now I just need to figure out some reproduction steps. I think it might happen if you have an empty menu or maybe a location with no menu assigned.
talldan commentedon Nov 29, 2023
@tommusrhodus From what I can tell it takes quite a set of circumstances for this error to trigger:
wp_navigationposts can be presentcore/page-listblock needs to be unregistered on the siteDoes that sound like it might be the situation on your site?
For me it has been quite hard to trigger as the fallback that adds the page list is quite effective, and that only fails to work when there's no page list block registered, but I guess you may have unregisted of that block so that you can use
__unstableLocation.getdave commentedon Nov 30, 2023
It should be possible to test Dan's PR at
http://gutenberg.run/56629
tommusrhodus commentedon Nov 30, 2023
@talldan Confirming we have no
wp_navigationposts, butcore/page-listis available.However, our first classic menu is titled
login/logoutand contains two items that I believe may be filtered out (need to review plugins on the site)Yes, we do have a location called from
__unstableLocationwith no menu assigned.Your PR changes what I suspect is the cause, still can't quite replicate how to get there on the site that originally triggered the fatal though.
Thank you!