You are here

function permissions_by_term_form_alter in Permissions by Term 7

Same name and namespace in other branches
  1. 8.2 permissions_by_term.module \permissions_by_term_form_alter()
  2. 8 permissions_by_term.module \permissions_by_term_form_alter()

Implements hook_form_alter().

File

./permissions_by_term.module, line 171
Allows access to terms in a vocabulary to be limited by user or role.

Code

function permissions_by_term_form_alter(&$form, $form_state, $form_id) {

  // This is the node add / edit form. If a different selector is used from
  // another contributed module, we do nothing so as to not break the form.
  if (isset($form['type']) && isset($form['#node']) && !variable_get('taxonomy_override_selector', FALSE) && $form['type']['#value'] . '_node_form' == $form_id || isset($form['#entity_type']) && isset($form['#bundle'])) {

    // Field types we are looking for.
    $types = array(
      'taxonomy_term_reference',
    );
    foreach (element_children($form) as $field_name) {
      if (!($field_info = field_info_field($field_name))) {
        continue;
      }
      if (!in_array('#language', $form[$field_name])) {
        continue;
      }
      $options =& $form[$field_name][$form[$field_name]['#language']]['#options'];
      if (!in_array($field_info['type'], $types) || !isset($options)) {
        continue;
      }
      foreach ($options as $tid => $name) {
        if ($tid == "_none") {
          continue;
        }

        // Now we have the term ID, check to see if the current user has
        // access to the term.
        global $user;
        if (!permissions_by_term_allowed($tid, $user)) {
          unset($options[$tid]);
        }
      }
    }
  }
}