3

I have another view that uses the same path as the front page view which is "/node" and I will like to disable the front page view programmatically.

How can I do that from a hook_install?

2 Answers 2

13

Views are entities, so:

  1. Load the View:

    $view_id = 'content'; // Replace with the ID of the view you wish to disable.
    $view = \Drupal::entityTypeManager()->getStorage('view')->load($view_id);
    
  2. Update the status:

    $view->setStatus(FALSE);
    
  3. And save it:

    $view->save();
    

Or all in one go:

\Drupal::entityTypeManager()->getStorage('view')
  ->load($view_id)
  ->setStatus(FALSE)
  ->save();
1
  • 1
    Perfect. It looks as if the preferred invocation is now entityTypeManager as per drupal.org/node/2549139
    – AdamS
    Commented Sep 8, 2016 at 7:40
3

Even simpler - with the only real drawbacks being that you need a use statement for View and it assumes that is the class for view objects, which would be very unusual not to be the case:

View::load('content')->disable()->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.