We have a client that is wanting spaces replaced by hyphens rather than underscores, and otherwise Transliteration is perfect for the job. So having an alter hook to tie in options to rename the file name would be fantastic :)

Something like:

function transliteration_clean_filename($filename, $source_langcode = NULL) {
  if (is_array($filename)) {
    ...
  }
  $original_filename = $filename;
  $filename = transliteration_get($filename, '', $source_langcode);
  ...
  drupal_alter('transliteration_clean_filename', $filename, $original_filename, $source_langcode);
  return $filename;

Comments

alan d.’s picture

Status: Active » Needs review
StatusFileSize
new585 bytes

Patched and working example. Note, I ended up using a prepare as post-transliteration lost useful information. For example, the client used ampersands a lot, so I was replacing these with the word and.

function w2c_seo_transliteration_clean_filename_prepare_alter(&$filename, $source_langcode) {
  $filename = drupal_strtolower($filename);
  $filename = str_replace(array('& ', '& '), 'and ', $filename);
  $filename = str_replace('_', '-', $filename);
  $filename = str_replace(array('-mla copy', '-mla v2', 'gg-tvc-', '5223-', 'wc2-', '-mla-copy'), '', $filename);
  $filename = preg_replace('/\-[avx0-9]*\.([jpg|jpeg|png|gif]{1,1})/', '.$1', $filename);
}

File uploaded:
gg-Tvc-test chicken & veg pies-x3244325.jpg

File name after processing:
test-chicken-and-veg-pies.jpg

zterry95’s picture

Looks like a very good feature.

mariusz.slonina’s picture

Issue summary: View changes

Thank you!

amateescu’s picture

Status: Needs review » Needs work

The patch looks good to me as well, but we need to document it in a transliteration.api.php file :)

alan d.’s picture

Status: Needs work » Needs review
StatusFileSize
new1.34 KB

Added ;)

amateescu’s picture

Version: 7.x-3.x-dev » 6.x-3.x-dev
Status: Needs review » Patch (to be ported)

Committed and pushed to 7.x-3.x. Thanks!

Marking for backport to the 6.x branch.