Skip to content
Snippets Groups Projects

Update file filefield_paths.module

1 file
+ 78
64
Compare changes
  • Side-by-side
  • Inline
+ 78
64
@@ -11,6 +11,8 @@ use Drupal\Core\Entity\EntityTypeInterface;
@@ -11,6 +11,8 @@ use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\Language;
 
use Drupal\Core\Render\BubbleableMetadata;
 
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Url;
use Drupal\Core\Url;
use Drupal\field\FieldConfigInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
@@ -340,72 +342,84 @@ function _filefield_paths_create_redirect($source, $path, Language $language) {
@@ -340,72 +342,84 @@ function _filefield_paths_create_redirect($source, $path, Language $language) {
* The cleaned string, in which tokens are replaced and other alterations may
* The cleaned string, in which tokens are replaced and other alterations may
* have been applied, depending on the settings.
* have been applied, depending on the settings.
*/
*/
function filefield_paths_process_string($value, array $data, array $settings = []) {
function filefield_paths_process_string($value, $data, array $settings = []) {
$transliterate = $settings['transliterate'];
$context = new RenderContext();
$pathauto = \Drupal::moduleHandler()
/* @var \Drupal\Core\Cache\CacheableDependencyInterface $result */
->moduleExists('pathauto') && isset($settings['pathauto']) && $settings['pathauto'] == TRUE;
$result = \Drupal::service('renderer')->executeInRenderContext($context, function() use($value, $data, $settings) {
$remove_slashes = !empty($settings['slashes']);
$transliterate = $settings['transliterate'];
$pathauto = \Drupal::moduleHandler()
// If '/' is to be removed from tokens, token replacement need to happen after
->moduleExists('pathauto') && isset($settings['pathauto']) && $settings['pathauto'] == TRUE;
// splitting the paths to subdirectories, otherwise tokens containing '/' will
$remove_slashes = !empty($settings['slashes']);
// be part of the final path.
if (!$remove_slashes) {
// If '/' is to be removed from tokens, token replacement need to happen after
$value = \Drupal::service('token')
// splitting the paths to subdirs, otherwise tokens containing '/' will be
->replace($value, $data, ['clear' => TRUE]);
// part of the final path.
}
if (!$remove_slashes) {
$paths = explode('/', $value);
$value = \Drupal::service('token')
->replace($value, $data, ['clear' => TRUE]);
foreach ($paths as $i => &$path) {
}
if ($remove_slashes) {
$paths = explode('/', $value);
$path = \Drupal::service('token')
->replace($path, $data, ['clear' => TRUE]);
foreach ($paths as $i => &$path) {
}
if ($remove_slashes) {
if ($pathauto == TRUE) {
$path = \Drupal::service('token')
if ('file_name' == $settings['context'] && count($paths) == $i + 1) {
->replace($path, $data, ['clear' => TRUE]);
$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 {
if ($pathauto == TRUE) {
$path = \Drupal::service('pathauto.alias_cleaner')->cleanstring($path);
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) ? Unicode::substr($basename, 0, Unicode::strlen($basename) - Unicode::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);
}
}
}
}
elseif ($remove_slashes) {
$value = implode('/', $paths);
$path = str_replace('/', '', $path);
}
// Ensure that there are no double-slash sequences due to empty token values.
$value = preg_replace('/\/+/', '/', $value);
// Transliterate string.
if ($transliterate == TRUE) {
return $value;
$path = \Drupal::service('transliteration')->transliterate($path);
});
}
// Handle any bubbled cacheability metadata.
}
if (!$context->isEmpty()) {
$value = implode('/', $paths);
$bubbleable_metadata = $context->pop();
BubbleableMetadata::createFromObject($result)
// Ensure that there are no double-slash sequences due to empty token values.
->merge($bubbleable_metadata);
$value = preg_replace('/\/+/', '/', $value);
}
return $value;
return $result;
}
}
/**
/**
* Get the recommended file scheme based on which file systems are enabled.
* Get the recommended file scheme based on which file systems are enabled.
Loading