Created
August 10, 2020 18:06
-
-
Save carolinan/fb173c890421c7130b0a885f87a751ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * External dependencies | |
| */ | |
| import { map, filter } from 'lodash'; | |
| import classnames from 'classnames'; | |
| /** | |
| * WordPress dependencies | |
| */ | |
| import { | |
| AlignmentToolbar, | |
| BlockControls, | |
| InspectorControls, | |
| } from '@wordpress/block-editor'; | |
| import { PanelBody, SelectControl } from '@wordpress/components'; | |
| import { useSelect } from '@wordpress/data'; | |
| import { __ } from '@wordpress/i18n'; | |
| function HierarchicalTaxonomyEdit( { context, attributes, setAttributes, } ) { | |
| const { postType, postId } = context; | |
| // Get all taxonomy data. | |
| const taxonomies = useSelect( ( select ) => | |
| select( 'core' ).getTaxonomies() | |
| ); | |
| // Filter the hierarchical taxonomies. | |
| const hierarchicalTaxonomies = filter( | |
| taxonomies, | |
| ( taxonomy ) => taxonomy.hierarchical | |
| ); | |
| //console.log( hierarchicalTaxonomies ); | |
| // Get values for the select list. | |
| const taxonomyOptions = map( hierarchicalTaxonomies, ( taxonomy ) => { | |
| return { | |
| value: taxonomy.slug, | |
| label: taxonomy.name, | |
| }; | |
| } ); | |
| //console.log( taxonomyOptions ); | |
| // Get taxonomy name | |
| const taxonomyName = map( hierarchicalTaxonomies, ( taxonomy ) => { | |
| return taxonomy.name.toLowerCase(); | |
| } ); | |
| //console.log(taxonomyName); | |
| const taxonomies2 = useSelect( | |
| ( select ) => | |
| select( 'core' ).getEditedEntityRecord( | |
| 'postType', | |
| postType, | |
| postId | |
| )?.taxonomyName | |
| ); | |
| console.log( taxonomies2 ); | |
| /* | |
| const taxonomyLinks = useSelect( | |
| ( select ) => { | |
| const { getEntityRecord } = select( 'core' ); | |
| let loaded = true; | |
| const links = taxonomies.map( ( categoryId ) => { | |
| const taxonomy = getEntityRecord( | |
| 'taxonomy', | |
| taxonomySlug, | |
| categoryId | |
| ); | |
| if ( ! taxonomy ) { | |
| return ( loaded = false ); | |
| } | |
| return ( | |
| <a key={categoryId} href={taxonomy.link}> | |
| {taxonomy.name} | |
| </a> | |
| ); | |
| }); | |
| return loaded && links; | |
| }, | |
| [taxonomies] | |
| ); | |
| */ | |
| const { align } = attributes; | |
| const blockClassNames = classnames( 'wp-block-hierarchical-taxonomy' ); | |
| return ( | |
| <> | |
| <InspectorControls> | |
| <PanelBody title={ __( 'Taxonomy Settings' ) }> | |
| <SelectControl | |
| label={ __( 'Taxonomy' ) } | |
| options={ taxonomyOptions } | |
| /> | |
| </PanelBody> | |
| </InspectorControls> | |
| <BlockControls> | |
| <AlignmentToolbar | |
| value={ align } | |
| onChange={ ( nextAlign ) => { | |
| setAttributes( { align: nextAlign } ); | |
| }} | |
| /> | |
| </BlockControls> | |
| <div | |
| className={ classnames( blockClassNames, { | |
| [ `has-text-align-${ align }` ]: align, | |
| } ) } | |
| >ghgjh | |
| </div> | |
| </> | |
| ); | |
| } | |
| export default ( HierarchicalTaxonomyEdit ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment