WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.46k stars 4.18k forks source link

hierarchical term selector for flat taxonomies not working properly - problem with utils/terms.js #65704

Open yellowmastodon opened 3 weeks ago

yellowmastodon commented 3 weeks ago

Description

If following the example in gutenberg/packages/editor/src/components/post-taxonomies/README.md to use the hierarchical term selector with a non-hierarchical taxonomy, non-hierarchical taxonomy does not render terms at all.

Problem lies in gutenberg/packages/editor/src/utils/terms.js. termsByParent is somehow not an array, but an object with one 'undefined' key.

So the return statement: return fillWithChildren( termsByParent[ '0' ] || [] ); in function buildTermsTree() always returns empty array.

If the return statement is replaced with: return fillWithChildren( Object.values(termsByParent)[0] || [] ); the terms function properly again.

It would be also nice if HierarchicalTermsSelector automatically removed rendering of dropdown for parent term if using non-hierarchical taxonomy. Will post a comment to the issue, if I am able to propose a solution.

Step-by-step reproduction instructions

  1. add code to theme as per instructions in gutenberg/packages/editor/src/components/post-taxonomies/README.md
  2. open any post in gutenberg editor
  3. observe that taxonomies with flat terms do not render any terms

Screenshots, screen recording, code snippet

No response

Environment info

Wordpress version: 6.6.2 Theme: custom theme in development with no other changes to the gutenberg editor so far

Please confirm that you have searched existing issues in the repo.

Please confirm that you have tested with all plugins deactivated except Gutenberg.

torounit commented 1 week ago

I was able to reproduce the problem with the following code:

src/index.js

import { PostTaxonomiesHierarchicalTermSelector } from '@wordpress/editor';
import { addFilter } from '@wordpress/hooks';

function customizeTrackSelector( OriginalComponent ) {
    return function ( props ) {
        if ( props.slug === 'track' ) {
            return <PostTaxonomiesHierarchicalTermSelector { ...props } />;
        } else {
            return <OriginalComponent { ...props } />;
        }
    };
}

addFilter(
    'editor.PostTaxonomyType',
    'my-plugin/set-hierarchical-term-selector',
    customizeTrackSelector
);

<?php
/**
 * Plugin Name: Issue 65704
 */

 add_action( 'admin_enqueue_scripts', function() {
    $asset_file = include plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
    wp_enqueue_script(
        'issue-65704',
        plugins_url( 'build/index.js', __FILE__ ),
        $asset_file['dependencies'],
        $asset_file['version']
    );
 });

 add_action( 'init', function () {
    register_taxonomy(
        'track',
        'post',
        array(
            'public' => true,
            'label' => 'Track',
            'hierarchical' => false,
            'show_in_rest' => true,
        )
    );
 });