I am building a site using openatrium.
One of the feature modules in openatrium contains a view ("atrium_members") that I would like to disable.

Is it possible to disable a view programmatically?

Of course, I could disable the view manually. But I'm trying to avoid manual intervention, if possible.
Thanks.

Comments

dawehner’s picture

in theory

$status = variable_get('views_defaults', array());
$status[$name] = TRUE;
variable_set('views_defaults', $status);
kobnim’s picture

Works like a charm. Thank you!

dawehner’s picture

Status: Active » Fixed

So

Status: Fixed » Closed (fixed)

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

leex’s picture

D7 disabling using hooks, not variables:

The following would enable the default taxonom_term view, to disable an already enabled view, change FALSE to TRUE, depending on what other modules are doing, you may need to weight your module heavier to get the results you need.

/**
 * Implememnts hook_views_default_views_alter().
 */
function basic_ubercart_main_views_default_views_alter(&$views) {

  if (isset($views['taxonomy_term'])) {
    $views['taxonomy_term']->disabled = FALSE;
  }

}

With regards to ENABLING views in code: The problem with using variables is that if you're setting it in a module on install for example, there is no way to reliably remove the variable setting later (to disable the view) without knowing which other modules are using the view.

Here we're saying we'd like this view enabled, but once the module is disabled the view will go back to it's default state (disabled) if nothing else is using it.

karopka’s picture

Issue summary: View changes

to disable an already enabled view

I have view with two attachment displays.
Can you show how to programmatically disable one display from a view?

joco_sp’s picture

I'm using D7.

#1 worked for me.
#5 didn't.

a.milkovsky’s picture

janusman’s picture

This worked for me under D7 (from http://www.snip2code.com/Snippet/165083/Drupal-disable-a-Views-view-prog... )

drush --uri=example.com ev '$view_name = "myview"; $view = views_get_view($view_name); ctools_export_crud_set_status("views_view", $view, TRUE);'

When disabled, it shows up grayed out in the /admin/structure/views page. And changing the last 'TRUE' to 'FALSE' and re-running the above re-enables it.

TechNikh’s picture

If anyone is looking to disable a display of a view and not the entire view, below code worked for me

$view_name = "course";
  $view_display_name = "block_5";
  $view = views_get_view($view_name);
  $view->display[$view_display_name]->display_options['enabled'] = FALSE;
  views_save_view($view);
jedihe’s picture

#9 worked for me (D7).

chOP’s picture

For anyone searching the issue queue for an answer to this, take a look at this Forum post:

How to disable or enable views page programmatically?

swirt’s picture

Comment #9 from Janusman worked so well I expanded it into a release in Module: Hook Update Deploy Tools so others can call it an get validation on the results in a hook_update_n()
https://www.drupal.org/node/2720425

mon623’s picture

I am new to Drupal8 and didn't understand where to place the code mentioned in #9. Can anyone please help me out? I had disabled some views related to ubercart module and now i am not able to login to the backend. I get Unexpected error message. And the log files show this exception

[Mon Sep 26 16:13:32.409313 2016] [:error] [pid 11696:tid 1856] [client ::1:59358] Uncaught PHP Exception Symfony\\Component\\Routing\\Exception\\RouteNotFoundException: "Route "view.uc_cart_links.report" does not exist." at C:\\xampp\\htdocs\\drupal-final\\core\\lib\\Drupal\\Core\\Routing\\RouteProvider.php line 187

Will enabling the views solve this issue? If yes then where and how can i enable them?

Thanks

Tunprog’s picture

You can disable views programmatically in D7 like this:

/**
 * Disable nodes and comments views.
 */
function YOUR_MODULE_update_7000() {
  // A list of views to be disabled
  $viewnames = array(
    'admin_views_node',
    'admin_views_comment',
  );
  // Grab the list of views that are already disabled
  $views_status = variable_get('views_defaults', array());
  // Add our views to the list
  foreach ($viewnames as $key => $viewname) {
    $views_status[$viewname] = TRUE;
  }
  // Reset the variable with the new list
  variable_set('views_defaults', $views_status);
  // Empty cache
  if (function_exists('views_invalidate_cache')) {
    views_invalidate_cache();
  }
}