12

How to restrict direct access to the nodes but just allow views to access the required field of the particular node type?

For example in Views Slideshow we are sliding the image field that belongs to particular node type. But we don't want search engines to access & visitors to access the nodes belong to that node type.

Any suggestions?

migrated from stackoverflow.com Sep 11 '11 at 5:11

This question came from our site for professional and enthusiast programmers.

12

A simple solution would be to not publish the nodes you don't want to be accessible. Views can still pull the data from those nodes.

  • 1
    Too simple? There is no such thing as a 'too simple solution'. Complexity isn't a goal in itself, is it? Perhaps Googletorp's answer doesn't solve your specific problem, but in that case you will have to be more elaborate and explain why not. – marcvangend Sep 11 '11 at 8:19
  • @marcvangend Though it's late but I have realize that is not the answer of my question. Hope some one can add a better answer – Fahad Dec 8 '12 at 22:31
  • @Fahad To get better answers, you need to ask better questions. Googletorp's answer looks like a good solution to me, and the upvotes seem to agree. If it doesn't solve your problem, go in and edit your question, adding all relevant details. – marcvangend Dec 9 '12 at 7:46
  • @marcvangend actually my issue was resolved and I accepted the answer earlier but in light of your comment. Googletorps has a very high reputation, does not get any difference if I accept or don't accept his answer. He provided an excellent alternate solution for which I have also up voted him. – Fahad Dec 9 '12 at 8:04
  • @Fahad By acception an answer you are saying: this answer solved my problem. It's a powerful statement and very useful for visitors to come, to find out if an answer is useful or not. If my answer didn't solve your problem, then you should edit your question and post some detail on why my solution doesn't work for you and like marcvangend said, add more detail. – googletorp Dec 9 '12 at 15:11
3

If we unpublish nodes added by user1 they wont be editable by any other user even if the user have the rights to edit the content of that content type.

Restrict Node Page View seems a better way to restrict the direct access to any nodes of specific content type.

Usage is also fairly simple you just need to enable this module and configure the permissions for the nodes which are directly accessible as normal.

http://drupal.org/project/restrict_node_page_view

From the project page:

Have you ever used a slideshow nodetype that you need to be published but don't want the node be accessible by itself using then node/XXX path? This module is for you!

With this module you can disable direct access to node pages (node/XXX) based on nodetypes and permissions.

Enable the module and don't forget to configure your permissions.

1

An other module who do the same thing but with more functionalities.

Rabbit Hole

Maintainers of this project ask to "Restrict Node Page View" Maintainer to merge projects in issues.

Advantages against "Restrict Node Page View" :

  • Same main functionality on node but extensible to users/files/taxonomy terms
  • Can be used in your module
  • Can be global to content type or on every content
  • See more on project page...

Disadvantage against "Restrict Node Page View" :

  • More code so possibly less effective (need tests, not proved)
1

you should use node_access(), there are certain options that you can easily implement using hook_node_access(). Implementing node_access() allows you to make a decision as to who can access a node and who can create, delete, edit, or update such a node. Views, however, can still pull data from these nodes whatsoever.

hook_node_access(), node_access()

0

Inspired by https://www.drupal.org/project/restrict_node_page_view you might write a small module yourself:

/**
 * Implements hook_node_access()
 */
function MYMODULE_node_access($node, $op, $account) {

  // Default checks
  if (!is_object($node)) {
    return NODE_ACCESS_IGNORE;
  }

  $type = is_string($node) ? $node : $node->type;

  if($op == 'view' && arg(0) == 'node' && arg(1) === $node->nid) {

    // Restrict access to full MYTYPE node for anon users
    if ($type == 'MYTYPE' && user_is_anonymous()) {
      return NODE_ACCESS_DENY;
    }
  }
  // Default
  return NODE_ACCESS_IGNORE;
}
  • This module was already mentioned couple of years ago. Could you explain what new your answer brings to the subject? – Mołot Aug 19 '15 at 9:18
  • @Mołot - ...to write a module yourself. Edited answer. – leymannx Aug 19 '15 at 9:19
0

Using the Rules module you can implement a rule that looks similar to this:

{ "rules_check_url" : {
    "LABEL" : "Disallow node/* access",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "site:current-page:url" ],
          "match" : "node\/\\d+$",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "Sorry, direct access to URLs like [site:current-page:url] is not allowed around here ...",
          "type" : "error"
        }
      },
      { "redirect" : { "url" : "no_access" } }
    ]
  }
}

The above rule does not (yet) take into account to only apply the "action" for selected roles only (eg so that admins can still use these paths to view a node). But for anybody a bit familiar with the Rules module, that is a straight forward "Condition" to add ...

To experiment with this rule in your own site, just copy the entire Rules code above, and paste it in a new Rule in your own site, created via the "Import" function. Then further edit/refine to make it fit in your own environment (e.g the "Sorry, ..." message to be shown).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.