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've made a module which disables the rendering of node links and bodies and template_preprocess_node fails with this log:
Notice: Undefined index: content in template_preprocess_node() (line 1377 of /var/www/buyzentral/modules/node/node.module).
Here is the module file:
function mymodule_node_view($node, $view_mode) {
unset($node->content['links']);
unset($node->content['body']);
}
The reason is that template_preprocess_node assumes it has fields attached:
// Helpful $content variable for templates.
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
Comment | File | Size | Author |
---|---|---|---|
#34 | 705892-template_preprocess_node_34.patch | 876 bytes | ELC |
#33 | 705892-template_preprocess_node_33.patch | 876 bytes | ELC |
#31 | 705892-template_preprocess_node_31.patch | 881 bytes | ELC |
#29 | 705892-template_preprocess_node_29_d7.patch | 878 bytes | ELC |
#29 | 705892-template_preprocess_node_29b_d7.patch | 868 bytes | ELC |
Comments
Comment #2
jherencia CreditAttribution: jherencia commentedRe-rolling.
Comment #3
catch#2: template_preprocess_node.patch queued for re-testing.
Comment #4
mlncn CreditAttribution: mlncn commentedThis cleaned up my log messages nicely ;-) And i was getting the error on a content type that did have fields.
Comment #5
tamsoftware CreditAttribution: tamsoftware commentedThanks, worked great for me, applied on 7.0 official.
Comment #6
Neue Werte CreditAttribution: Neue Werte commentedWorked for me, Drupal 7.0
Comment #7
pillarsdotnet CreditAttribution: pillarsdotnet commented+1
Comment #8
pillarsdotnet CreditAttribution: pillarsdotnet commentedPatch didn't apply. Re-rolled using "git format-patch"
Comment #9
pillarsdotnet CreditAttribution: pillarsdotnet commentedAlso needs tests to ensure this error does not reoccur. Unfortunately, learning Simpletest is still on my to-do list. Tagging.
Comment #10
pillarsdotnet CreditAttribution: pillarsdotnet commentedHmm...
Trying to recreate the problem without writing a custom module:
Nothing.
Inserting a watchdog() into the template_preprocess_node() function reveals that there is still a 'links' element, even though its rendered version doesn't contain visible content.
Is there a way to duplicate this problem without writing a custom module?
Comment #11
pillarsdotnet CreditAttribution: pillarsdotnet commentedExpanded patch context makes it clear why the change is necessary -- $variables['content'] is assumed to exist only six lines further down.
Comment #13
pillarsdotnet CreditAttribution: pillarsdotnet commented#11: template_preprocess_node-705892-11.patch queued for re-testing.
Comment #14
bfroehle CreditAttribution: bfroehle commentedMaybe you should check your version of patch...
The patch in #2 is still RTBC in my book (as it was set to in #4).
Comment #15
pillarsdotnet CreditAttribution: pillarsdotnet commentedTrying again, using "git apply -p0" instead of "git apply"
Trying the patch in #11 instead:
The patch in #11 was prepared according to the Git instructions linked from the project page. Maybe you should read them.
Comment #16
pillarsdotnet CreditAttribution: pillarsdotnet commentedBikeshedding aside, I agree on the RTBC, but it still needs tests.
Is there any way to recreate this problem using simpletest?
Comment #17
Damien Tournoud CreditAttribution: Damien Tournoud commentedIt's a stupid notice, so I don't see the need for a test. That said, this should not have that many spaces:
Let's get a reroll, please.
Comment #18
bfroehle CreditAttribution: bfroehle commentedRerolled per Damien in #17. Patch credit goes to jherencia in the original post.
I agree that no test is required here.
Sorry to upset you. My apologies. I guess git is stricter than patch in it's context requirements—adding
-C1
to the git apply command successfully applies the patch.Comment #19
pillarsdotnet CreditAttribution: pillarsdotnet commentedComment #20
fboulais CreditAttribution: fboulais commentedI got the same problem. It works great for me too, applied on 7.0 official. Thanks !
Patch applied : 705892-Fixed-template_preprocess_node-assumes-.patch
Comment #21
catchComment #22
jherencia CreditAttribution: jherencia commented@catch it is a minor change that affects D7 and D8 the same way, so the patch is valid for both branches.
Comment #23
catchYep, I'm using the 'needs backport' tag to keep track of issues that need to go into D7 as well as D8 (as opposed to new Drupal 8 issues that will never be backported).
Comment #24
jherencia CreditAttribution: jherencia commentedOk, perfect.
Comment #25
willem77 CreditAttribution: willem77 commentedG'day guys,
I really appreciate the work that has gone into finding a solution for this and developing a patch. Thanks.
Now what do I do with the patch? I am not a coder and have only the barest knowledge of php. I am a website designer with a good working knowledge of html. I am not afraid of making changes if I know what to do.
Please pardon my ignorance, but I'd appreciate it if somebody could tell me which bits of the patch go where in what file. That would be really helpful.
I am running D7 with Drupal Commerce using mainly the latest dev versions of the modules.
Thanks
Willem
Comment #26
dale386 CreditAttribution: dale386 commentedbfroehle's patch fixes the issue in Drupal 7. Any chance that it'll be rolled into the next release?
Comment #27
pillarsdotnet CreditAttribution: pillarsdotnet commented@dale386 -- yes, but not before next week. See http://drupal.org/node/1050616#comment-4471696
Comment #28
Alan D. CreditAttribution: Alan D. commentedJust a slight observation, is it possible to set content before template_preprocess_node(). While this does not happen in the core Drupal code, if a modules weight is less than the node system weight, then this module could potentially add something to the content array, and this change will happily nuke that modules additions.
So maybe this is better?
Comment #29
ELC CreditAttribution: ELC commentedI'd have to concur with Alan D that the addition should be non-destructive. Someone might have reason to fill up the content array before template_preprocess_node gets to it, or their module just happens to get called first.
BUT, I don't actually know if that even partially probable or possible.
If it is possible though, then that fact should be taken into account. Or specifically documented that adding anything to the content key prior to X will mean it is deleted.
This change didn't manage to make it into Drupal 7.2 - is there any way to make sure that it does make it into the next version? I've attached a git formatted patch of both versions just for fun.
The += version
705892-template_preprocess_node_29_d7.patch
Overwrite version
705892-template_preprocess_node_29b_d7.patch
Comment #30
jherencia CreditAttribution: jherencia commentedYep, it's a rare situation, but could happen. It is better that way.
Let's try to get this into next D7 release.
Comment #31
ELC CreditAttribution: ELC commentedOk then, here's the D8 version with +=, credited to bfroehle (I just manually edited his patch to maintain the header)
The D7 and D8 patches are now identical except for offset. The 705892-template_preprocess_node_29_d7.patch from my post above is the += one.
Should I be setting this to needs review, or leaving it as reviewed? It is a single line after all, and should not cause issues to >PHP5.
edit: I just discovered that RTBC is not "Ready To Be Committed", but "Reviewed & Tested by the Community" so this should probably be set to "Needs Review". oops.
Comment #33
ELC CreditAttribution: ELC commentedBlah .. it's been committed to D8 already! Shouldn't the status have been changed when that happened? Or is the D7 work causing the status not be able to be changed?
Here's a new patch to change D8 so that it's non-destructive too.
Comment #34
ELC CreditAttribution: ELC commented.. the exact same patch with status set to needs review. So frustrating. Sorry for the spam.
Comment #35
catchThis was the commit - http://drupalcode.org/project/drupal.git/commitdiff/7d1738506cfa81eb3e70...
Was a duplicate issue with the same patch as earlier ones on this issue.
This new one looks fine, in fact it's possible the other patch would have broken performance_hacks, although I don't have time to check now, marking RTBC.
Comment #36
ELC CreditAttribution: ELC commentedI haven't followed it completely through, but it doesn't look like they tread on each other. Strangely, the $node->content gets built twice, in two different modes; one as a render array, the other as food for the node template file. (This is in both the normal node_view and the performance_hacks one). This patch is in the latter.
Doing normal things while rendering nodes would have been fine without this little change, but doing strange things like calling node_view with a specially crafted node->content would have been wiped out.
How do we get this into D7?
Comment #37
webchickOops. Mea culpa. :( I remember writing a reply to this issue, but Firefox must've crashed. That reply said basically that I'd considered the fact that something might be trying to set the content variable before it gets to this point but that by investigating the stacktrace I couldn't actually find such a point.
Nevertheless, this seems like a fine follow-up to be extra safe.
Committed to 8.x and 7.x. Thanks!
Comment #38
aspilicious CreditAttribution: aspilicious commentedRemoving tags (needs backport to D7 queue cleanup)