How to hide Edit | View tabs?

后端 未结 11 988
轮回少年
轮回少年 2021-02-01 04:43

Can I hide the

Edit | View

tabs on top of each node ?

I\'ve searched for this option in theme settings (both global and sta

相关标签:
11条回答
  • 2021-02-01 05:16

    D8 solution: If you want to hide all "local" tabs on certain pages, remember that "Tabs" is listed in the block library: find it in the "Content" region and exclude by content type, page URL or user role.

    0 讨论(0)
  • 2021-02-01 05:17

    On the module side, you could do something that decouples the Edit's menu entry from the local tasks for the node:

    function custom_menu_alter(&$items) {
      $items['node/%node/edit']['type'] = MENU_CALLBACK;
    }
    

    The edit path is still there, but now it is not associated with the View tab. This includes the edit page itself--no View tab there.

    0 讨论(0)
  • 2021-02-01 05:23

    here is a very easy solution for you. (Drupal 7)

    • Open your page.tpl.php in your current template and search for the $tabs variable.

    • Remove the render code if you want to hide it completely.

    • If you want to display it only to administrators use this code

      <?php if ($tabs and $is_admin): ?>
      <div class="tabs">
        <?php print render($tabs); ?>
      </div>
      

      The above code checks if the user is administrator. If it is it will render the tabs. If not it wont render them.

    0 讨论(0)
  • 2021-02-01 05:25

    This really is a presentational thing, not a functionality thing, so it should be done at the theme level.

    The problem with overriding theme_menu_local_tasks() is that you override/take a hatchet to the entire local task display, when you really just want to get in there with a scalpel to remove two specific local tasks. So, you need to get a little more specific.

    theme_menu_local_tasks() gets the current page's local tasks and passes them to menu_local_tasks(). Here, two theme functions are used:

    1. theme_menu_item_link(), which gets the link markup for the task
    2. theme_menu_local_task(), which gets the <li> element for the task.

    So, you can get rid of the View and Edit local tasks in a really robust way by overriding theme_menu_item_link() and theme_menu_local_task() to include your check for them:

    function mytheme_menu_item_link($link) {
      // Local tasks for view and edit nodes shouldn't be displayed.
      if ($link['type'] & MENU_LOCAL_TASK && ($link['path'] === 'node/%/edit' || $link['path'] === 'node/%/view')) {
        return '';
      }
      else {
        if (empty($link['localized_options'])) {
          $link['localized_options'] = array();
        }
    
        return l($link['title'], $link['href'], $link['localized_options']);
      }
    }
    
    function mytheme_menu_local_task($link, $active = FALSE) {
      // Don't return a <li> element if $link is empty
      if ($link === '') {
        return '';
      }
      else {
        return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";  
      }
    }
    

    This way, you're relying on the menu router path, not modifying the menu router item, and achieving the result you want with minimal changes to core functionality or theming.

    0 讨论(0)
  • 2021-02-01 05:25

    View and Edit are functional features. They have a reason for being there.

    The best way to "remove" them, is to "remove" that functionality alltogether. After all: why remove the interface of a piece of functionality, but not the functionality itself? Besides, simply not printing the tabs, does not remove the url endpoints. In other words: if you don't print the edit tab, people can still access the edit page.

    Again: best is to remove that functionality: The fact that you don't want the edit tab, sounds as if you don't want the edit functionality for certain users. If so, then just remove that permission for that role. That is all. The tabs will be gone.

    If, however, you simply wish to display these tabs differently, Drupal is your friends. As you may have noticed, they are called local tasks and not tabs. That is because the theme decides how to render them: The theme is the thing that decides to show them as tabs.

    Simply override the theme_menu_local_tasks() to create your own HTML for the "local-tasks". And in your page-tpl, simply move the $tabs variable around to a place, where you want them.

    But again: Don't try to change the behavior of the app, by removing interface-elements. That is not the right thing to do: you should change the behavior, in order to change the behavior :)

    0 讨论(0)
  • 2021-02-01 05:25

    Thanks for the last answer. But be aware of this detail: if you try it as-is it cannot work: literals should be just rounded with " or ', not both of them altogether. So it should be:

    /**
     * Implements hook_menu_local_tasks_alter() to unset unwanted tabs
     */
    function MYMODULE_menu_local_tasks_alter(&$data) {
      foreach ($data['tabs'][0]['output'] as $key => $value) {
        if ($value['#link']['path'] == "node/%/view") {
          unset($data['tabs'][0]['output'][$key]);
        }
      }
    }
    

    Once taken care of it, it works :)

    0 讨论(0)
提交回复
热议问题