0

I am writing a module that has 2 views in code. At first, I pasted the code that was exported from views directly into hook_views_default_views() like so...

function MY_MODULE_views_default_views() {
  $views = array();

  // Copy and pasted code from views export here...

  $views[$view->name] = $view;
  return $views;
}

After installing the module, the only way to get the views to show up would be to run cron and then flush the caches. I tried just running cron, or just flushing caches, and it would only work if I ran them both in that order. Once the views showed up, I could go along with things fine for a little while, then randomly the view would disappear again. I could not figure out a pattern to why they would disappear. Flushing caches would mostly fix it. Sometimes I had to flush caches two or three times or run cron and flush caches to get the views to appear again.

I searched around for some help and was able to find this post... A whole view disappears. This seems to be a similar problem. The function is too long and each view needs to be split into separate files. Okay, I did that and now my code looks like this...

// Files located in "my_module/views":
my_module.view_1.view
my_module.view_2.view

/**
 * Implementation of hook_views_default_views().
 */
function MY_MODULE_views_default_views() {
  $views = array();
  $files = file_scan_directory(drupal_get_path('module', 'MY_MODULE'). '/views', '/.*\.view$/');
  foreach ($files as $filepath => $file) {
    require $filepath;
    if (isset($view)) {
      $views[$view->name] = $view;
    }
  }
  return $views;
}

This is a nice solution as it makes adding new views very easy and clean. However, I am having the exact same experience as before. After installing the module, it still takes a cron run and a cache flush for the views to show, which they eventually do, but then they still disappear randomly. The only way to get them back is to flushes the caches (sometimes several times), or run cron and flush caches.

I'm feeling pretty stuck here. If anyone would be so kind as to offer some guidance, that would be most appreciated. Thank you.

  • Are you implementing hook_views_api() in your module? – Alfred Armstrong Sep 20 '14 at 16:53
  • Yes, I'm implementing hook_views_api(). When the views are showing up in the list, everything works fine. They just disappear sometimes until I flush the caches again. Sometimes it takes two or three flushes. – mlanning Sep 20 '14 at 17:22
  • I would make sure the max packet size in MySQL is bigger than the default 1mb; I'd use 32mb if you're wondering – mikeytown2 Sep 20 '14 at 17:32
1

Thank you so much to kladria on IRC #drupal-support for helping me through this one!

After using a debugger to step through the code, I saw that my implementation of hook_views_default_views() was not being called. The problem I was having was that the function was in the wrong file. Originally, I had the function "MY_MODULE_views_default_views()" in the file "My_MODULE.views.inc", and it is supposed to go in "My_MODULE.views_default.inc". That "_default" made all the difference for it to be auto-loaded correctly.

So far, there has been no more needing to flush caches, and the views haven't disappeared. I'm pretty sure this did the trick.

  • which debugger did you use? any tips on how to set one up? I usually work off of the shell and never gotten to use a debugger yet. – Patoshi パトシ Oct 19 '16 at 15:19
  • 1
    I use xdebug with NetBeans IDE. There is some setup that you'll need to do to your php configuration file to get it working. Google Chrome has an extension called "Xdebug Helper" that works with it all too. – mlanning Oct 19 '16 at 15:30
  • i mainly use VIM. also does adding xdebugger slow down the site? i need to debug on production server. i dont have a dev. – Patoshi パトシ Oct 19 '16 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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