Skip to content

Taxonomy_select support for hierarchical Taxonomy #751

@bignall

Description

@bignall

Related to #640 but for taxonomy_select field type. I wanted a way to show the children of a parent taxonomy term in an element.

So, I modified CMB2_Type_Taxonomy_Select to accept a 'hierarchy' (boolean) option. When hierarchy is true it groups the terms by parent and produces options in optgroups, e.g.:

<optgroup label="Parent Term">
    <option value="{term_id}" {checked}>Term name</option>
    <option value="{term_id}" {checked}>Term name</option>
</optgroup>

Note that this only works for taxonomies with 2 levels. <optgroups> can't be nested so it's not clear how what you'd do it for deeper level taxonomies anyway. If you use it with taxonomies with more levels, you'll probably get optgroups without labels (I haven't tested it).

As part of the rendering, I added CMB2_Type_Multi_Base::select_optgroup which generates the html for an optgroup. So it would be a fairly simple matter to allow optgroups on other select fields as well.

I'll submit a pull request.

I'll be happy to add documentation, but I didn't see where to do it except in the Wiki and that doesn't seem to be in any branch so I didn't want to add it until the PR is accepted.

Also of note, there is an undocumented option for the taxonomy_select field: show_option_none. It appears to allow you to change the label for the None option.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions