You are here

function filefield_paths_filefield_paths_process_file in File (Field) Paths 6

Same name and namespace in other branches
  1. 8 filefield_paths.inc \filefield_paths_filefield_paths_process_file()
  2. 5 filefield_paths.module \filefield_paths_filefield_paths_process_file()
  3. 7 modules/filefield_paths.inc \filefield_paths_filefield_paths_process_file()

Implements hook_filefield_paths_process_file().

File

./filefield_paths.module, line 488
Contains core functions for the FileField Paths module.

Code

function filefield_paths_filefield_paths_process_file($new, &$file, $settings, &$node, &$update) {
  if ($new && !empty($file['field']['filename'])) {

    // 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.
        if (isset($node->body) || module_exists('content')) {
          $file['filepath']['new'] = str_replace($file['filename']['old'], $file['filename']['new'], $file['filepath']['new']);
          $file_directory_path = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC ? file_directory_path() : 'system/files';

          // Regular expression to replace old file reference.
          $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']),
          );

          // Process regular expression.
          _filefield_paths_replace_pattern($pattern, $node, $update);
        }

        // Store new filename in file Array
        $file['field']['filename'] = $file['filename']['new'];
      }
    }
  }
}