Drupal hook_menu_alter () for adding tabs

I want to add some tabs on the page "node /% / edit" from my module called "cssswitch". When I click Rebuild Menu, two new tabs are displayed, but they are displayed for ALL nodes when editing them, and not just for node "cssswitch". I want these new tabs to appear only when editing a node like "cssswitch".

Another problem is when I clear the entire cache, the tabs completely disappear from all the editing pages. Below is the code I wrote.

    function cssswitch_menu_alter(&$items) {

        $node = menu_get_object();
        //print_r($node);
        //echo $node->type; //exit();
        if ($node->type == 'cssswitch') {

            $items['node/%/edit/schedulenew'] = array(
                'title' => 'Schedule1',
                'access callback'=>'user_access',
                'access arguments'=>array('view cssswitch'),
                'page callback' => 'cssswitch_schedule',
                'page arguments' => array(1),
                'type' => MENU_LOCAL_TASK,
                'weight'=>4,
            );

            $items['node/%/edit/schedulenew2'] = array(
                'title' => 'Schedule2',
                'access callback'=>'user_access',
                'access arguments'=>array('view cssswitch'),
                'page callback' => 'cssswitch_test2',
                'page arguments' => array(1),
                'type' => MENU_LOCAL_TASK,
                'weight'=>3,
            );  


        }

    }

function cssswitch_test(){
    return 'test';
}

function cssswitch_test2(){
    return 'test2';
}

Thanks for any help.

+3
source share
1 answer

hook_menu_alter() , node .

, , :

       // Note, I replaced the '%' in your original code with '%node'. See hook_menu() for details on this.
       $items['node/%node/edit/schedulenew2'] = array(
            ...
            'access callback'=>'cssswitch_schedulenew_access',
            // This passes in the $node object as the argument.
            'access arguments'=>array(1),
            ...
        );  

:

function cssswitch_schedulenew_access($node) {
  // Check that node is the proper type, and that the user has the proper permission.
  return $node->type == 'cssswitch' && user_access('view cssswitch');
}

node false, .

+8

Source: https://habr.com/ru/post/1746450/


All Articles