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

Drupal debugging story: Rules defined in multiple Features

Fatal error: Unsupported operand types in …../patched/rules/rules/rules.module on line 347

That was the error message Daniel sent me when he asked for my help in debugging. After some digging, I found out that the rules had been defined in two features, so Drupal got thoroughly confused. After I commented out one of the implementations of hook_rules_defaults and deleted the relevant rows from rules_rules in the database, the site worked again.

Daniel wanted to know how I figured out that problem, so here’s the story.

The line number told me that rules_sort_children was having problems. I added a var_dump to it so that I could find out what kind of unexpected data was causing the errors.

if (!is_numeric($element[$key]['#weight'])) { 
  var_dump($key, $element[$key]['#weight']); 
}

The output showed that the regular rules were fine, but our custom rules weren’t – weight was an array instead of an integer.

I looked at the difference between the features in code and the features in the database. The rules were being duplicated. I tried updating the features to match the information in the database, but the code looked wrong, so I used git to stash the changes. I also tried reverting the features, but that didn’t solve the problem either. Time to dig deeper.

I backed up the database, then deleted our custom rules from rules_rules. Still problematic. I commented out the rule definitions in our site_structure.features.inc. The rules administration page now successfully displayed – but mysteriously, the rule definitions were still available.

I looked at the rule tags to see where else they might be defined, and found that another feature had included the rules. Aha! I commented those out. As expected, the rules disappeared from the administration page. I’d identified the problem: the rules had been defined in more than one module, which had thoroughly confused Drupal.

Because it made more sense to define the rules in our site_structure feature than in the other feature, I uncommented the site_sitestructure_rules_defaults definitions and left the other feature’s rules commented. That worked.

I tried restoring the rule customizations from the database, but that gave the same error. The database copy had multiple definitions, and I didn’t feel up to picking my way through the data or writing a Drush script to manipulate the rows. I re-deleted the customizations to get a clean copy. Just in case the other feature had more recent definitions of the rules, I compared the two. Aside from the extra tag, they were identical, so I didn’t need to copy any information over. It meant that Daniel would have to make his changes again, though.

Features: When it’s good, it’s very very good. When it’s bad, it results in quirky bugs. Make sure you don’t define your rulesets in multiple features. Drupal Features still has some rough spots when it comes to Rules. I remember when I created this feature with the rules in it – it created duplicate rules, so I needed to delete the old ones. Still, it’s a great way to export rules and other configuration changes to code, even though it takes some getting used to (and the occasional bit of database-diving).

Anyway, that’s the story of how I identified that issue and worked around it.

When you’re faced with a fatal error involving unsupported operand types, figure out what kind of operands it expects and print out anything that doesn’t match. Then you can start figuring out the real problem, which is how that data got in there in the first place. I’ve used this to find form elements that were mistakenly defined, array elements that were unexpectedly null, and so on. Don’t be afraid to add debugging code to core or contributed modules, particularly if you can use source code control to restore a clean copy. If you use a runtime debugger like XDebug, you can easily explore the data and the call stack. If not, there’s always var_dump.

Hope that helps!

Short URL: http://sachachua.com/blog/p/22446

On This Day...

  • 2012: Process: Keeping notes of conversations — I’m starting to use Evernote for more of my little notes, such as the follow-up notes after conversations. I like [...]
  • 2010: Setting up my new tablet PC – apps, config, etc. — AdAware: check the computer for any malware from previous owner’s use – checked out clean Dropbox: synchronize my [...]
  • 2008: Editors for blogging — Penelope Trunk’s editor helps her improve her blogging and her tweeting: In case you didn’t know, I have an editor for [...]
  • 2008: On Changing the World — Like Daniel at Young and Frugal, I am going to change the world. I’m changing it already, and as I grow, [...]
  • 2008: Celebrating the little things you learn — Photo (c) 2007 Sean Dreilinger, Creative Commons Attribution Noncommercial Copyleft I love learning. I love being able to do something I’d [...]
  • 2007: Speaking of fitness landscapes… — Neil wanted to follow up on my post last 2008.08.12 about fitness landscapes and life, so here it is: Imagine that a [...]
  • 2006: The power of a good nap — I feel that I’m stretching myself a bit too thin again, so I might just cancel my Thursday stuff. I scheduled [...]
  • 2006: Oopsie — Hate it when I plan to go to sleep early and a tech problem comes up. Forgot my domain account password [...]
  • 2005: Voicemail — Figuring out voicemail for the first time. It’s so nice hearing people’s voices… I’m really glad my phone is a great [...]
  • 2005: 30-day challenges: Touch three times — After having successfully become an early riser, J Wynia has set a new challenge for the next 30 days: read [...]
  • 2005: I need pictures — After flipping through all of my digital pictures, I’ve realized that I don’t have a good head shot. My nicest pictures [...]
  • 2005: The joy of paper — There is something inexpressibly elegant about fine cream cards and gold-lined envelopes, and it was with great delight that I found [...]
  • 2005: Mom’s trip — Mom’s on her way to Toronto! =) She’ll be here from August 31 (AC 272, 16:40) to September 14 (AC [...]
  • 2003: School of Humanities Lounge Day — Welcome to Lounge Day. We’re supposed to showcase not only the newly renovated building, but also showcase the heart and soul [...]
  • 2003: TFI followups — Intellectual autobiographies, capitalism; memo sent out today
  • 2003: Extreme programming: Process vs. culture — http://www.javaworld.com/javaworld/jw-08-2003/jw-0815-xp.html?

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging

Free sample!