ModX で作成された Web サイトに取り組んでいます。リソースが作成されたときにphpコードを実行し、作成されたリソースに関連付けられたテンプレート変数を編集する方法を知る必要があります。
2 に答える
The answer by okyanet is correct, but I've added details for those unfamiliar with MODX.
Create a plugin, and configure it to execute on the two system events 'onBeforeDocFormSave' and 'onDocFormSave'. The two events are fired just before and just after a resource is saved. When a resource is saved, the $mode variable is available to your plugin, and if it is a new resource it's value is modSystemEvent::MODE_NEW and if an update its value is modSystemEvent:MODE_UPD
I've written a gist that includes the full code with examples:
https://gist.github.com/2768300
This example shows you how to target either event through a switch statement, and how to further target the 'new' or 'update' status of the action. For intercepting and changing resource fields, its quite easy using the onBeforeDocFormSave event as you can simply change any resource fieldname with $resource->set('yourFieldname'). There is no $resource->save() required as this happens automatically after this event.
It's apparently much more difficult to save a tv using the onBeforeDocFormSave event, because of the way tvs are processed, so therefore the example shows how to update a tv using the system event 'onDocFormSave'. Again there is no $resource->save() required as tvs are saved immediately when you call 'setValue' on them.
A plugin could be made to function requiring only one of the system events, but as there are advantages to using both states as shown above, and to help explain, I have used both.
OnDocFormSaveイベントで実行するように設定されたpluginを使用してこれを行うことができます。このイベントには、保存されるドキュメントが作成されたばかりのときにnewに設定されるモードプロパティがあります。ドキュメントが保存されるたびにプラグインが実行されるのを防ぐために、簡単なチェックを行うことができます。
現在のリソースの TV 値を設定するには、次のようにします。
// get the required TV object by name (or id)
$tv = $modx->getObject('modTemplateVar',array('name'=>'tvName'));
// set the new value and save it
$tv->setValue($modx->resource->get('id'), $newValue);
$tv->save();