6093 comments
2357 subscribers
6238 on Twitter
Subscribe! Feed reader E-mail

Drupal in the Trenches: Fighting with Views

The other developers have bought into the idea that all behavior-related changes should be in the source code. It’s the only thing keeping us sane with four developers and three environments: local, testing, and production. It has its own challenges, though, like this one:

Problem: Blocks based on views with dependencies on custom tables sometimes don’t show up when we refreshed from the database dump, although things work if we update the database one revision at a time.

After far too much pain and suffering, I figured out that _views_get_default_views was caching the results. When it checked our newly-enabled modules for default views, it found some views whose dependencies hadn’t gotten enabled yet, so it didn’t cache those. The next time _views_get_default_views was called, it used the values it had stored in a static variable available only in that function.

_views_get_default_views did not provide a way to reset that static variable, so once it was called, the data was practically written in stone.

_views_get_tables is similarly evil. (ARGH!)

This might’ve been fixed in a Views update (and we’re still using Drupal 5, in any case), but I’m not going to suggest updating the module this close to external acceptance testing.

So our options were:

  • Make sure _block_rehash and similar functions never get called before the update function that enables the module. Problems: Not only do we need to untangle the order of update functions that get called and be hyper-aware of what other modules do, but we’ll also need to change old update functions if we ever need to do this for another view in the future.
  • Hack _views_get_default_views and _views_get_tables to accept an optional parameter that resets the static variable.

When there are no clean alternatives, you just gotta get your fingers dirty and hack code.

Was that really only one hour of my life? It felt so much longer.

Short URL: http://sachachua.com/blog/p/5978
  • http://angrydonuts.com merlinofchaos

    views_invalidate_cache() at the end of your update. Everything will be reread properly on the next page update.

  • http://rtfverterra.mathalino.com RTFVerterra

    Wow! the lord and creator of views himself is here. My site won’t live without views. :)

  • http://sachachua.com Sacha Chua

    merlinofchaos: Right, but not in time for other update functions that try to mess around with the blocks table to set visibility and things like that. What’s the canonical Drupal 5 way to set block visibility for views that provide blocks, in code? I think the developer tried inserting into the blocks table, but he reported problems with it.

  • http://angrydonuts.com merlinofchaos

    Ugh, block visibility. Since the delta of the view won’t change, being based on the view name, you could just write the block record directly instead of trying to modify it, I suppose. There’s nothing dynamic going on in there that I can think of, so just writing it the way you know it should be should work for that update.

    Also, possibly moving the module enables earlier? I realize that might not be feasible, sometimes order of things is important. It is *very* rare that the available default views is expected to change during a single pageload (first time in 3 years I’ve heard of anybody needing to, for example) so I guess I’m not surprised I never thought to put in a way to reset that static cache.

  • David Stosik

    Here is how I update my blocks table in my hook_update_N, 4 days ago:

    views_invalidate_cache(); // to be sure our last default views (defined in files) are loaded

    // change theme_key just during _block_rehash, so that blocks for the good theme are rehashed
    global $theme_key;
    $old_theme_key = $theme_key;
    $theme_key = ‘blog_rfi’;

    // rebuild the blocks entries from blocks table for theme ‘blog_rfi’
    _block_rehash();

    // restore old theme_key (which is probably something like administration_theme, as we’re in hook_update_N)
    $theme_key = $old_theme_key;

    After this, I can do some UPDATE on blocks visibility, with WHERE clauses looking like “theme=’blogs_rfi AND module=’my_module’ AND delta=1″.

    Hope this will help. :)

    Regards,
    David

  • http://sachachua.com Sacha Chua

    That generally worked for us, until we had a case where we had two update functions that called _block_rehash, which causes _views_get_default_views to store the results in a static variable. Or maybe it’s just the version of Views we’re still using… (Ah, the Drupal 5 world).

  • http://sachachua.com Sacha Chua

    merlinofchaos: Yeah, I suggested using DELETE and INSERT instead of UPDATE, but the developer mentioned he had been having weird problems with that, too. I haven’t confirmed for myself how deep the rabbit hole goes, though.

    Three years, eh? <laugh> Maybe I really am weird for insisting that all behavior-related changes (blocks, views, anything that moves) need to be checked into the source code some way or another, which usually means writing install functions. This has been fantastic for smoothly deploying to production sites, but occasionally runs into assumptions in code.

    For example, I live in dreadful fear of running into an update function that uses #finished = 0, because I don’t know how to simulate a browser refresh and new instance of PHP (while still keeping session variables) in my Drush command-line tool for updating all the modules…

    By the way, thank you so much for your time and insight, and for the awesomeness that is Views!

  • David Stosik

    Sacha Chua: you just made me understand where really was your problem, and that the code I pasted won’t help… That was a tough one! ^^

    By the way, you’re not so weird: I have been thinking the same thing for long and started doing it for real some days ago. :D

    David

  • http://angrydonuts.com merlinofchaos

    It’s not weird to want to do this, it’s just that most of Drupal does not currently provide the tools necessary to do this. Views actually for the most part does; the problem you’re running into is that blocks don’t, and because they don’t, they run into something where *I* made an assumption that default views would never actually change during a page load.

    We still live in a world where sometimes you have to use drupal_execute(). This is unfortunate.

    As an aside, the captcha words for this comment disturb me.

  • http://sachachua.com Sacha Chua

    drupal_execute wouldn’t solve this particular views problem, I think, but it does come in handy for other things. =) (I think every module developer should write APIs first.)

    I wonder how to control the words for my reCAPTCHA…

On This Day...

  • 2013: Emacs Chat: Carsten Dominik — In which Carsten shares how he got started with Emacs, the joys of Calc, and other cool things. =) Carsten [...]
  • 2010: Reflections on PresentationCampToronto — At Tuesday’s PresentationCampToronto organized by Chris Gurney, I gave an eight-minute talk on relentlessly improving your presentation skills. This was [...]
  • 2009: Drupal gotchas: Never ever ever use anything less than module AND delta to specify blocks — One of the common mistakes I run into is people not specifying enough information when deleting or updating entries in [...]
  • 2009: Upcoming Web 2.0 Conferences — Web 2.0 Expo – SF March 31 to April 3, 2009 (Schedule) San Francisco, CA Conference plus workshops: $1745 before March 30, $1945 [...]
  • 2009: Ada Lovelace Day linkfest and wrapup — Here’s a quick wrap-up of some posts from Ada Lovelace Day: Linda Rodriguez wrote about how Ada Lovelace Day came to [...]
  • 2009: Helping my parents learn more about Internet and business; any tips? — Last Sunday, my mom and I were talking about other businesses that she and my dad could explore. She’d recently [...]
  • 2009: Looking for female IT role models in Toronto? — Joey de Villa (Accordion Guy) has put together a great list of some awesome Toronto women in technology. I know [...]
  • 2008: Optimizing my day — Maybe there’s some truth to the advice, “Early to bed and early to rise makes a man healthy, wealthy, and [...]
  • 2007: Sunday tea and developing my personal style — You discover yourself through your choices. One of the things I like about having my own place is learning more about [...]
  • 2007: Tea — I picked up another teapot and a wooden box of assorted teas at Winners this afternoon in preparation for my second [...]
  • 2006: Speaking of The Miniskirt… — Lusine was surprised to find out that I’m taking my master’s in mechanical and industrial engineering. (I really should drop the “mechanical [...]
  • 2006: Noodle night — One of the wonderful things about Graduate House is that the Graduate House Council organizes a lot of social events. Tonight [...]
  • 2006: Where do network cards go to die? — I’ve rummaged through all of my things, and I’m certain I left my wireless network card in the Philippines. Mumble. That’s [...]
  • 2004: ACM Technews: “Search Beyond Google” — From ACM Technews: Google’s enormous success with its search engine—and its apparent inability to develop a follow-up innovation momentous enough to sustain [...]
  • 2004: Paolo’s first webcomic — Looks like Paolo Venegas is starting a webcomic!
  • 2004: Remembering — A friend and I were thinking of papers to submit to the Loyola Schools Review. I joked, “What could I write [...]
  • 2003: killer examples for computer science education — http://www.cse.buffalo.edu/~alphonce/OOPSLA2002/KillerExamples/
  • 2003: Computer Braille Code Reference Card — nabs-l@nfbnet.org, Wade Hemmelrick: This is a free listing of all the computer braille code symbols. Computer Braille Code Card in Grade 2 [...]
  • 2003: python web solutions — python@lists.free.net.ph, Andy Sy: http://www.python.org/cgi-bin/moinmoin/WebProgramming
  • 2003: Emacs test suite — Robert Anderson has some testing scripts at http://rwa.homelinux.net/emacs
  • 2003: Chinese Python? — Miguel Paraz, python@lists.free.net.ph: http://www.chinesepython.org/cgi_bin/cgb.cgi/english/english.html The author claims that Python is easy to translate to Chinese. This could be useful to the Chinese-literate people [...]
  • 2003: [[ComputerScienceCurriculum2002-2003]] — Finished encoding the ComputerScienceCurriculum2002-2003. Whew!
  • 2003: Summary of G. Polya’s “How to Solve It” — education — http://www.math.utah.edu/~alfeld/math/polya.html Local copy: PolyaHowToSolveIt
  • 2003: Free mailing lists — Tech mailing lists without a home might want to check out http://www.freelists.org .
  • 2003: Nemeth mathematics — On the blindprogramming@yahoogroups.com mailing list, Yvon Provencher mentions the useful tutorial at http://www.freedomscientific.com/fs_downloads/notenemeth.asp .
  • 2003: emacs-wiki-markup-word — I use (defun emacs-wiki-markup-word ()) to squelch all the bad markup I’m getting lately.
  • 2003: equals in URLs and emacs-wiki.el — I suspect that URLs need to be verbatim; stray equal signs and underscores can be their undoing! Maybe I should just [...]
  • 2003: solition paper — Cross-reference: CoastLog#1
  • 2003: magicpoint — Chris Beggy writes in with another tip (thanks!). Chris thinks that Magicpoint is an excellent PowerPoint replacement. I like Magicpoint’s text-based presentation files [...]
  • 2003: pdf presentations — On debian-user@lists.debian.org, David Z Maze thinks Prosper is great for making PDF presentations from LaTeX. xpdf -fullscreen -papercolor black to actually run [...]

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging

Free sample!