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 Sep 22, 2021
1 parent f12b08f commit c3076b2
Show file tree
Hide file tree
Showing 3 changed files with 42 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 @@ -56,6 +56,11 @@
"style",
"openSubmenusOnClick"
],
"attributes": {
"childrenOnly": {
"type": "boolean"
}
},
"supports": {
"reusable": false,
"html": false
Expand Down
21 changes: 19 additions & 2 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
useBlockProps,
store as blockEditorStore,
getColorClassName,
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 Down Expand Up @@ -69,7 +70,8 @@ export default function PageListEdit( {
context.customOverlayBackgroundColor,
] );

const { textColor, backgroundColor, style } = context || {};
const { textColor, backgroundColor, showSubmenuIcon, style } =
context || {};

const [ allowConvertToLinks, setAllowConvertToLinks ] = useState( false );

Expand Down Expand Up @@ -135,8 +137,23 @@ export default function PageListEdit( {
showSubmenuIcon: !! context.showSubmenuIcon,
};

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
24 changes: 18 additions & 6 deletions packages/block-library/src/page-list/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,35 +235,47 @@ function block_core_page_list_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();

$active_page_ancestor_ids = array();

foreach ( (array) $all_pages as $page ) {

$is_active = ! empty( $page->ID ) && ( get_the_ID() === $page->ID );

if ( $is_active ) {
$active_page_ancestor_ids = get_post_ancestors( $page->ID );
}

if ( $page->post_parent ) {
if ( ( $page->post_parent && !$childrenOnly ) ||
( $childrenOnly && $page->post_parent !== $parent_id ) ) {

$pages_with_children[ $page->post_parent ][ $page->ID ] = array(
'page_id' => $page->ID,
'title' => $page->post_title,
Expand Down

0 comments on commit c3076b2

Please sign in to comment.