Hello, I am trying to render some view programmatically, by invoking:
$rendered = $view->execute_display($displayName);

View renders as usual, with all the context links depending on currently logged in user. But I need this $rendered HTML to be without any contextual links (without both view and node contextual links) even when logged in as administrator.

So, how do I tell views not to render contextual links at all?
Or, is it possible to render the view as for anonymous user (in this case contextual links will go away as well)?

Thank you.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Fixed

Just use this simple code snippet:

$view->hide_admin_links = TRUE;

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

davidwhthomas’s picture

Thanks for this tip!

Is there also a way to hide contextual links from within the Views admin UI?

I'm finding the contextual links are being output in a empty view, resulting in cruft content titles in the template.

I had a look, but couldn't see a

$view->hide_admin_links = TRUE;

type setting in the Views UI.

Dt

alexverb’s picture

Category: support » feature
Status: Closed (fixed) » Needs review

Changing this to a feature request:

I also vote for an option in the UI. I'm embedding a view with the viewfield module to send out as a newsletter. I have no access to any code and also wish to hide those admin links.

dawehner’s picture

Status: Needs review » Active

Update status

damiankloip’s picture

Version: 7.x-3.0-beta3 » 7.x-3.x-dev
Status: Active » Needs review
FileSize
4.66 KB

I have found a couple of instances where this would have been handy. How about something like this?

damiankloip’s picture

As a note on the patch, hide_admin_links is set in the pre_execute() for the plugin. Is that the best place for that?

damiankloip’s picture

FileSize
3.19 KB

Here is an updated patch that relies on whether contextual links locations have been declared instead of directly adding a 'hide admin links' element to the plugin definition.

dawehner’s picture

FileSize
3.16 KB

Just a small skitch what i had in mind.

damiankloip’s picture

Status: Needs review » Needs work
+++ b/plugins/views_plugin_display.incundefined
@@ -532,6 +534,10 @@ class views_plugin_display extends views_plugin {
+      'hide_admin_links' => array(
+        'default' => empty($this->definition['contextual links locations']),
+        'bool' => TRUE,

Ah, so mainly just change this part to be dynamic?

damiankloip’s picture

Status: Needs work » Needs review

oops

damiankloip’s picture

Hmm, I'm not sure now. for example, in admin views, they would now all default to contextual links off beause the plugin definition doesn't declare a 'contextual link locations'. Is this what we want? or should admin views be declaring this? (It isn't needed to have contextual links).

dawehner’s picture

Ah i see what you mean ...

    // If contextual links locations are not set, provide a sane default. (To
    // avoid displaying any contextual links at all, a display plugin can still
    // set 'contextual links locations' to, e.g., an empty array.)
    $plugin += array('contextual links locations' => array('view'));
damiankloip’s picture

Yes, that will do it :) What do you think then?

dawehner’s picture

Having the need for two different kind of flags is kind of odd, because from my perspective having one would somehow be enough.
Can't be check whether it's != array() ?

damiankloip’s picture

FileSize
3.25 KB

dawehner, here is a new patch as discussed on IRC. Yes, I think we should check that 'contextual links locations' isset() and is_array(). This would then work how I expect it to; so page, block, attachment will show this option, and feed etc... will not. I also think the default value for this should always be 'No'. Let me know what you think.

damiankloip’s picture

FileSize
3.25 KB

logic changed for the visibility of this option:

!isset($this->definition['contextual links locations']) || !empty($this->definition['contextual links locations'])

damiankloip’s picture

FileSize
4.59 KB

With updated docs in views.api.php

dawehner’s picture

Status: Needs review » Fixed

Awseome! Committed to 8.x-3.x and 7.x-3.x, though it didn't applied to the plugins branch, what a wonder ...

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jweowu’s picture

Could someone explain why it is that 'hide_admin_links' => FALSE in views_plugin_display::option_definition() ?

That makes this option over-ridden by default when creating a new display, but the default value for the Master display is 'No', and the default for the over-ridden values is likewise 'No', so I don't understand why this isn't TRUE like most other options that are only over-ridden on demand?

Thanks.

damiankloip’s picture

Status: Closed (fixed) » Needs review
FileSize
665 bytes
553 bytes

@jweowu This totally makes sense. I have just tested this out to makes sure it makes sense, and I think it does. Here are patches for both 7.x and 8.x branches.

jweowu’s picture

Status: Needs review » Reviewed & tested by the community

With damiankloip in agreement, I guess it's okay for me to mark this RTBC.

I've confirmed the fix using the D7 patch.

damiankloip’s picture

#22: views-1306564-22-d7.patch queued for re-testing.

dawehner’s picture

+1

damiankloip’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 7.x-3.x-dev » 8.x-dev
Component: User interface » views.module
Status: Reviewed & tested by the community » Needs review
FileSize
726 bytes

Committed and pushed to 7.x-3.x. Now we can move this to 8.x/core.

Here is a re rolled 8.x patch now views is in core.

Nicolas Bouteille’s picture

Thank you guys! I needed it, found this thread and it made my day :)

dawehner’s picture

Status: Needs review » Fixed

I guess this is not needed for d8 anymore.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.