Ok, I studied create.php and update.php now for a couple of hours and after some "trial and error"-fun I finaly got it
The Solution is not that simple, but a lot simpler than I thought:
1. The right event handler for this is
OnDocFormSave and not
OnBeforeDocFormSave.
OnBeforeDocFormSave works fine with new resources. But if we want to update an existing resource, there are some problems because the handling of TVs is totaly different then.
So I channged the event handler from
OnBeforeDocFormSave to
OnDocFormSave and now my code looks like this:
$custv = $modx->newObject('modTemplateVarResource');
$custv->set('tmplvarid',4); // The id of my TV
$custv->set('value',$teaserimage); // The value I want for TV4
$custv->set('contentid',$id); // The resource ID. We always have it when we use OnDocFormSave
$custv->remove(); // Just a lazy thing for the moment. It's 3am here in germany and this is the easiest way for now to prevent redundant entries
$custv->save();
That’s it! I testet it with new and existing resources.
Hope it will help some others who are looking for the same problem.
Late Greetings from germany,
Sebastian