I'm useing drupal7 and views3x.

I build a views page, views module code is really complex for me.
So how can I disable and enable a views page programmatically?
Help ~~ Thank you

Comments

nevets’s picture

Why do you want to be able to enable/disable views programmaticaly?

rajeevk’s picture

What kind of view you are using ?

What ever type you create of a view...you can set the list of nodes, where you want that to appear or disappear. You can define content type etc..

If you have any specific need then explain and then only some body can help you...

Thanks,
RajeevK

Rajeev Kumar,
@drupler@bihar.social (ActivityPub)

kidd1126’s picture

Actually, now I am trying to persuade my boss not to do that.
My boss ask me to make a backend interface which user (a man with no drupal or php skills) can manager the views.
Eg: We use views to build a news list page, we do two views page with different styles for the news list page.
So user can preview these two pages and set one enable. This must be done by my interface not views admin page.

rajeevk’s picture

No problem in that.
If you are using Drupal6 then try Dashboard module. You can list the views list for every users & your user get to customize their own dashboard according to their choice.

I don't know this full fill your criteria or not, but sure it will help you if you see the structure of their organizing function.

You can some what functionality like this Drupal7 too.

Thanks,
RajeevK

Rajeev Kumar,
@drupler@bihar.social (ActivityPub)

elvis2’s picture

Here is a snippet of code you would put in mymodule.install

/**
 * Disable workbench views
 */
function mymodule_update_7001() {

  // a list of views (their view name) I want to disable
  $viewnames = array(
    'workbench_current_user',
    'workbench_edited',
    'workbench_moderation',
    'workbench_recent_content',
  );

  // grab list of views that are already disabled
  $views_status = variable_get('views_defaults', array());

  // add our views to be disabled 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 now
  if (function_exists('views_invalidate_cache')) {
    views_invalidate_cache();
  }
}
chOP’s picture

If, like me, you're looking for a snippet showing how to disable a specific views display, then here's the solution I came up with. Just place this code snippet into your module install file mymodule.install and run the database updates.

/**
 * Disable views displays for Example page and feed.
 */
function mymodule_update_7001() {
  $t = get_t();

  $views_name = 'example_views';
  $views_displays = array('views_page_display', 'views_feed_display');

  $disabled_displays = array();
  $variables = array('@view_name' => $views_name);

  $view = views_get_view($views_name);
  if ($view) {
    foreach ($views_displays as $display_name) {
      if (isset($view->display[$display_name])) {
        $view->display[$display_name]->display_options['enabled'] = FALSE;
        $disabled_displays[] = $display_name;
      }
    }
    views_save_view($view);
  }
  else {
    $message = 'Unable to load @view_name from views.';
    throw new DrupalUpdateException($t($message, $variables));
  }

  if (count($disabled_displays)) {
    $message = 'Disabled @view_name views displays named @displays. ';
    $variables['@displays'] = implode(', ', $disabled_displays);

    $missing_displays = array_diff($views_displays, $disabled_displays);
    if (count($missing_displays)) {
      $message .= 'Missing @view_name views displays named @displays could not be disabled. ';
      $variables['@missing'] = implode(', ', $missing_displays);
    }
  }
  else {
    $message = 'Nothing to do. No @view_name views displays named @displays found to disable. ';
    $variables['@displays'] = implode(', ', $views_displays);
  }

  return $t($message, $variables);
}


Hope this helps save you time. I've tested it out as a pattern in my own module and it has worked fine so far.

--
Madness takes its toll.
Please have exact change.

codechefmarc’s picture

Thanks! Elvis2's version worked perfectly for me.

Tunprog’s picture

Thank you elvis2 this works like a charm for me too ;).