Problem/Motivation
When node and comment links are built the links are first passed through drupal_pre_render_links(), which messes with the render array in a way that is unexpected by the themer.
When you are adding links they have this form:
<?php
array(
'#theme' => 'links__node',
'#pre_render' => array(
'drupal_pre_render_links',
),
'#attributes' => array(
'class' => array(
'links',
'inline',
'post-links',
),
),
'node' => array(
'#theme' => 'links__node__node',
'#links' => array(),
'#attributes' => array(
'class' => array(
'links',
'inline',
),
),
),
'comment' => array(
'#theme' => 'links__node__comment',
'#links' => array(
'comment-add' => array(
'title' => 'Add new comment',
'attributes' => array(
'title' => 'Share your thoughts and opinions related to this posting.',
),
'href' => 'node/126',
'fragment' => 'comment-form',
),
),
'#attributes' => array(
'class' => array(
'links',
'inline',
),
),
),
)
?>
Now looking at that I would assume I could override the links function with THEMENAME_links__node__comment(), however much to my confusion it doesn't work.
After a bit of investigation I realise that drupal_pre_render_links() is moving the #links from the child links arrays into the parent one, which has the theme suggestion of links__node instead, but it doesn't actually render the child structures and marks them as #printed so that links__node__comments and other similar ones are never called.
This is unusual.
Proposed resolution
Possible solutions are:
- Change the way links are added (change the structure of the original links array), so that this moving links around is not necessary.
- Properly render the child render arrays so from the themer's point of view everything is working as expected.
- Change the child arrays' #theme value to be links__node, seeing as that is how they will actually be themed in the end. Although in this case there is nothing stopping modules from adding their own custom links with their own custom theme suggestion (like links__node__mymodule) and then wondering why they can't override that. (I don't think this really properly addresses the problem.)
Comments
Comment #1
rooby CreditAttribution: rooby commentedI noticed this in D7 but it seems to also be a D8 problem.
Comment #2
joelpittetMay want to close this but
drupal_pre_render_links()
doesn't exist in D8.Really don't follow the issue summary in this.
Comment #3
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedIt exists in Drupal 8 for me :)
https://api.drupal.org/api/drupal/core!includes!common.inc/function/drup...
I think what's described here is the documented, expected behavior of the function though. If you're rendering the whole list in one place, you need to get one list in the end, so multiple theme functions (each producing their own separate list) can't be called. They are only called if you choose to render a part of the array separately (i.e. to pull it out and render it as its own list).
Comment #6
joelpittetSorry must have had a filter on my search or something, thanks @catch
Comment #15
catchI think this is by design. A general modernisation of node links would be good, however there are other issues (somewhere) dealing with that.