1

I am creating a website that will allow users to upload projects. Project submissions will consist of a couple of text fields, a screenshot and a .zip file. Once the user uploads their project, I need to unzip the .zip file and catalog its contents. Also, it needs to be possible to upload these projects through the web interface "Add New Content" and through Node.create by way of the services module.

So my first question, is there a way to hook up a module that will be triggered after a content type is created? I built my project content using the built in Drupal 7 CCK editors but I see no way to hook up some code to this that could unzip my file and then do whatever else needs to be done.

Now if the above is not possible using CCK does that mean I need to recreate this content type by making it into its own module?

Finally, where would be the best place to put this unzipping code? I could see it might be cleaner to make a standalone module for unzipping but might it be easier to put this code directly in the content module (if I have to make one)?

1

I think the best place for your code is in a custom module, in hook_node_insert() and hook_node_update(); they're fired when a node is created and updated respectively. As the services module uses the standard node save/submission functionality these hooks will be fired even when using services.

It would make sense to keep your un-zip code in that same module.

This is a brief example just to show what I mean:

function MYMODULE_node_insert($node) {
  // Get the file ID from the node field
  $fid = $node->field_file[$node->language][0]['fid'];

  // Load the file
  $file = file_load($fid);

  // Pass the file path off to the unzip function
  $result = _my_unzip_function($file->uri);

  // Do the rest of your processing...
}
2
  • Wow thanks Clive, you seem to be answering all my questions. Just to make sure I understand you correctly it sounds to me like I should be creating this content type through a content module and not use CCK at all. Is that correct? – Mike2012 Feb 8 '12 at 18:40
  • No worries :) You can use the UI to create a content type, attach fields to it, set it up exactly the way you want it, and then use the hook system to add custom functionality to it...that's what I love about Drupal, you get the best of both worlds. I guess you could argue if this module needs to be portable to other projects then the content type should be created programatically within the module...but for a custom, one off project I think this is overkill really – Clive Feb 8 '12 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.