18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Node Access<br />

Out-of-the-box, <strong>Drupal</strong> is a great system for creating and managing content. Users<br />

can log in and create content. Proper use of roles and permissions allows site editors<br />

to review some or all of a site's content. Site visitors can read published posts.<br />

But what happens if you want all site visitors to view some content, but only<br />

registered users to view a select list of restricted content? If, for example, your site<br />

requires paid registration to view in-depth articles about how to build <strong>Drupal</strong> web<br />

sites, the basic permissions provided by <strong>Drupal</strong> are not enough.<br />

There are cases where you need more advanced rules regarding which of the<br />

users (or groups of users) can create, view, edit, and delete content. To enable<br />

these rules, <strong>Drupal</strong> provides a Node Access system. Node Access provides an<br />

API for determining the grants, or permissions, that a user has for each node. By<br />

understanding how these grants work, a module developer can create and enforce<br />

complex access rules.<br />

In <strong>Drupal</strong> 7, any module may declare node access rules. This is a change from<br />

the earlier versions, and it provides some of the most powerful tools for <strong>Drupal</strong><br />

development.<br />

In this chapter, we will cover:<br />

• Node Access compared to user_access() and other permission checks<br />

• How <strong>Drupal</strong> grants node permissions<br />

• The node_access() function<br />

• hook_node_access() compared to {node_access}<br />

• Controlling permissions to create content<br />

• Using hook_node_access()<br />

• When to write a Node Access module

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!