Is there a way to turn off contextual links in D7 ?

Comments

David_Rothstein’s picture

Yes, just disable the Contextual links module.

bluepal’s picture

Thanks. The reason why I want to turn off the contextual links is because they don't seem to work for ALL themes. For some themes, the "gear wheels" graphic appears all the time for the admin users (not just when the blocks are hovered), and for a minority of odd themes, the gear wheels are significantly overlapping and misaligned.
I don't know if the CSS of custom themes and blocks have to be changed or coded in a certain way for the contextual links to work right (perhaps this should be documented somewhere?). Until then, they are bit of a visual annoyance ... so the compromise is for me to turn it off for now.

David_Rothstein’s picture

Out of curiosity, which themes have these problems? These sound like bugs which should be fixed.

In general, the contextual links stying is such that most themes shouldn't have to do anything for it to look correctly when combined with the theme, but there may be some exceptions to that (especially for color mismatch, that type of thing).

bluepal’s picture

Side effect #1: display:inline . Some custom themes use "display:inline" which causes the contextual links to be misaligned (entire dotted box shifted to the left). For contextual links to work properly, the css of the blocks have to be defined as "display:block".

The misalignment happens in any HTML5 themes that wrap the blocks in section markup and then style the section like this :

section {
display: inline;
}

or the section markup is not styled at all.

Change the "display: inline" to "display: block", then the contextual links are happy and everything is ok. The trouble is that the "display:block" MUST be coded EXPLICITLY because the DEFAULT css behavior is "inline". (Many designers don't like "display:block" because it adds extra spacing at the bottom. Inline is tighter, cleaner and is the default behavior).

Custom themes that add an extra wrapper DIV to the block or content and then style the DIV as "display:inline" (or no style) also show the misalignment. This behavior can be reproduced in any theme.

Side effect #2: nested div . D7 inserts an extra div for contextual links, div class="contextual-links-wrapper". The insertion of the contextual div will throw off existing CSS that depends on nested level of divs! Here is a classic example to reproduce this side effect.

.right-sidebar div div {border: 1px solid #000;}

This css draws a black border around an inner block of the right sidebar, assuming that the second div is for the "inner block". With the contextual links turned on, the extra contextual div is added, making it a three-level nested div, which then causes the above css not to work as intended! (the border is not drawn or drawn on the wrong block!)

So to sum up, the insertion of the contextual links is not as harmless as it may seem. It will cause side effects to the CSS. To mitigate this problem, recode the css or turn off contextual links (I am too scared and too lazy to redo my "perfect" css. Added to the complexity is the css will work for anonymous users with contextual off, and will not work for authenticated users with contextual on. Too many factors, too much trouble. The safest thing is to turn it off. Don't get me wrong: contextual links are great as long you are confident that they don't affect your theme).

Another problem (or possible bug?): How to turn off the contextual links for the ADMIN ? Is it always turned on by default for admin users? In permission, when I turn off the contextual links for the "Administrator", it doesn't work. It is always on for the Admin. I would like to be able to turn it off for Admins too.

David_Rothstein’s picture

Thanks for the detailed description.

For side effect #1, it sounds like you might want to file a bug report? I'm not really sure what the proposed fix would be though. I guess HTML5 themes have to do something to work around this for the time being (e.g. style the contextual links specifically) but it would be nice if Drupal core did not force them to.

For side effect #2, that looks to me like poorly written CSS. Why can't the CSS target the div it wants to affect more specifically (e.g., using a class)? You can't in general write a Drupal theme and assume the markup is always constant. Also, I'm not sure what you mean by a three-level nested div? - as far as I see, Drupal inserts the contextual links div as a sibling of the content div that contains the block content, not as a wrapper around it.

For the issue with administrators always seeing contextual links, if you mean user #1 then it's not a bug; it's just the way permissions work. User #1 always gets all permissions automatically, since it's intended for site maintenance and emergency access, that sort of thing. You can avoid the issue by not using user #1 to do day-to-day administration of the site. I agree that it's pretty confusing that we never explain anywhere that turning off permissions for the Administrator role doesn't affect user #1 - for that, see #572240: Explain in the user interface that user/1 and the "administrator" role are not the same thing :)

allenthehusband’s picture

You can also 'turn off' contextual links for certain specific nodes, views, blocks, etc. from within a TPL file by removing (or commenting out) the $title_suffix variable.

Example: I have a View that uses the Views Data Export module. This particular view also has a field which itself is another view (using the Views Field View module). The Contextual Links for the imported view were messing up my Data Export, so I needed to remove Contextual Links from my imported view *only*. By copying the views--view.tpl.php file from the Views module and renaming the new copy so it would override my one specific view, I was able to effectively turn off the contextual links for that one view only.

bqre’s picture

This worked really well! :)
I just unset the whole #contextual_links array in the block

Utkarsh Harshit’s picture

Hi,
Sometimes the core Contextual Links module can get in your way, or be unnecessary on certain elements. Depending on your theme and workflows, you might want to disable them for certain entities. Unfortunately, the core Contextual Links module does not provide a way to disable them without custom code.

If you don't want to write code, check out my Contextual Hide module. It allows you to hide contextual links for certain entities, such as Nodes, Menus, Blocks and Views.

If you don't want to install another module, I'll explain how to do this through code. What you need to implement is hook_contextual_links_view_alter() in your module. You should place your logic there, and finally remove the links by unsetting the ['#links'] array. Here's the code:

/**
* Implements hook_contextual_links_view_alter().
*/
function YOUR_MODULE_contextual_links_view_alter(&$element, $items) {
// Disable contextual links on views.
if (isset($element['#element']['#views_contextual_links_info'])) {
unset($element['#links']);
}
// Disable contextual links on blocks.
if (isset($element['#element']['#block'])) {
unset($element['#links']);
}
// Disable contextual links on nodes.
if (isset($element['#element']['#node'])) {
unset($element['#links']);
}
}
Finally, if you don't want to deal with any of the above things, you can do this through CSS. It might not be the "best" solution, but it will be get the job done:

.your-node-selector .contextual-links {
display: none!important;
}