You are here

function filefield_paths_process_string in File (Field) Paths 7

Same name and namespace in other branches
  1. 8 filefield_paths.module \filefield_paths_process_string()
  2. 5 filefield_paths.module \filefield_paths_process_string()
  3. 6 filefield_paths.module \filefield_paths_process_string()

Process and cleanup strings.

Parameters

$value:

$data:

array $settings:

Return value

mixed|string

1 call to filefield_paths_process_string()
filefield_paths_filefield_paths_process_file in modules/filefield_paths.inc
Implements hook_filefield_paths_process_file().

File

./filefield_paths.module, line 592
Contains core functions for the File (Field) Paths module.

Code

function filefield_paths_process_string($value, $data, $settings = array()) {
  $transliterate = module_exists('transliteration') && isset($settings['transliterate']) && $settings['transliterate'];
  $pathauto = module_exists('pathauto') && isset($settings['pathauto']) && $settings['pathauto'] == TRUE;
  $remove_slashes = !empty($settings['slashes']);
  if ($pathauto == TRUE) {
    module_load_include('inc', 'pathauto');
  }

  // If '/' is to be removed from tokens, token replacement need to happen after
  // splitting the paths to subdirs, otherwise tokens containing '/' will be
  // part of the final path.
  if (!$remove_slashes) {
    $value = token_replace($value, $data, array(
      'clear' => TRUE,
    ));
  }
  $paths = explode('/', $value);
  foreach ($paths as $i => &$path) {
    if ($remove_slashes) {
      $path = token_replace($path, $data, array(
        'clear' => TRUE,
      ));
    }
    if ($pathauto == TRUE) {
      if ('file_name' == $settings['context'] && count($paths) == $i + 1) {
        $pathinfo = pathinfo($path);
        $basename = drupal_basename($path);
        $extension = preg_match('/\\.[^.]+$/', $basename, $matches) ? $matches[0] : NULL;
        $pathinfo['filename'] = !is_null($extension) ? drupal_substr($basename, 0, drupal_strlen($basename) - drupal_strlen($extension)) : $basename;
        if ($remove_slashes) {
          $path = '';
          if (!empty($pathinfo['dirname']) && $pathinfo['dirname'] !== '.') {
            $path .= $pathinfo['dirname'] . '/';
          }
          $path .= $pathinfo['filename'];
          $path = pathauto_cleanstring($path);
          if (!empty($pathinfo['extension'])) {
            $path .= '.' . pathauto_cleanstring($pathinfo['extension']);
          }
          $path = str_replace('/', '', $path);
        }
        else {
          $path = str_replace($pathinfo['filename'], pathauto_cleanstring($pathinfo['filename']), $path);
        }
      }
      else {
        $path = pathauto_cleanstring($path);
      }
    }
    elseif ($remove_slashes) {
      $path = str_replace('/', '', $path);
    }

    // Transliterate string.
    if ($transliterate == TRUE) {
      $path = transliteration_clean_filename($path);
    }
  }
  $value = implode('/', $paths);

  // Ensure that there are no double-slash sequences due to empty token values.
  $value = preg_replace('/\\/+/', '/', $value);
  return $value;
}