Opened 15 years ago
Closed 11 years ago
#14041 closed enhancement (fixed)
Ensure a 'has_children' parameter is given to start_el
Reported by: | jaapjanfrans | Owned by: | wonderboymusic |
---|---|---|---|
Milestone: | 4.0 | Priority: | normal |
Severity: | minor | Version: | 3.0 |
Component: | General | Keywords: | has-patch needs-testing |
Focuses: | template | Cc: |
Description
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)
Change History (18)
#3
follow-up:
↓ 4
@
15 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', ...)
and
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
@
15 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] ); else $args[0]->has_children = ! empty( $children_elements[$element->$id_field] ); $cb_args = array_merge( array(&$output, $element, $depth), $args);
#5
follow-up:
↓ 6
@
15 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. } }
#11
@
11 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.
#13
@
11 years ago
14041.2.diff adds rudimentary UT.
#wchh14
Could you please point to the file and line number where has_children is set?