1
craft()->on('entries.saveEntry', function(Event $event)
{
    // entry slug, will be used as image name
    $EntryTitle = $event->params['entry']['slug'];

    // Get attached image
    $entry = $event->params['entry']['imgdir'][0];

    craft()->assets->renameFile($entry, $EntryTitle);
    $entry->setAttribute('filename', $EntryTitle);
    craft()->assets->storeFile($entry);

});

This init() code gets the current entry title right after it was saved, and is supposed to use it to rename an attached file. I used a method given by another developer in the context of a saveAsset, but it doesn't seem to work in this new context (→ see Change asset name on upload).

I'm not sure i've set the $entry variable to correct data. Fact is I'm able trough the path you see above, to get access to the asset. Please tell me where this code is wrong.

Benj
  • 47
  • 3

2 Answers2

3

You probably just confused the entry model and the asset model, see if the code below works for you. I guess the entries.onBeforeSaveEntry event is a better match in this case, and I also added some conditionals to only do something for a specific entry section and type, and if there's exactly one asset selected. I hope this helps (code is untested)!

craft()->on('entries.beforeSaveEntry', function(Event $event)
{
    $entry = $event->params['entry'];

    if (($entry->section['handle'] == 'friends') && ($entry->type['handle'] == 'friends'))
    {
        $title = $entry->slug;
        $assets = $entry->imgdir;

        if (count($assets) != 1)
        {
            $event->params['entry']->addError('imgdir', Craft::t('Exactly 1 asset has to be selected.'));
            $event->performAction = false;
        }
        else
        {
            $filename = $title.'.'.$assets[0]->getExtension();

            craft()->assets->renameFile($assets[0], $filename);
            craft()->assets->storeFile($assets[0]);
        }
    }
});
carlcs
  • 36,220
  • 5
  • 62
  • 139
  • Thanks for your answer. I tried your code but it does not change the image filename, it's as if it does nothing particular. – Benj Jan 13 '16 at 13:31
  • 1
    You forgot to add the file extension to the new title, as suggested in mmikkel's original answer and so did I, corrected it in the answer above. – carlcs Jan 13 '16 at 14:50
  • Does this work with Amazon S3 too? My application is a bit different. Immediately after upload, I need to push the mp3 file to Soundcloud using their API. I have experience with their API. I just don't know how I access the file to send to them. – David A McInnis Apr 12 '16 at 14:28
  • @david yes, you can use the asset events to do your things with an external API. Probably worth adding a new question for this though. – carlcs Apr 12 '16 at 15:11
  • What would be the equivalent in Craft 3? https://craftcms.stackexchange.com/a/27604/298 works, but I want the new file name to be a concatenation of Post Date (or upload time stamp) & the entry's author id. – Andrea DeMers Oct 21 '18 at 23:33
1

In Craft 3, here's a version that worked for me to change the file name after upload (in a module's init() function):

Event::on(
Elements::class,
Elements::EVENT_AFTER_SAVE_ELEMENT,
    function (ElementEvent $event) {
        $asset = $event->element;
        if ($event->isNew && $asset instanceof craft\elements\Asset) {
             $newFileName = "something.txt";
              Craft::$app->assets->moveAsset($asset,$asset->getFolder(),filter_var($asset->getFilename(), $newFileName);
        }
     }
);
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Udo
  • 607
  • 4
  • 9