You are here

function file_aliases_filefield_paths_process_file in File Aliases 6

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

Implements hook_filefield_paths_process_file().

File

./file_aliases.module, line 67
Contains core functions for the File Aliases module.

Code

function file_aliases_filefield_paths_process_file($new, $file, $settings, $node, $update) {
  if ($new) {
    $file['filealias'] = filefield_paths_process_string($settings['filealias']['value'], 'node', $node, $settings['filealias']);
    $file['filealias'] = filefield_paths_process_string($file['filealias'], 'field', array(
      0 => $file['field'],
    ), $settings['filealias']);
    $path = 'filefield_paths/alias/' . $file['field']['fid'];
    $alias = drupal_get_path_alias($path);

    // Path already has an alias.
    if ($alias != $path) {
      path_set_alias(NULL, $alias);
    }

    // Rename alias if already in use.
    if (drupal_lookup_path('source', $file['filealias']) != NULL) {
      $info = pathinfo($file['filealias']);

      // PHP < 5.2: pathinfo() doesn't return 'filename' variable.
      $info['filename'] = isset($info['filename']) ? $info['filename'] : basename($file['filealias'], '.' . $info['extension']);
      $counter = 0;
      do {
        $file['filealias'] = $info['dirname'] . '/' . $info['filename'] . '_' . $counter++ . '.' . $info['extension'];
      } while (drupal_lookup_path('source', $file['filealias']) != NULL);
    }

    // Create Alias.
    path_set_alias('filefield_paths/alias/' . $file['field']['fid'], $file['filealias']);

    // Display Alias in Node Body and CCK Text fields.
    if ($settings['filealias']['display']) {

      // Regular expression to replace file reference with file alias.
      $pattern = array(
        'regex' => str_replace('/', '\\/', $file['filepath']['new']),
        'regex_enc' => str_replace('/', '\\/', drupal_urlencode($file['filepath']['new'])),
        'replace' => $file['filealias'],
      );

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