In Drupal, how to set node-[content type].tpl.php to display 404 Error?
In Drupal 6, I would like to have certain Content Types display Error 404, when accessed. I don't want them indexed by sea开发者_如何转开发rch engines or being accessible to users. They are used to store data, such as photos or other attachments.
I've tried setting node-[content type].tpl.php to
<?php
return drupal_not_found();
but it duplicates the entire 404 page within a page.
After you call drupal_not_found() call exit(), otherwise Drupal will just continue processing the page elements.
You can use e.g. the content access module to restrict access on a per content-type basis. This will return a permission denied error instead of a 404.
If you want to code a lighter version yourself you'll have to write a module that extends the Drupal permissions system, the theming layer is the wrong place for that. I think node_access would be the right hook for that.
Putting it in the theme layer also prevents any admin (that uses this theme) from viewing the content.
I set the path-alias for these content types to be
no-view/[nid]
and then use
function MYMODULE_init ()
{
$path = drupal_get_path_alias(request_uri());
if (strpos($path, "no-view/") !== false) {
drupal_not_found();
exit;
}
}
This way, you intercept things earlier in the process. You can also avoid having lots of node templates that all do the same thing.
As for the reason for making certain content types unavailable, there are several legitimate reasons for doing so. One is that it is often a better option to store complex data on a node w/ a custom content type rather than a CCK field in a node, and share this with other nodes. You may never want this data node to be viewed on its own. Another is using nodes to display groups of things in a view on a page, but that don't make sense to be viewed on their own.
精彩评论