-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62bf9c2
commit 7119606
Showing
3 changed files
with
75 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { select } from '@wordpress/data'; | ||
const transforms = { | ||
to: [ | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/list' ], | ||
transform: ( attributes ) => { | ||
const { style, fontFamily } = attributes; | ||
const pages = select( coreStore ).getEntityRecords( | ||
'postType', | ||
'page', | ||
{ | ||
per_page: 100, | ||
status: 'publish', | ||
orderBy: 'menu_order', | ||
order: 'asc', | ||
_fields: [ | ||
'id', | ||
'link', | ||
'parent', | ||
'title', | ||
'menu_order', | ||
], | ||
} | ||
); | ||
|
||
if ( ! pages || pages.length === 0 ) { | ||
return createBlock( 'core/list', {} ); | ||
} | ||
|
||
const createListItems = ( parentId = 0 ) => { | ||
return pages | ||
.filter( ( page ) => page.parent === parentId ) | ||
.map( ( page ) => { | ||
const childItems = createListItems( page.id ); | ||
const listItem = createBlock( 'core/list-item', { | ||
content: `<a href="${ page.link }">${ page.title.rendered }</a>`, | ||
} ); | ||
if ( childItems.length > 0 ) { | ||
const subList = createBlock( | ||
'core/list', | ||
{}, | ||
childItems | ||
); | ||
listItem.innerBlocks = [ subList ]; | ||
} | ||
return listItem; | ||
} ); | ||
}; | ||
|
||
const innerBlocks = createListItems(); | ||
|
||
return createBlock( | ||
'core/list', | ||
{ | ||
style, | ||
fontFamily, | ||
}, | ||
innerBlocks | ||
); | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
export default transforms; |