0

My vocabularies machine name is vocab_ . $account_id .
First Funcs: too see the user $account_id is the same with vocab.. if yes then return TRUE.

function _term_account(){
  global $base_url;
  global $user;
  $account_id = custom_get_user_account_id();
  $tesr = explode('_', arg(3));
// admin/structure/taxonomy/vocab_21117 .. witch 21117 is $account_id
 if($tesr[1] == $account_id){
    return true;
 } else {
    return false;
 }
}

I use hook_menu_alter to alter the taxonomy url:

function custom_menu_alter(&$items) {
  // Works fine.. So every user has he's own and only vocabulary..
  $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/list']['access callback'] = '_term_account';
  // DOESN'T WORK.
  $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/add']['access callback'] = '_term_account';
}

UPDATE
When User edit term..

function _term_account_edit(){
    $account_id = custom_get_user_account_id();
    global $base_url;
    $term = taxonomy_term_load(arg(2));
    if($term->vocabulary_machine_name == 'branches_'.$account_id){
        return true;
    }else{
        return false;
    }
}

function custom_menu_alter(&$items) {
  $items['taxonomy/term/%taxonomy_term/edit']['access callback'] = '_term_account_edit'; 
}

The Delete Button dosen't Show up? Why?

7
  • 1
    How do you know it doesn't work? Presumably you're expecting access to be granted/denied and that's not working, but how have you identified that line as the culprit, as opposed to, say, another module with a higher weight also implementing hook_menu_alter for that path? We need more to go on than just that one function, some context and the results of your own debugging are needed too
    – Clive
    Commented Jul 12, 2017 at 14:07
  • 1
    Adding to @Clive, we do now know the expected behavior. What is your intention? From the url you want to block people from adding terms to a certain vocabulary right? This does not restrict access to creating vocabularies. Commented Jul 12, 2017 at 14:09
  • @Neograph734 i need only certien users to add term to certien vocab
    – Barry
    Commented Jul 12, 2017 at 14:12
  • Please edit the question and include some more details :) Commented Jul 12, 2017 at 14:13
  • 1
    @Neograph734 update it
    – Barry
    Commented Jul 12, 2017 at 14:21

2 Answers 2

1

The code looks good! However, try the following:

  • See if there is already a permission or a module which lets you control the things you wish to control. Maybe you can mention the desired outcome or the objective in the question?
  • Add a dpm() or something to the end of the function to see if the $items are being modified correctly.
  • Add a dpm() or something in the _term_account() function to see if it is being called correctly and that it is returning TRUE or FALSE correctly as per your requirements.
5
  • it works fine with "list".. but when i try to "add" any term .. return false..
    – Barry
    Commented Jul 12, 2017 at 14:23
  • What's different between ['access callback'] = '_term_account'; and ['access callback'] = _term_account(); the weird thing it all works fine :)
    – Barry
    Commented Jul 19, 2017 at 17:37
  • 1
    Well, access callback is supposed to be a callable, i.e. a function or a method. If it is a callable, then Drupal will call it with access arguments, if any, and the output of the callback will decide if the user can access the path. However, _term_account() will not be a valid callable, so it might be evaluated as a boolean TRUE and then all users might have access to the path without any access check. I think setting _term_account() might even throw errors, if not, then the boolean TRUE thing is happening, which is a security issue.
    – Jigarius
    Commented Jul 21, 2017 at 15:57
  • So what about Delete button? Is something wrong in my code that avoid show the delete btn ?
    – Barry
    Commented Jul 23, 2017 at 6:15
  • Try debugging your access callback and check if you are returning TRUE or FALSE correctly for the delete case. Your access callback doesn't need to load the taxonomy term like you are doing. Use 2 arguments in your access callback, $term, $action and then use access arguments = [2, 'edit'], etc as the case maybe. Try to study an existing access callback provided by some other module. I think you are not properly returning TRUE for delete access. See drupal.org/node/553368.
    – Jigarius
    Commented Jul 23, 2017 at 14:30
0

I fix it .. Just disable taxonomy_access_fix module cause it has the same menu alter with access callback ..

2
  • There is no way anyone would know this - that is not a core module nor mentioned in your original question.
    – Kevin
    Commented Jul 12, 2017 at 17:33
  • @Kevin in first place i don't know is Any module use menu alter exactly like i use it above.. so that's why i search for $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/add']['access callback'] in the whole modules .. then i found out taxonomy_access_fix module use the same menu alter..
    – Barry
    Commented Jul 13, 2017 at 5:26

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.