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;
}
}
}
}
}
?>










Code Update
I updated this code snippet this morning. I realized that it was not a good idea to simply remove the entire taxonomy array from the $form variable. 1) this would remove taxonomy if you re-saved the node; 2) If the taxonomy was required the form would never validate. Instead, I added code to set a default taxonomy termID and disable the form element.
Hey I'm trying out your
Hey I'm trying out your script and it seemed to work in the beginning, but then I noticed that it seems to strip taxonomy from an existing node. We're looking at what the issue is.
Our situation:
We have nodes with 2 taxonomy vocab's: language (English, French, single select) and product categories (multi select). The only role having taxonomy control being super admin. All other roles have your new permission set as unchecked. Logged in as a limited admin, went to edit an existing node and the taxonomy select elements are indeed greyed out. Everything good so far. Upon pressing submit, the first taxonomy seems to remain (the language), and the second taxonomy seems to get cleared! Not good.
Upon simply commenting out this line "$form['taxonomy'][$k]['#disabled'] = true;" the form function returns to native state, as suspected, and the taxonomy selections remain.
Does Drupal process a form differently if it see's a form element set to DISABLED ?