Skip to content

Commit

Permalink
Allow Page List to only show children
Browse files Browse the repository at this point in the history
- Add a childrenOnly attribute to Edit
- Update render to use attribute and only pull in children

Fixes #31063

?? This uses global $post which is probably not ideal
  • Loading branch information
mkaz committed May 4, 2021
1 parent 791c8fc commit 1fcbffc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
5 changes: 5 additions & 0 deletions packages/block-library/src/page-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"showSubmenuIcon",
"style"
],
"attributes": {
"childrenOnly": {
"type": "boolean"
}
},
"supports": {
"reusable": false,
"html": false
Expand Down
25 changes: 23 additions & 2 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
BlockControls,
useBlockProps,
store as blockEditorStore,
InspectorControls,
} from '@wordpress/block-editor';
import ServerSideRender from '@wordpress/server-side-render';
import { ToolbarButton } from '@wordpress/components';
import { PanelBody, ToggleControl, ToolbarButton } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEffect, useState } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
Expand All @@ -28,7 +29,12 @@ import ConvertToLinksModal from './convert-to-links-modal';
// Performance of Navigation Links is not good past this value.
const MAX_PAGE_COUNT = 100;

export default function PageListEdit( { context, clientId } ) {
export default function PageListEdit( {
attributes,
context,
clientId,
setAttributes,
} ) {
const { textColor, backgroundColor, showSubmenuIcon, style } =
context || {};

Expand Down Expand Up @@ -78,8 +84,23 @@ export default function PageListEdit( { context, clientId } ) {
const openModal = () => setOpen( true );
const closeModal = () => setOpen( false );

const { childrenOnly } = attributes;

return (
<>
<InspectorControls>
<PanelBody>
<ToggleControl
label={ __( 'Show only children' ) }
checked={ childrenOnly }
onChange={ () => {
setAttributes( {
childrenOnly: ! childrenOnly,
} );
} }
/>
</PanelBody>
</InspectorControls>
{ allowConvertToLinks && (
<BlockControls group="other">
<ToolbarButton title={ __( 'Edit' ) } onClick={ openModal }>
Expand Down
22 changes: 16 additions & 6 deletions packages/block-library/src/page-list/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,27 +146,37 @@ function nest_pages( $current_level, $children ) {
* @return string Returns the page list markup.
*/
function render_block_core_page_list( $attributes, $content, $block ) {
global $post; // ??
static $block_id = 0;
$block_id++;


$childrenOnly = ( isset( $attributes['childrenOnly'] ) && $attributes['childrenOnly'] );
$parent_id = ( $post->post_parent ) ? $post->post_parent : $post->ID;

// TODO: When https://core.trac.wordpress.org/ticket/39037 REST API support for multiple orderby values is resolved,
// update 'sort_column' to 'menu_order, post_title'. Sorting by both menu_order and post_title ensures a stable sort.
// Otherwise with pages that have the same menu_order value, we can see different ordering depending on how DB
// queries are constructed internally. For example we might see a different order when a limit is set to <499
// versus >= 500.
$all_pages = get_pages(
array(
'sort_column' => 'menu_order',
'order' => 'asc',
)
$query_args = array(
'sort_column' => 'menu_order',
'order' => 'asc',
);

if ( $childrenOnly ) {
$query_args['child_of'] = $parent_id;
}

$all_pages = get_pages( $query_args );

$top_level_pages = array();

$pages_with_children = array();

foreach ( (array) $all_pages as $page ) {
if ( $page->post_parent ) {
if ( ( $page->post_parent && !$childrenOnly ) ||
( $childrenOnly && $page->post_parent !== $parent_id ) ) {
$pages_with_children[ $page->post_parent ][ $page->ID ] = array(
'title' => $page->post_title,
'link' => get_permalink( $page->ID ),
Expand Down

0 comments on commit 1fcbffc

Please sign in to comment.