Hi,

I'm attempting to be able to serialize a node, along with any filefield / imagefields attached to the content type. This is similar to node cloning, but the nodes don't really exist. My thinking is this:

  1. Enumerate the filefields attached to the node form in hook_form_alter.
  2. Add a #submit[] parameter to each field with my own custom function, which takes the uploaded files and stores them appropriately.
  3. When the nodes are unserialized, copy the appropriate files from the "cache" and fill in the filefields behind the scenes.

However, I can't seem to get my #submit function to be called. Looking through the filefield code, I don't see an easy way to override how it handles saving a file in some other "CCK-blessed" way.

Here's the snippet of what I'm trying to do:

  $content_type = content_types($node['type']);
  $image_fields = array();
  // Find all the image fields
  foreach ($content_type['fields'] as $data) {
    if ($data['type'] == 'filefield') {
       $file_fields[] = $data['field_name'];
    }
  }
  foreach($file_fields as $key) {
    $form[$key]['#submit'][] = '_skeleton_test_submit';
  }
function _skeleton_test_submit($form, &$form_state) {
  // My submit handler goes here and unsets the field from $form_state when done.
}

Any suggestions? Or am I just asking for filefield to blow up in my face? :)

Thanks!

Comments

quicksketch’s picture

The primary problem here is that #submit only works two things: the form itself (#type = 'form') and on buttons (#type = 'submit'). Putting submit properties on elements themselves won't have any effect.

I'm not sure when you intend this serialization to take place (or even why you're wanting it to take place). You might look at how the node_form_submit_build_node() function works, which could be helpful in generating a pseudo-node based off the current form state.

deviantintegral’s picture

Ah, that would do it. So I'd have to bind to the 'upload' and 'remove' buttons instead.

The background is enabling filefield to work with the Skeleton Outlines module. It lets you create an outline of pages for a book, and then copy them into nodes to start as a template for a new book. It doesn't create actual nodes for templates; instead it hijacks the node form and serializes the values. When you create a copy of an outline, it unserializes the node and runs it through drupal_execute(). This works great for other CCK fields, but fails quite horribly on filefield (and core's upload too).

I will look at the function you mentioned and see if it helps. Thanks!

quicksketch’s picture

Status: Active » Fixed

Marking fixed, since I think the question about the #submit property should be answered.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.