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.
I was looking at how hook_link is implemented and found something in function node_link() that, at a first sight, looks a bit strange. I'm wondering if this is something left to fix when obj2array/array2obj were removed.
This is node_link() in node.module:
function node_link($type, $node = 0, $main = 0) {
$links = array();
if ($type == 'node') {
if (array_key_exists('links', $node)) {
$links = $node->links;
}
if ($main == 1 && $node->teaser && $node->readmore) {
$links[] = l(t('read more'), "node/$node->nid", array('title' => t('Read the rest of this posting.'), 'class' => 'read-more'));
}
}
return $links;
}
1) I believe the use of array_key_exists() is wrong here. $node is an object.
2) Is $links = $node->links;
really needed?
Comment | File | Size | Author |
---|---|---|---|
#5 | node.node_link.patch.txt | 664 bytes | Robrecht Jacques |
#4 | node.module.node_link.patch | 459 bytes | markus_petrux |
Comments
Comment #1
scroogie CreditAttribution: scroogie commentedOn http://php.net/array_key_exists its explicitly stated that array_key_exists() works on objects, too. $links = $node->links is because you have to return the value, otherwise you would have to introduce another if statement at the return clause, which is possibly slower than having a temporary variable.
Comment #2
markus_petrux CreditAttribution: markus_petrux commentedOops. I missed that comment, thanks
hmm... $links is initialized at the top of node_link(). It does not need to be initialized again. Anything returned by hook_link is appended (or array_merged) to $node->links.
This is node_view():
This is module_invoke_all():
Other hook_link implementation I've seen just do:
I don't get why node_link is different. Sorry, maybe it is in front of me, but I can't see it. I wouldn't have post about it, but it seems to me that even if the expression where array_key_exists() is used in node_link is TRUE (see below), $node->links is still empty. Hence, this code is not needed. Well, that's what it seems to me.
Is it possible that $node->links contains something else before that point? Maybe filled at node load time by another module? If so, if that is correct, then it looks like other hook_link implementations should also behave as node_link does?
Comment #3
scroogie CreditAttribution: scroogie commentedI just looked at the code from the syntactical point of view at first. Now I think your second point is right, this seems to be redundant.
Comment #4
markus_petrux CreditAttribution: markus_petrux commentedThanks for checking :-)
Here's a patch that removes that piece of code from node_link().
Comment #5
Robrecht Jacques CreditAttribution: Robrecht Jacques commentedIt is indeed unclear why that piece of code is needed. In
node_view()
$node->links
is set by calling allhook_link()
, including thenode_link()
, and then array_merged. So this is basically a no-op.Rerolled patch.
Ready to commit.
Comment #6
killes@www.drop.org CreditAttribution: killes@www.drop.org commentedcommitte Marcus' patch to 4.7
Comment #7
drummCommitted to HEAD.
Comment #8
(not verified) CreditAttribution: commented