We have to update the Asset after the SAVE_ON event, and the


Not seems to be working (it is getting timed out). So what is the best pratice to update the Asset after the save, which event should we use for example auto-tagging?

If we are trying to save the entry, it is working like charm, maybe we should query the asset parent first?

Update some additional findings: It's turned out it is not so easy to re-save the assets after the SAVE_ON. We need to add additional parameters - based on the class rules:

$rules[] = [['newLocation'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_FILEOPS]];
$rules[] = [['tempFilePath'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_REPLACE]];

Obviously the goal is to just add some tags/category to the image (eg: getting back the tags data from cloudanary auto tagging interface via the filesystem, so we are not able to use the before save event). So if we are simply trying to save the $asset it is not going to work, because we do not have newLocation either the tempFilePath is old as well. Instead we have to change the scenario, and save the asset after that (the idea was coming after checking the ElementController)

$element->setFieldValue('fieldHandle', $ids);

I do not have better idea for Asset save (specially because the asset itself was saved, and only the fields has to be updated), but this solution is working like charm, and the same logic is applied on the CP. If only the fields are getting updated the ElementController is getting called otherwise the AssetController. Hopes it helps for future reference

  • Looking back at the revision history of this question, it appears that you drastically changed the scope and meaning of this question on Mar 6 at 23:02. Please do not edit a question so drastically once it's been posted, as it may invalidate existing answers. If you need to ask a new question based on recent information, please post an entirely new question. You can link back to your previous question for reference.
    – Lindsey D
    Commented Mar 11, 2018 at 21:03
  • Noted, but it was the same issue. Only our current understanding along craft has been changed and notnthe problem Commented Mar 12, 2018 at 17:35
  • Understood. The revision was still significant enough to change the meaning of the question. In the future, please consider opening a new question based on learned information.
    – Lindsey D
    Commented Mar 13, 2018 at 18:55
  • As i said noted and i do my best next time! Appreciate your feedback! Commented Mar 14, 2018 at 19:41
  • Great, thank you!
    – Lindsey D
    Commented Mar 14, 2018 at 19:41

1 Answer 1


A Tag field is like every other relation field. Just insert an array of ids in the field and save the element

$element->setFieldValue('fieldHandle', [1,2,3,4,5,6]);

In order to get the current ids and add one, just do

$ids = $element->getFieldValue('fieldHandle')->ids();
$ids[] = 'yourNewTagId';
$element->setFieldValue('fieldHandle', $ids);

To Create a new Tag do

$tag = new Tag();
$tag->title = "your new tag name";
$tag->groupId = 1;

// and add it to the element
$element = Craft::$app->getElements()->getElementById(2);
$ids = $element->getFieldValue('fieldHandle')->ids();
$ids[] = $tag->id;
$element->setFieldValue('fieldHandle', $ids);


Note that I'm using the setFieldValue directly instead of the magic __get and __set methods for a better performance.

  • Strange, we have been tried this, and it was not working, Will doing the troubleshooting again, thx, will come back Commented Mar 6, 2018 at 22:22
  • What does not work? You know you can receive all errors with $element->getErrors() in order to see what is not correct Commented Mar 6, 2018 at 22:41
  • We are trying to save the asset on the SAVE_ON event. But, even if we are not doing anything just simply using: Craft::$app->getElements()->saveElement($asset); it is going for a timeout, and we have to restart the webserver If we are trying to save the Entry it self, it is working like charm, but in case of the SAVE_ON event it is failing... (we would like to do the auto tagging at the SAVE_ON after the SAVE_ON event) Commented Mar 6, 2018 at 22:57
  • Well of course. The saveElement function triggers the OnBeforeSave event, and you call the saveElement..... That triggers the event again.... You see? You are producing an endless loop. You don't need to call the saveElement function since this function is going to run when you use the BeforeElementSave event Commented Mar 6, 2018 at 23:05
  • Gosh... you right... thank you!!! We are checking it! Commented Mar 7, 2018 at 0:15

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.