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.

Chapter 9<br />

Rebuilding the {node_access} table<br />

One of the trickier parts of the node access system is rebuilding the {node_access}<br />

table. When you first install a node access module, you will notice a warning at the<br />

top of the configuration page, prompting you to rebuild permissions.<br />

As a site administrator, you should always rebuild permissions when prompted<br />

to do so. As a module developer, you are responsible for ensuring that those<br />

permissions are rebuilt correctly.<br />

In our example module code, we avoided this issue by relying on data that is<br />

always present in the $node object, the user's identity, from which we can derive the<br />

user's roles. However, if your module relies on data not stored by <strong>Drupal</strong> core or<br />

contributed modules – both of which should be listed as dependencies[] in your<br />

module.info file – then it is your responsibility to store the data necessary to rebuild<br />

the {node_access} table properly.<br />

For example, let's look quickly at the Domain Access module. This module stores<br />

information about its grants in the {domain_access} table, which mirrors much<br />

of the data in {node_access}. The table schema is as follows:<br />

$schema['domain_access'] = array(<br />

'fields' => array(<br />

'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null'<br />

=> TRUE, 'default' => 0),<br />

'gid' => array('type' => 'int', 'unsigned' => TRUE, 'not null'<br />

=> TRUE, 'default' => 0),<br />

'realm' => array('type' => 'varchar', 'length' => '255', 'not<br />

null' => TRUE, 'default' => '')),<br />

'primary key' => array('nid', 'gid', 'realm'),<br />

'indexes' => array(<br />

'nid' => array('nid')),<br />

);<br />

[ 273 ]

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

Saved successfully!

Ooh no, something went wrong!