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];
  }  
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, template_preprocess_node.patch, failed testing.

jherencia’s picture

Status: Needs work » Needs review
FileSize
712 bytes

Re-rolling.

catch’s picture

#2: template_preprocess_node.patch queued for re-testing.

mlncn’s picture

Status: Needs review » Reviewed & tested by the community

This cleaned up my log messages nicely ;-) And i was getting the error on a content type that did have fields.

tamsoftware’s picture

Thanks, worked great for me, applied on 7.0 official.

Neue Werte’s picture

Worked for me, Drupal 7.0

pillarsdotnet’s picture

+1

pillarsdotnet’s picture

Patch didn't apply. Re-rolled using "git format-patch"

pillarsdotnet’s picture

Issue tags: +Needs tests

Also needs tests to ensure this error does not reoccur. Unfortunately, learning Simpletest is still on my to-do list. Tagging.

pillarsdotnet’s picture

Hmm...
Trying to recreate the problem without writing a custom module:

  1. Created a content type "foo".
  2. Removed the body field.
  3. Turned off all the optional stuff I could find.
  4. Created a node of that type with title "bar" ($nid = 1058)
  5. Viewed the node
  6. Looked at error logs

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?

pillarsdotnet’s picture

Expanded patch context makes it clear why the change is necessary -- $variables['content'] is assumed to exist only six lines further down.

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs tests

The last submitted patch, template_preprocess_node-705892-11.patch, failed testing.

pillarsdotnet’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
bfroehle’s picture

Patch didn't apply. Re-rolled using "git format-patch"

Maybe you should check your version of patch...

$ curl http://drupal.org/files/issues/template_preprocess_node_2.patch | patch -p0
patching file modules/node/node.module
Hunk #1 succeeded at 1459 with fuzz 2 (offset 85 lines).

The patch in #2 is still RTBC in my book (as it was set to in #4).

pillarsdotnet’s picture

git clone git://drupalcode.org/project/drupal.git ; cd drupal; git checkout 7.x ; curl http://drupal.org/files/issues/template_preprocess_node_2.patch | git apply
remote: Counting objects: 118209, done.
remote: Compressing objects: 100% (27790/27790), done.
remote: Total 118209 (delta 87494), reused 114738 (delta 84049)
Receiving objects: 100% (118209/118209), 35.96 MiB | 241 KiB/s, done.
Resolving deltas: 100% (87494/87494), done.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   712  100   712    0     0    970      0 --:--:-- --:--:-- --:--:--  5836
error: node/node.module: No such file or directory

Trying again, using "git apply -p0" instead of "git apply"

curl http://drupal.org/files/issues/template_preprocess_node_2.patch | git apply -p0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   712  100   712    0     0   1093      0 --:--:-- --:--:-- --:--:--  6414
error: patch failed: modules/node/node.module:1374
error: modules/node/node.module: patch does not apply

Trying the patch in #11 instead:

curl http://drupal.org/files/issues/template_preprocess_node-705892-11.patch | git apply
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1053  100  1053    0     0   1099      0 --:--:-- --:--:-- --:--:--  9000
Maybe you should check your version of patch.

The patch in #11 was prepared according to the Git instructions linked from the project page. Maybe you should read them.

pillarsdotnet’s picture

Status: Needs review » Reviewed & tested by the community

Bikeshedding aside, I agree on the RTBC, but it still needs tests.

Is there any way to recreate this problem using simpletest?

Damien Tournoud’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs tests

It's a stupid notice, so I don't see the need for a test. That said, this should not have that many spaces:

   // Helpful $content variable for templates.
+  $variables['content']   = array();

Let's get a reroll, please.

bfroehle’s picture

Status: Needs work » Needs review
FileSize
871 bytes

Rerolled per Damien in #17. Patch credit goes to jherencia in the original post.

I agree that no test is required here.

The patch in #11 was prepared according to the Git instructions linked from the project page. Maybe you should read them.

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.

pillarsdotnet’s picture

Status: Needs review » Reviewed & tested by the community
fboulais’s picture

Version: 8.x-dev » 7.x-dev
Issue tags: -Needs backport to D7

I 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

catch’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7
jherencia’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

@catch it is a minor change that affects D7 and D8 the same way, so the patch is valid for both branches.

catch’s picture

Yep, 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).

jherencia’s picture

Ok, perfect.

willem77’s picture

G'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

dale386’s picture

bfroehle's patch fixes the issue in Drupal 7. Any chance that it'll be rolled into the next release?

pillarsdotnet’s picture

@dale386 -- yes, but not before next week. See http://drupal.org/node/1050616#comment-4471696

Alan D.’s picture

Just 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?

+  $variables += array('content' => array());
ELC’s picture

I'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

jherencia’s picture

Yep, it's a rare situation, but could happen. It is better that way.

Let's try to get this into next D7 release.

ELC’s picture

Ok 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.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 705892-template_preprocess_node_31.patch, failed testing.

ELC’s picture

Blah .. 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.

ELC’s picture

Status: Needs work » Needs review
FileSize
876 bytes

.. the exact same patch with status set to needs review. So frustrating. Sorry for the spam.

catch’s picture

Status: Needs review » Reviewed & tested by the community

This 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.

ELC’s picture

I 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?

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Oops. 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!

aspilicious’s picture

Issue tags: -Needs backport to D7

Removing tags (needs backport to D7 queue cleanup)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.