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

Transform page list to list #69116

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/block-library/src/page-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { pages } from '@wordpress/icons';
*/
import initBlock from '../utils/init-block';
import metadata from './block.json';
import transforms from './transforms.js';
import edit from './edit.js';
t-hamano marked this conversation as resolved.
Show resolved Hide resolved

const { name } = metadata;
Expand All @@ -18,6 +19,7 @@ export const settings = {
icon: pages,
example: {},
edit,
transforms,
};

export const init = () => initBlock( { name, metadata, settings } );
71 changes: 71 additions & 0 deletions packages/block-library/src/page-list/transforms.js
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;
Loading