Drupal 7 – Disable the node view URL for some content types

Sometimes the content types we created are only used in Views. We don’t want to have the node view page for those content. The following solution would turn those unwanted node view pages to 404 not found.

Create a custom module and implement the hook_node_view() as follow.

function <module>_node_view($node, $view_mode, $langcode) {
  if ($node->type == '<content-type>' && $view_mode == 'full' && !user_access('edit any <content_type> content')) {
    drupal_not_found();
    drupal_exit();
  }
}

 

Replace the <content-type> with the content type machine name. Enable the module and clear the cache. You will find that those node view pages are not found but the edit tab is still valid if the logged in user has the proper permission. You can also restrict the access by a different permission.

Done =)

Reference: StackOveflow – Display a node in Views, but disable the node page

Advertisements

2 thoughts on “Drupal 7 – Disable the node view URL for some content types”

  1. Very good idea.

    I’m using to redirect on my custom view instead of using 404:
    drupal_goto(‘real url’);

    (you get an useless parenthesis 😉

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s