6096 comments
2357 subscribers
6254 on Twitter
Subscribe! Feed reader E-mail

Drupal gotchas: watch out for Views dependencies

One of the downsides of writing all configuration changes as update functions in .install files is that you need to watch out for static or cached variables and module dependencies.

For example, I just finished helping a teammate debug an elusive Views problem. The symptom: some views with blocks didn’t show up in the blocks table, even after a _block_rehash in the update function. Other views were there. If we ran the update function independently, all the blocks appeared in the blocks table. What was going on?

hook_views_default_views is a great way to programmatically define views. By using the Views Export function, you can interactively create the view, then export the code so that you can make the view available on your production site without manual administration.

However, if your new view requires a table ($view->requires) that doesn’t exist, then _views_get_default_views discards it. By the time you enable the module in another update function (and you’re using update functions to do things like that instead of manually enabling modules on your production site, right?), views_cache.inc has cached the (incomplete) views list and won’t recognize that the requirements are now available.

To get around the view dependencies, comment out the $views->requires line. It’s a hack, but it’s probably better than hacking all the functions that cache data in static variables.

Short URL: http://sachachua.com/blog/p/5870
  • http://mikeyp.net Michael Prasuhn

    Wouldn’t it be easier to just do multiple update functions and make sure the required modules are enabled before clearing the views cache?

  • http://sachachua.com Sacha Chua

    I use multiple update functions already, but I can’t figure out how to clear the internal static $views_default_views; in _views_get_default_views. I’m tempted to hack the function to accept an optional parameter to clear it.

On This Day...

  • 2011: Leveling up as a developer! — It’s satisfying adding a bunch of IBM acronyms to my “marketable skills.” They’re not that intimidating after all! I [...]
  • 2010: Experiments as a presenter — After I shared The Shy Presenter with 200 people at last Wednesday’s Ignite Toronto, Rohan Jayasekera told me that he [...]
  • 2007: The tension between research and research — I’m starting to feel that my thesis is actually doable. I’ve applied for the ethics review of my experimental protocol. The [...]
  • 2006: CookOrDie: Domestic goddesshood — I hosted a small party to thank some of my friends for truly extraordinary acts of kindness. =) Calum, James, Steve [...]
  • 2004: CS161 Finals Schedule — Whoops, we don’t have a schedule for the CS161 finals yet. I’d like to have it on Thursday morning or on [...]
  • 2004: More stuff about finite automata in LaTeX — I had exactly this issue when I was tutoring a group of Computability Theory students. I found that only GNU Pic [...]
  • 2004: Free calculus textbooks — http://books.slashdot.org/article.pl?sid=04/03/04/028253&mode=thread
  • 2004: Jester: A test coverage tool that introduces bugs — http://jester.sourceforge.net/ “Why just think your tests are good when you can know for sure? Sometimes Jester tells me my tests are airtight, [...]

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging

Free sample!