Make WordPress Core

Opened 14 years ago

Closed 10 years ago

#14041 closed enhancement (fixed)

Ensure a 'has_children' parameter is given to start_el

Reported by: jaapjanfrans's profile jaapjanfrans Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.0 Priority: normal
Severity: minor Version: 3.0
Component: General Keywords: has-patch needs-testing
Focuses: template Cc:


In the current display_element function of the Walker class, a 'has_children' argument is only added if args[0] is an array.

I'm not quite sure why this is the case. Wouldn't it be easier if a has_children argument is always added and passed on on to the callback functions like start_el?

i've currently modified a custom walker to do it like this:

		//display this element
		if ( is_array( $args[0] ) )
			$args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
		$cb_args = array_merge( array(&$output, $element, $depth), $args);
		$cb_args['has_children'] = ! empty( $children_elements[$element->$id_field] );

But recon it can be done in a cleaner way.

Attachments (3)

has_children.diff (2.1 KB) - added by scribu 14 years ago.
Set flag on Walker object
14041.diff (2.5 KB) - added by obenland 10 years ago.
Refreshed patch
14041.2.diff (4.5 KB) - added by obenland 10 years ago.

Download all attachments as: .zip

Change History (18)

#1 @scribu
14 years ago

  • Keywords reporter-feedback added; has children walker has_children removed

Could you please point to the file and line number where has_children is set?

#2 @jaapjanfrans
14 years ago


file '/wp-includes/classes.php' on line number 854

#3 follow-up: @scribu
14 years ago

  • Keywords reporter-feedback removed
  • Milestone Unassigned deleted
  • Resolution set to invalid
  • Status changed from new to closed

First of all, call_user_func_array() only accepts a numeric array, so $cb_argshas_children? wouldn't work.

Secondly, it checks if $args[0] is an array so that start_el() can be both:

start_el($arg1 = 'foo', $arg2 = 'bar', ...)


start_el($args = array())

Anyway, it's just a convenience. You can do the check yourself, since you have access to $children_elements.

#4 in reply to: ↑ 3 @jaapjanfrans
14 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

I see your point, but it would still be nice if the start_el function could get this argument passed on by default, because now it only knows of this argument if the $args object passed to display_element is an array.. or am I missing something something there? I mean, $children_elements is not available to start_el in some way is it?

for now I resorted to adding the has_children argument to the $args object if it is an object (in display_element):

		//display this element
		if ( is_array( $args[0] ) )
			$args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
			$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
		$cb_args = array_merge( array(&$output, $element, $depth), $args);

14 years ago

Set flag on Walker object

#5 follow-up: @scribu
14 years ago

  • Component changed from General to Template
  • Milestone set to 3.1

You're right, start_el() doesn't have access to $children_elements.

Instead of altering $cb_args, a better solution would be to set a flag on the Walker object instead. See has_children.diff

You could then do:

function start_el() {
  if ( $this->has_children ) {
    // etc.

#6 in reply to: ↑ 5 @jaapjanfrans
14 years ago

Nice, thanks!

#7 @scribu
14 years ago

  • Keywords has-patch needs-testing added

#8 @scribu
14 years ago

Related: #14102

#9 @nacin
14 years ago

  • Milestone changed from Awaiting Triage to Future Release

#10 @nacin
10 years ago

  • Component changed from Template to General
  • Focuses template added

10 years ago

Refreshed patch

#11 @obenland
10 years ago

Updated patch and tested with threaded and paged comments. I switched the logic in the comment_class() argument.

What else would need testing? It doesn't look like any other walker outside of comments is currently relying on the has_children value.

#12 @SergeyBiryukov
10 years ago

  • Milestone changed from Future Release to 4.0

#13 @obenland
10 years ago

14041.2.diff adds rudimentary UT

Version 0, edited 10 years ago by obenland (next)

10 years ago

This ticket was mentioned in IRC in #wordpress-dev by obenland. View the logs.

10 years ago

#15 @wonderboymusic
10 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from reopened to closed

In 28824:

Ensure that a has_children parameter is given to Walker::start_el().

Adds unit tests.

Props scribu, obenland.
Fixes #14041.

Note: See TracTickets for help on using tickets.