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

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

Closed
2 tasks done
yellowmastodon opened this issue Sep 27, 2024 · 1 comment · Fixed by #66006
Closed
2 tasks done
Assignees
Labels
[Package] Editor /packages/editor [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@yellowmastodon
Copy link

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.

  • Yes

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

  • Yes
@yellowmastodon yellowmastodon added the [Type] Bug An existing feature does not function as intended label Sep 27, 2024
@Mamaduka Mamaduka added Needs Testing Needs further testing to be confirmed. [Package] Editor /packages/editor labels Sep 27, 2024
@torounit
Copy link
Member

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,
        )
    );
 });

@torounit torounit self-assigned this Oct 10, 2024
@torounit torounit removed the Needs Testing Needs further testing to be confirmed. label Oct 10, 2024
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Editor /packages/editor [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants