0

I'm really in a bind. I have a Vocabulary called Foods.

and I have these terms.

Meet. Fruits.

Now I want the user role "Gardner" to be able to edit the description of only the term "Fruits". 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 terms description.

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 from admin to whatever user I need to be the owner.

migrated from stackoverflow.com Sep 7 '16 at 21:08

This question came from our site for professional and enthusiast programmers.

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.'),
  );
}
  • 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. – Pierre Buyle Sep 16 '16 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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