ModX:创建任何资源时执行 php 代码

发布于 2024-12-24 20:58:30 字数 69 浏览 1 评论 0 原文

我正在开发一个用 ModX 创建的网站。我需要知道创建任何资源时执行 php 代码的方式,并编辑与创建的资源关联的模板变量。

I am working on a website created in ModX. I need to know the way I could execute a php code when any resource is created and also edit template variable associated to the created resource.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

善良天后 2024-12-31 20:58:31

您可以使用 插件 来执行此操作,并将其设置为在 OnDocFormSave 事件。此事件有一个 mode 属性,当刚刚创建保存的文档时,该属性将设置为 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();

You can do this using a plugin, set to run on the OnDocFormSave event. This event has a mode property which will be set to new when the document being saved has just been created - you can do a simple check for this to prevent the plugin being run every time a document is saved.

To set a TV value for the current resource, do this:

// 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();
红玫瑰 2024-12-31 20:58:31

okyanet 的答案是正确的,但我为那些不熟悉 MODX 的人添加了详细信息。

创建一个插件,并将其配置为在两个系统事件“onBeforeDocFormSave”和“onDocFormSave”上执行。这两个事件在保存资源之前和之后触发。保存资源后,$mode 变量可供您的插件使用,如果它是新资源,则其值为 modSystemEvent::MODE_NEW,如果更新其值是 modSystemEvent:MODE_UPD

我已经编写了一个要点,其中包含带有示例的完整代码:

https://gist.github.com/2768300

此示例向您展示如何通过 switch 语句定位任一事件,以及如何进一步定位“新”或“更新”状态的行动。要拦截和更改资源字段,使用 onBeforeDocFormSave 事件非常容易,因为您可以使用 $resource->set('yourFieldname') 简单地更改任何资源字段名称。不需要$resource->save(),因为这会在该事件之后自动发生。

由于电视的处理方式,使用 onBeforeDocFormSave 事件保存电视显然要困难得多,因此该示例展示了如何使用系统事件“onDocFormSave”更新电视。同样,不需要 $resource->save(),因为当您对电视调用“setValue”时,电视会立即保存。

可以使插件仅需要一个系统事件即可运行,但由于使用如上所示的两种状态都有优点,并且为了帮助解释,我使用了这两种状态。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文