4

Is there any way to control the formatting of an asset's title that is created from the filename on upload?

At the moment, if you uploaded a file called Craft-CMS.pdf it would create the title as Craft Cms - where in most cases I you would expect the uppercase of CMS to remain intact.

I see there is a config setting for filenameWordSeparator, but nothing related to the pattern used to created the title - is there anyway I'm not seeing to configure this?

michaelroper
  • 303
  • 2
  • 5

1 Answers1

4

For Craft 2, You can write a plugin that listens to the onBeforeSaveAsset event, and changes the title from the asset parameter to whatever you want. Something like:

if ($event->params['isNewAsset])
{
    $asset = $event->params['asset']
    $asset->getContent()->title = 'My New Title';
}

For Craft 3, you can write a plugin that listens to the BEFORE_HANDLE_FILE event like so:

Event::on(Asset::class, Asset::EVENT_BEFORE_HANDLE_FILE, function(AssetEvent $event) {
    $asset = $event->asset;
    $asset->title = 'My New Title';
}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • Thanks Brad, will look into doing that if I need to! Out of interest, any reason why the default behavior changes the case instead of just replacing separators with spaces? – michaelroper May 12 '16 at 14:57
  • Craft title cases (https://en.wikipedia.org/wiki/Letter_case#Case_styles) the file name. Just so happens that doesn't play well with acronyms like "CMS" – Brad Bell May 12 '16 at 17:22