Drupal 6: Restrict which roles can modify node taxonomy
I recently decided to use taxonomy to set the status of my nodes as they moved through phases of the application. A created a category called status and added the terms: incomplete, complete, and submitted. I needed the ability to restrict who can set the taxonomy, so I wrote this module which allows you to set which roles can modify taxonomy:
<?php
function tpr_perm() {
return array('modify node taxonomy');
}
function tpr_form_alter(&$form, $form_state, $form_id) {
// ensure this is a node form
if (substr($form_id,-10)!='_node_form') return;
// ensure taxonomy exists for this node type
if (!isset($form['taxonomy'])) return;
// if the user does not have permission to modify node taxonomy:
if (!user_access('modify node taxonomy')) {
// node already exists
if (isset($form['#node']->nid)) {
// loop through taxonomy form elements
foreach ($form['taxonomy'] as $k => $v) {
// set each form element to disabled
if (is_int($k) && is_array($v)) {
$form['taxonomy'][$k]['#disabled'] = true;
}
}
} else {
// new node, remove taxonomy from elements
// loop through taxonomy from elements
foreach ($form['taxonomy'] as $k => $v) {
if (is_int($k) && is_array($v)) {
// get first termID
$termID = array_shift(array_keys($v['#options'][0]->option));
if (is_int($termID)) {
// set default option
$form['taxonomy'][$k]['#default_value'] = $termID;
}
// set element as disabled
$form['taxonomy'][$k]['#disabled'] = true;
}
}
}
}
}
?>