Use "presave" instead of insert/update

quicksketch - October 12, 2008 - 12:12
Project:Upload path
Version:6.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:needs review
Description

Switching uploadpath.module to using presave allows more flexibility when dealing with uploads. It can't quite fix #154971: wrong url in "File attachments", but at least it makes it possible to work around. Like others, I have the trouble where the initial upload path is wrong (causing all kinds of dead images when posting them into the body) until the node is updated. Because the insert/update actions take place after the "base" node has already been saved, it's impossible to update the body or teaser fields without doing a second SQL query to update these fields.

Switching to "presave" makes it so that the new uploaded file path is available to other modules. Making it possible (in my particular case) to update all the URLs that reference these files in the body and teaser. Tested and this patch has no direct effect on the functionality of the module.

AttachmentSize
uploadpath_presave.patch733 bytes

#1

quicksketch - October 12, 2008 - 12:20

Here's an example of a hook_nodeapi() that updates links in the body and teaser, after the uploadpath.module has been switched to presave. Note that this module must have a heavier weight than uploadpath.module to work.

<?php
/**
* Implementation of hook_nodeapi().
*/
function mycustommodule_nodeapi(&$node, $op, $teaser, $page) {
 
// Upload path module changes the path of uploaded files, yet these files
  // may have been inserted into the body. Update all the file paths.
 
if ($op == 'presave' && isset($node->files) && user_access('upload files')) {
    foreach (
$node->files as $file) {
      if (
$file['new'] && !$file['remove']) {
       
$original_path = file_directory_path() .'/'. $file['filename'];
       
$moved_path = file_directory_path() .'/'. $file['filepath'];
       
$node->body = str_replace($original_path, $moved_path, $node->body);
       
$node->teaser = str_replace($original_path, $moved_path, $node->teaser);
      }
    }
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.