You are here

function filefield_paths_process_string in File (Field) Paths 8

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

Process and cleanup strings.

Parameters

string $value: Todo.

string $data: Todo.

array $settings: Todo.

Return value

string Todo.

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

File

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

Code

function filefield_paths_process_string($value, $data, array $settings = []) {
  $transliterate = $settings['transliterate'];
  $pathauto = \Drupal::moduleHandler()
    ->moduleExists('pathauto') && isset($settings['pathauto']) && $settings['pathauto'] == TRUE;
  $remove_slashes = !empty($settings['slashes']);

  // 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 = \Drupal::service('token')
      ->replace($value, $data, [
      'clear' => TRUE,
    ]);
  }
  $paths = explode('/', $value);
  foreach ($paths as $i => &$path) {
    if ($remove_slashes) {
      $path = \Drupal::service('token')
        ->replace($path, $data, [
        'clear' => TRUE,
      ]);
    }
    if ($pathauto == TRUE) {
      if ('file_name' == $settings['context'] && count($paths) == $i + 1) {
        $pathinfo = pathinfo($path);
        $basename = \Drupal::service('file_system')
          ->basename($path);
        $extension = preg_match('/\\.[^.]+$/', $basename, $matches) ? $matches[0] : NULL;
        $pathinfo['filename'] = !is_null($extension) ? mb_substr($basename, 0, mb_strlen($basename) - mb_strlen($extension)) : $basename;
        if ($remove_slashes) {
          $path = '';
          if (!empty($pathinfo['dirname']) && $pathinfo['dirname'] !== '.') {
            $path .= $pathinfo['dirname'] . '/';
          }
          $path .= $pathinfo['filename'];
          $path = \Drupal::service('pathauto.alias_cleaner')
            ->cleanstring($path);
          if (!empty($pathinfo['extension'])) {
            $path .= '.' . \Drupal::service('pathauto.alias_cleaner')
              ->cleanstring($pathinfo['extension']);
          }
          $path = str_replace('/', '', $path);
        }
        else {
          $path = str_replace($pathinfo['filename'], \Drupal::service('pathauto.alias_cleaner')
            ->cleanstring($pathinfo['filename']), $path);
        }
      }
      else {
        $path = \Drupal::service('pathauto.alias_cleaner')
          ->cleanstring($path);
      }
    }
    elseif ($remove_slashes) {
      $path = str_replace('/', '', $path);
    }

    // Transliterate string.
    if ($transliterate == TRUE) {
      $path = \Drupal::service('transliteration')
        ->transliterate($path);
    }
  }
  $value = implode('/', $paths);

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