node_load after switching database + cck = dangerous

While everyone agrees the node_load() function and the CCK and Content module are two fantastic things, be carefull using node_load when switching your database in code. The current database you're on doesn't necessarily know which modules are activated on the other database/site and what the properties are of the node you're trying to retrieve. This all makes sense to me now, but I had to learn this the hard way spending 8 hours debugging an enormous and enoying problem where data sometimes was completely corrupted coming back.

CCK uses its own cache table to store data to gain more performance. That's fine. Let's imagine we are on site A, create our node with many properties (hence, lot's of extra cck widgets) and save this to the database. We can look at it, edit etc, it will work just fine. Now, let's switch to our second site where we have a tiny little function which simply does this:

<?php
db_set_active
('siteA');
$node = node_load(1);
db_set_active('default'); // or siteB
?>

Everyone with a bit of Drupal knowledge will know we'll get a full node object back from the database. But there's a lot happening in the background: node_load, module_invoke_all('hook_load') invoking content_load in CCK and here's where things went wrong this afternoon. CCK has it's own hook system calling other cck widgets to know their properties and fields we associated with our node. When there's nothing cached, CCK builds it's own cached object with all existing fields etc, but when you call this from site B and say the 'filefield.module' isn't enabled, cck will not see this, thus leaving out any uploaded properties in the node object we want to retrieve (CCK will also miss other settings, but I don't want to elaborate on this to much). Yet, we have defined this filefield in site A before. What happens now in site A is simple: we view or edit our node, but we are now retrieving the cached cck object and we see missing properties ('see' is very ironic, but you get the point right?). Frustration is wat's left if you encounter this the first time.

Oh well, luckily the French fries were very good :)

Topics 

drupal, cck, planet

Garfield minus garfield

Everybody knows the cat Garfield from the comic, they even made (horrible) movies about him. But what happens if we leave garfield out of the comic ? Quote from the site: the result is an even better comic about schizophrenia, bipolar disorder, and the empty desperation of modern life. Especially this one is hilarious: http://garfieldminusgarfield.tumblr.com/post/29944829

Topics 

humor

Extend node filters form

I've been wanting to extend the default node_filter_form on admin/content/node since ages so I finally wrote my own patch because I really needed it for some projects. The patches attached introduce a new hook called hook_node_filters. Modules can hook into the default node_filters function to add their own select boxes and types to filter on. An example hook could look like this:

<?php
/**
* Implementation of hook_node_filters().
* Returns extra filter option at admin/content/node to filter on every page.
*/
function testmodule_node_filters() {
  
$q = "SELECT n.nid, n.title FROM {node} n WHERE n.type = 'page'";
  
$result = db_query(db_rewrite_sql($q));
   while (
$row = db_fetch_object($result)) {
    
$options[$row->nid] = $row->title;
   }
   return array(
'page' => array(
      
'title' => 'page',
      
'options' => $options,
      
'where' => 'n.nid = %d',
      
'join' => '')
   );
}
?>

Although I'm pretty sure this patch is safe, this is not an official patch reviewed by the Drupal community. In the event something might go wrong, don't hold me responsible allright ? ;) Also, take a look at http://drupal.org/project/better_node_admin_content which will hopefully serve as a patch for D7.

Topics 

drupal, hooks

Goodbye sweet passwords

I just installed my own openid identity server on my own URL so I can finally login on other websites with my own openID. Now I don't have to worry about passwords anymore and I'm going to be very strict about this: any new site not supporting openID will be gladly ignored by me from now on. And the ones I like allready might get a friendly mail from me :)

Topics 

openid

Upgrade

I did an upgrade of my site to Drupal 6 this evening and so far everything looks fine. If you encounter problems, let me know! Not depending on cck & views is a major advantage even after D6 is out for almost two months now. In fact, I only have the spam module to replace - or find another solution - because there is no 6 version out yet for it. For now you always must preview your comment, I wonder (probably naively) if spambots can circumvent this ;-)

I've tested D6 a few times allready at work, but not on a production site yet. The drag & drop features and the actions/triggers module are really wonderfull and I'm sure I'll discover some more cool features in the days to come.

update 2/4 : I just installed mollom on my site, so you can now submit comments directly!

Topics 

drupal

Pages

Subscribe to realize.be RSS