0

I have a vocabulary (Foods) with these terms.

  • Meet
  • Fruits

I want every user with the Gardner role to be able to edit the description of Fruits only. How can I do that?

This is just an example, on my site I have several terms, and I need specific users roles to edit specific term descriptions.

https://www.drupal.org/project/private_taxonomy does what I need, but I already have the taxonomy terms and I don't know how to change the ownership.

1 Answer 1

2

The access callback for a term edit page is taxonomy_term_edit_access($term). You need to replace it with a custom one in order to allow gardeners to access the page for fruit terms.

/**
 * Implements hook_menu_alter().
 */
function MODULE_menu_alter(&$items) {
  $items['taxonomy/term/%taxonomy_term/edit']['access callback']
    = 'MODULE_term_edit_access';
}

/**
 * Access callback for taxonomy edit term page.
 */
public function MODULE_term_edit_access($term) {
  if (MODULE_term_is_fruit($term) && user_access('edit fruits terms')) {
    return TRUE;
  }
  return taxonomy_term_edit_access($term);
}

/**
 * Returns whether or not a term is a fruit.
 */
function MODULE_term_is_fruit($term) {
  // ... implement fruit detection here ...
  foreach (taxonomy_get_parents_all($term->tid) as $parent) {
    if ($parent->name == 'Fruit') {
      return TRUE;
    }
  }
  return FALSE.
}

/**
 * Implement hook_permissions().
 */
function MODULE_permissions() {
  $permissions['edit fruits terms'] = array(
      'title' => t('Edit fruits terms'),
      'description' => t('Allows the user to edit Fruits terms.'),
  );
}
1
  • 1
    Unless you have tight control over the enabled modules, you can't be sure the original access callback (before MODULE_menu_alter) is taxonomy_term_edit_access. A more robust solution would be to start with to $items['taxonomy/term/%taxonomy_term/edit']['access arguments'][] = $items['taxonomy/term/%taxonomy_term/edit']['access callback'] to keep a reference to the original callback. Then, at the end of MODULE_term_edit_access($term) you can do something like $args = func_get_args(); return call_user_func(array_pop($args), $args); to invoke it. Commented Sep 16, 2016 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.