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?

1 Answer 1

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';
}
2
  • 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? Commented May 12, 2016 at 14:57
  • Craft title cases (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
    Commented May 12, 2016 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.