Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Basically I want to be able to nest html element inside anchor, using render array.
Then, I could have this code inside hook_block_view.
$anchor = 'yahoo' => array(
'#type' => 'link',
'#href' => 'http://example.com/',
'#title' => array(
'#theme' => 'image',
'#path' => 'http://example.com/logo.png',
),
'#options' => array(
'attributes' => array(),
),
);
#title is populated with render array for img element.
I don't know if this could be implemented in some other way, but my current implementation is by applying a patch on drupal_pre_render_link.
Comment | File | Size | Author |
---|---|---|---|
#11 | argument_totheme_asrenderarray_1-rerolled.patch | 2.7 KB | musicnode |
#11 | interdiff-7-11.txt | 2.18 KB | musicnode |
#7 | argument_totheme_asrenderarray.patch | 2.69 KB | dozymoe |
#3 | argument_totheme_asrenderarray.patch | 2.24 KB | dozymoe |
anchor_has_children.patch | 781 bytes | dozymoe | |
Comments
Comment #0.0
dozymoe CreditAttribution: dozymoe commenteds/by of/,/
Comment #1
dozymoe CreditAttribution: dozymoe commentedOn second thought, it would be better if I can have,
rendered first then have it converted to,
somehow. :S
This is for #theme whose arguments expect simple text or predefined html markup.
Comment #2
dozymoe CreditAttribution: dozymoe commentedI came up with:
Basically it merges several children listed in $values part of #map_childs into a single key. Presumable, like in the sample below, points to #text, an argument that goes to theme_link.
Implemented here.
Doesn't work with #type such as '#type' => 'link' because the #pre_render clashed. I think. Dunno.
So it's fine if we just use '#theme' => 'link'.
PS: I see similar applications in drupal_pre_render_links.
Comment #3
dozymoe CreditAttribution: dozymoe commentedChanged title and marked as need review.
Also served as snippet page, if anybody is interested. :3
Comment #4
dozymoe CreditAttribution: dozymoe commentedFailed at theme_links() XD. Bummer.
Comment #5
dozymoe CreditAttribution: dozymoe commentedThe following code works, with a little exception.
The change is, for theme function that expects the argument to be an array, #map_childs' value has to be a string, not array. This tells drupal_pre_render_map_childs not to set the argument with results from drupal_render_children(), but link it by reference to $element[$value], thus result in an array that we wanted.
Example follows.
Notice that html value is '1' (a string). Either wise that element is passed to element_children() and produce warnings.
Warning: Invalid argument supplied for foreach() in element_children()
Ugly as hell, but in my use case, it works. 0.0b
PS: set html to value '1' or '0' works as if it is set with TRUE and FALSE. Me need to spent more time debugging. This could replace drupal_pre_render_links() with a bit more functionality.
Comment #6
dozymoe CreditAttribution: dozymoe commentedSimple fix, modified drupal_render() in includes/common.inc, before:
And after:
Maybe I should add
!count($elements)
in there. Hm.First attempt on OnionHead _failed_.
Comment #7
dozymoe CreditAttribution: dozymoe commentedIn case somebody is waiting for this to hit need review ~(‾-‾")~, forgotten to mark the patch listed, no changes made.
Reduce possible hick-up to one.
That is if the theme function requires 2 or more level-deep array-argument.
Might become a problem since drupal_render_children automatically adds #printed and #children element.
This two elements can be an unexpected baggage in the theme function.
Comment #8
dozymoe CreditAttribution: dozymoe commented#6: argument_totheme_asrenderarray.patch queued for re-testing.
Comment #9
xjmThanks for your work on this patch. I'm not sure whether we'd want to include this callback in core or not; however, here are some suggestions for cleaning up the patch a little.
Let's try to fit this on one line. Maybe:
Also, generally, let's change "childs" to "children" everywhere in the patch.
Finally, note that the Drupal 8.x patch will need to be rerolled, because the core directory structure for Drupal 8 has now changed. (For more information, see #22336: Move all core Drupal files under a /core folder to improve usability and upgrades). When the patch has been rerolled, please set the issue back to "Needs Review."
Tagging as novice for the task of rerolling the Drupal 8.x patch.
If you need help rerolling this patch, you can come to core office hours or ask in #drupal-gitsupport on IRC.
Comment #10
dozymoe CreditAttribution: dozymoe commentedA quick note:
This function uses iteration, can cause deep iteration if the render array is nested too deep.
PHP is not optimized to do iteration, I think.
Unless, drupal uses SPL iterators, then this might just work for the general cases.
I'll try to find the link where I've read that, in stackoverflow.com IIRC.
Comment #11
musicnode CreditAttribution: musicnode commentedRe-rolled per xjm's suggestions:
1) Changed comment to: Pre-render callback: renders specified children as the value of another child.
2) Changed change "childs" to "children" everywhere in the patch.
Comment #12
musicnode CreditAttribution: musicnode commentedUploaded the patch, so unassign.
Comment #13
dozymoe CreditAttribution: dozymoe commentedClosed as works as designed I guess, to be removed from the issue queue active priority.
Comment #13.0
dozymoe CreditAttribution: dozymoe commentedheh. s/of//