HeaderImage block displays Unpublished nodes

Linxor - June 15, 2009 - 06:21
Project:Header image
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

I just noticed today that some of my unpublished header image nodes were being displayed in my headerimage block. For some odd reason, the image displays fine (i.e. no "access denied" message), however the image has a pink background behind it.

Considering that there isn't any other built-in method right now of temporarily hiding/disabling a headerimage node from the block, it would be great to exclude unpublished nodes from the image selection. This also seems like a very intuitive method for admins to remove nodes from the image selection -- though I could think of cases where a site admin wouldn't want to actually unpublish a node, but simply hide it from the headerimage selection, and so a better solution could be thought of in the future. But either way, the semantic definition of any "unpublished node" is that it should not be available for public viewing.

The simplest method to exclude unpublished nodes (without doing a bunch of modifications to the existing code) is to alter the image selection query.
Line 180 in the function headerimage_select_node():

Change this line:
$result = db_query("SELECT nid, conditions FROM {headerimage} WHERE block = %d ORDER BY weight, nid ASC", $block);

To this:
$result = db_query("SELECT nid, conditions FROM {headerimage} LEFT JOIN {node} USING(nid) WHERE block = %d AND status = 1 ORDER BY weight, nid ASC", $block);

--

Here's a workaround method until this fix gets committed:
Create a separate header image block called something like "Header Images Disabled" and move the unpublished nodes into that block.

#1

Bartezz - October 17, 2009 - 23:05

Hey Linxor,

I've had this problem with D5 as well (http://drupal.org/node/347397)
The module maintainer stated this issue was fixed in D6 but I guess it's not (http://drupal.org/node/347397#comment-1603310)

Your code isn't working as headerimage table doesn't even have a column status, so it can not check on that.

Your best of by leaving the query to this;

$result = db_query("SELECT nid, conditions FROM {headerimage} WHERE block = %d ORDER BY weight, nid ASC", $block);

And then inserting this code on line #182

if(!node_access("view", node_load($header_image->nid))) {
return;  
}

That way header_image doesn't only work properly to not show unpublised nodes but it also respects node_access settings set by any other module!

Cheers

#2

Linxor - November 3, 2009 - 23:17

My code should work fine. Have you even tested it? It's been working for me since June 2009.
The "status" column in my query comes from the {node} table (via the "LEFT JOIN" statement).

Your second point about adding the node_access() check is unrelated to the original issue. In the future, please post that as a separate issue.
However, it's a nice idea in the right direction... and I'd like to recommend an improvement to it. Instead of the return;, it would be better to use continue;. The purpose of the while {} loop on line 181 is to search through all header images returned in the query above and find the best matching header image to display. When you tell the loop to return;, it effectively stops the code from searching any subsequent items in the list.

 
 

Drupal is a registered trademark of Dries Buytaert.