You are here

function file_aliases_filefield_paths_process_file in File Aliases 7

Same name and namespace in other branches
  1. 5 file_aliases.module \file_aliases_filefield_paths_process_file()
  2. 6 file_aliases.module \file_aliases_filefield_paths_process_file()

Implements hook_filefield_paths_process_file().

Parameters

$type:

$entity:

$field:

$instance:

$langcode:

$items:

File

modules/filefield_paths.inc, line 43
FileField Paths module integration.

Code

function file_aliases_filefield_paths_process_file($type, $entity, $field, $instance, $langcode, &$items) {
  if (isset($instance['settings']['filefield_paths'])) {
    $settings = $instance['settings']['filefield_paths'];
    if (isset($settings['file_alias'])) {
      foreach ($items as &$file) {

        // Process file if this is a new entity, 'Active updating' is set or
        // file wasn't previously attached to the entity.
        if (isset($entity->original) && empty($settings['active_updating']) && !empty($entity->original->{$field['field_name']}[$langcode])) {
          foreach ($entity->original->{$field['field_name']}[$langcode] as $original_file) {
            if ($original_file['fid'] == $file['fid']) {
              continue 2;
            }
          }
        }
        $token_data = array(
          'file' => (object) $file,
          $type => $entity,
        );

        // Process file alias.
        // This is a bit hacky, the context should be 'file_alias', but to
        // ensure the file name doesn't have it's '.' removed we need to
        // identify as the file name.
        $settings['file_alias']['options']['context'] = 'file_name';
        $file['alias'] = filefield_paths_process_string($settings['file_alias']['value'], $token_data, $settings['file_alias']['options']);

        // Convert the file alias into an absolute URL and ensure that it begins
        // with the current base URL and path.
        $url = url($file['alias'], array(
          'absolute' => TRUE,
        ));
        $base = $GLOBALS['base_url'] . base_path();
        if (variable_get('clean_url', 0) == FALSE) {
          $base .= '?q=';
        }
        if (substr($url, 0, strlen($base)) !== $base) {
          watchdog('file_aliases', 'The file alias %alias could not be created for %destination as it is invalid.', array(
            '%alias' => $file['alias'],
            '%destination' => $file['uri'],
          ));
          continue;
        }

        // Remove the current base URL and path to normalise the file alias.
        $file['alias'] = substr($url, strlen($base));
        $source = "file_aliases/{$file['fid']}";
        $alias = drupal_get_path_alias($source);
        if ($alias !== $source) {
          path_delete(array(
            'source' => $source,
          ));
        }
        $path = array(
          'source' => $source,
          'alias' => $file['alias'],
        );
        path_save($path);
      }
    }
  }
}