/** * Implementation of hook_filefield_paths_process_file(). */ function filefield_paths_filefield_paths_process_file($new, &$file, $settings, &$node, &$update) { if ($new) { // Process filename $file['filename']['old'] = $file['field']['filename']; if (($file['filename']['new'] = $settings['filename']['value']) != '') { $file['filename']['new'] = filefield_paths_process_string($file['filename']['new'], 'node', $node, $settings['filename']); $file['filename']['new'] = filefield_paths_process_string($file['filename']['new'], 'field', array(0 => $file['field']), $settings['filename']); } else { $file['filename']['new'] = $file['field']['filename']; } // Process filepath $file['filepath']['old'] = $file['field']['filepath']; $file['filepath']['new'] = filefield_paths_process_string(file_directory_path() .'/'. $settings['filepath']['value'] .'/'. $file['filename']['new'], 'node', $node, $settings['filepath']); $file['filepath']['new'] = filefield_paths_process_string($file['filepath']['new'], 'field', array(0 => $file['field']), $settings['filepath']); // Finalize files if necessary if (dirname($file['filepath']['new']) != dirname($file['field']['filepath']) || $file['filename']['new'] != $file['field']['filename']) { if (filefield_paths_file_move($file)) { // Fix reference to old paths in Body and Teaser and CCK $file['filepath']['new'] = str_replace($file['filename']['old'], $file['filename']['new'], $file['filepath']['new']); $pattern = array( 'regex' => str_replace('/', '\/', file_directory_path()) .'(.*?)'. str_replace('/', '\/', str_replace(file_directory_path(), '', $file['filepath']['old'])), 'regex_enc' => str_replace('/', '\/', drupal_urlencode(file_directory_path())) .'(.*?)'. str_replace('/', '\/', str_replace(drupal_urlencode(file_directory_path()), '', drupal_urlencode($file['filepath']['old']))), 'replace' => file_directory_path() .'$1'. str_replace(file_directory_path(), '', $file['filepath']['new']), ); // if module cck is activated if (module_exists('content')) { // loop through the field of this content type and check if field is set this node $fields = content_fields(NULL, $node->type); foreach($fields as $fieldname => $field){ // handle only the field text (perhaps other filed type need this too ?) if($field['type']=='text' && !empty($node->{$fieldname}[0]['value'])){ // handle multiple values (if no multiple, only one loop with key=0) foreach($node->{$fieldname} as $key => $value){ // do the preg_replace only if value is set if(isset($node->{$fieldname}[$key]['value'])){ $tmp = $node->{$fieldname}[$key]['value']; $tmp = preg_replace('/'. $pattern['regex'] .'/s', $pattern['replace'], $tmp); $tmp = preg_replace('/'. $pattern['regex_enc'] .'/s', $pattern['replace'], $tmp); if ($tmp != $node->{$fieldname}[$key]['value']) { $node->{$fieldname}[$key]['value'] = $tmp; $update->node = TRUE; } } } } } } if (isset($node->body)) { $body = $node->body; $body = preg_replace('/'. $pattern['regex'] .'/s', $pattern['replace'], $body); $body = preg_replace('/'. $pattern['regex_enc'] .'/s', $pattern['replace'], $body); $teaser = $node->teaser; $teaser = preg_replace('/'. $pattern['regex'] .'/s', $pattern['replace'], $teaser); $teaser = preg_replace('/'. $pattern['regex_enc'] .'/s', $pattern['replace'], $teaser); if ($body != $node->body || $teaser != $node->teaser) { $node->body = $body; $node->teaser = $teaser; $update->node = TRUE; } } // Store new filename in file Array $file['field']['filename'] = $file['filename']['new']; } } } }