tac_lite_create.module in Taxonomy Access Control Lite 7
Same filename and directory in other branches
Use tac_lite to control which terms can be used when creating new content. Unlike tac_lite.module, which relies on Drupal's node_access features, this module uses hook_form_alter to change which terms are presented by the taxonomy module.
Original implementation by markDrupal http://drupal.org/user/93970.
File
tac_lite_create.moduleView source
<?php
/**
* @file
* Use tac_lite to control which terms can be used when creating new
* content. Unlike tac_lite.module, which relies on Drupal's node_access
* features, this module uses hook_form_alter to change which terms are
* presented by the taxonomy module.
*
* Original implementation by markDrupal http://drupal.org/user/93970.
*/
/**
* Implementation of hook_form_alter().
*/
function tac_lite_create_form_alter(&$form, $form_state, $form_id) {
if (strpos($form_id, 'node_form') !== FALSE) {
global $user;
if (user_access('administer tac_lite')) {
// no need to restrict based on update permission in this cases
return;
}
// the vocabularies containing protected info.
$vids = variable_get('tac_lite_categories', array(
0,
));
// the terms this user is allowed to edit
$tids = array();
for ($i = 1; $i <= variable_get('tac_lite_schemes', 1); $i++) {
$config = _tac_lite_config($i);
if (isset($config['tac_lite_create']) && $config['tac_lite_create'] || in_array('grant_update', $config['perms'])) {
$tids = array_merge($tids, _tac_lite_user_tids($user, $i));
}
}
// Go through all of the fields in the system to find the ones we want use
$term_fields = array();
$info = field_info_fields();
foreach ($info as $key => $value) {
// First check for taxonomy_term_reference fields.
if ($value['type'] == 'taxonomy_term_reference') {
// Then check to see if they are associated with this node type (entity bundle).
if (isset($value['bundles']['node'])) {
if (in_array($form['#bundle'], $value['bundles']['node'])) {
// Add an entry to our term_fields in the form of field name => vocabulary machine name
$term_fields[$key] = $value['settings']['allowed_values'][0]['vocabulary'];
}
}
}
}
// Now that we have the names of the fields, go through each one in the form.
foreach ($term_fields as $field_name => $vocab_name) {
// Get the language key so we can find the correct element in the field.
$field_language = $form[$field_name]['#language'];
// Avoid PHP errors
if (empty($form[$field_name]) || empty($form[$field_name][$field_language])) {
continue;
}
// Skip auto complete fields as they are not supported.
if ($form[$field_name][$field_language]['#type'] != 'select' && $form[$field_name][$field_language]['#type'] != 'radios') {
continue;
}
// Get the vocabulary info so we can get the vid.
$v = taxonomy_vocabulary_machine_name_load($vocab_name);
// We only want to act on this field if it is tied to a vocabulary we are set to control.
if (!in_array($v->vid, $vids)) {
continue;
}
// Go through each option for this field.
foreach ($form[$field_name][$field_language]['#options'] as $term_id => $term_name) {
// Skip the "<none>" option (or anything like it).
if (!is_numeric($term_id)) {
continue;
}
// Is this option not in the list of terms this user can create with?
if (!in_array($term_id, $tids)) {
// The term is not the default value, and user is not allowed to create with it.
// HELP: What if it IS the default value? Do we not unset it?
if ($term_id != $form[$field_name][$field_language]['#default_value']) {
// Unset the option to it doesn't appear.
// TODO: It would be a nice feature to have tids that are already selected.
// go into a value element so we could merge then when saving the node.
unset($form[$field_name][$field_language]['#options'][$term_id]);
}
}
}
// If there are no options left, but this field is required then throw a 404.
if (count($form[$field_name][$field_language]['#options']) == 0 && !empty($form[$field_name]['#required'])) {
drupal_set_message(t('You have no permissions to add content to the required %name vocabulary. Please contact the site administrator if you believe you should have permission to add content.', array(
'%name' => $form[$field_name]['#title'],
)));
drupal_access_denied();
exit;
}
// Don't show if the only option left is <none>.
if (empty($form[$field_name][$field_language]['#options']) || count($form[$field_name][$field_language]['#options']) == 1 && isset($form[$field_name][$field_language]['#options']['_none'])) {
$form[$field_name]['#access'] = FALSE;
}
// HELP: Is size deprecated?
/*
if (isset($form[$field_name]['#size']) && $form[$field_name]['#size'] > count($form['taxonomy'][$field_language]['#options'])) {
$form[$field_name]['#size'] = count($form[$field_name][$field_language]['#options']);
}
*/
}
}
elseif ($form_id == 'tac_lite_admin_scheme_form') {
$config = $form['#tac_lite_config'];
$scheme = $form_state['build_info']['args'][0];
if (!empty($form['tac_lite_config_scheme_' . $scheme])) {
$form['tac_lite_config_scheme_' . $scheme]['tac_lite_create'] = array(
'#type' => 'checkbox',
'#title' => 'Visibility on create and edit forms',
'#default_value' => isset($config['tac_lite_create']) ? $config['tac_lite_create'] : FALSE,
'#description' => t('Show terms when creating content. This <strong>does not</strong> control which users can create a given content type. This <strong>does</strong> control which terms appear on the node edit forms. <br/>Note that schemes granting <em>update</em> permission (above) imply <em>visibility on forms</em> as well.'),
);
}
}
}
Functions
Name | Description |
---|---|
tac_lite_create_form_alter | Implementation of hook_form_alter(). |