views:

17

answers:

1

I'm building a module for Drupal 6 (what it does isn't wildly important; I've spent a long time looking for other solutions to this problem and rolling my own is definitely the simplest), and I've run into something of a conceptual road-block.

Much like the uploadpath module, I'm creating a directory based on (via a replacement pattern) properties of a node. For instance, creating a node with the "assignmentindex" contenttype called "Foobar", my module will create a folder sites/default/files/assignmentindex/Foobar.

The problem comes (predictably) during update events, when the target directory already exists, or can't be created for some other strange reason (the only one I can dream up right now is running out of inodes, but I'm sure there are other failure conditions). Obviously, to avoid losing data, the original folder needs to be left alone, but that breaks the association between the directory and the node. Thus, I see 3 different possibilities:

  • Rolling back the whole node
  • Rolling back just the changed title
  • Not rolling anything back (but showing an error)

First question is which of these is best? Rolling back the whole node wipes away a potentially huge amount of work, rolling back just the title is confusing and rolling back nothing means that the names are out of sync. My current preference is for just rolling back the title, but I'd really appreciate thoughts on this.

The second question is: how? I've already switched the relevant $op to presave (from update) so that the original node is available via

$old_node = node_load($node->id);

and to roll back a single field, can potentially just do

$node->title = $old_node-title;

but this seems sub-optimal, and I'm curious as to whether there's a better way of achieving this.

Sorry for the long question, and many thanks in advance for your help.

A: 

As an alternative to those discussed above: if you do not care about imported/programmatically created nodes, you could add a validation handler on the node form submission. Validation has access to both the new and old nodes, and can reject the submission to the user in a more standard fashion.

function custom_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'node-form') {
    $form['#validate'][] = '_custom_node_form_validate';
  }
}

function _custom_node_form_validate(&$form, &$form_state) {
  $old_node = $form['#node'];
  $new_node = $form_state['values'];
}
Grayside