用于更改日期 CCK 字段值的 Drupal 挂钩
我需要在日历活动的前一天以及后一天发送提醒电子邮件。不幸的是,我无法使用规则调度程序,因为无法使用 PHP 操作令牌。 去的话,那是不行的
[node:field_event_date-datetime] -1 day
如果我按照预定的时间
。我最终所做的是为“前一天”和“后天”创建两个“虚拟”日期字段,并且我尝试连接到表单,获取事件日期,使用一些 PHP(如 strtotime())来添加/减去日,并将这些值设为将进入数据库的值。
我尝试链接到表单的 #submit 部分,但在 phpMyAdmin 中,所有值均为 NULL。 对于这段代码,我什至没有更改日期,我只是试图让值出现在数据库中。
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "event_node_form") {
$form['#submit'][] = '_mymodule_after_build';
// Makes the fields invisible
$form["field_event_day_before"]["#access"] = FALSE;
$form["field_event_day_after"]["#access"] = FALSE;
}
}
function _mymodule_after_build($form, &$form_state) {
$eventcopy = array();
// copy the value part from the Event
$eventcopy = $form['field_event_date'][0]['#value'];
// without doing any PHP yet, simply copy the values. Doesn't show up in db.
$form['field_event_day_before'][0]['#value'] = $eventcopy;
dsm($form);
return $form;
}
我已阅读有关使用 Rules Scheduler with CCK 的教程,并且
我也在关注 根据 CCK 日期字段安排电子邮件发送 - 不适合我
Am我使用了正确的钩子吗?如何正确截取输入的日期值?
I need to send out a reminder email the DAY BEFORE a Calendar Event as well as the DAY AFTER. Unfortunately, I can't use Rules Scheduler, because the Tokens can't be manipulated with PHP. It doesn't work if I have
[node:field_event_date-datetime] -1 day
as the scheduled time.
What I've ended up doing is creating two "dummy" date fields for DAY BEFORE and DAY AFTER, and I'm trying to hook into the form, grabbing the event date, using some PHP like strtotime() to add/subtract a day, and make these the values that would go into the database.
I've tried linking to the #submit part of the form, but in phpMyAdmin, all values are NULL.
For this code i haven't even changed the date, I'm just trying to get values to appear in the database.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "event_node_form") {
$form['#submit'][] = '_mymodule_after_build';
// Makes the fields invisible
$form["field_event_day_before"]["#access"] = FALSE;
$form["field_event_day_after"]["#access"] = FALSE;
}
}
function _mymodule_after_build($form, &$form_state) {
$eventcopy = array();
// copy the value part from the Event
$eventcopy = $form['field_event_date'][0]['#value'];
// without doing any PHP yet, simply copy the values. Doesn't show up in db.
$form['field_event_day_before'][0]['#value'] = $eventcopy;
dsm($form);
return $form;
}
I've read the tutorials about using Rules Scheduler with CCK and
I'm also following Schedule email to go out based on CCK date field - not working for me
Am I using the right hooks? How do I intercept the inputted date value properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您没有以正确的方式处理问题。如果您想尝试沿着您建议的路径走下去,那么您需要查看 hook_nodeapi()。您可以为“插入”和/或“保存”(甚至可能是“预保存”)操作添加一些代码,以便可以更新您的
$node->field_event_day_before'][0]['#value']
和$node->field_event_day_after'][0]['#value']
字段基于 event_date 值。但是,您确实不想为之前日期和之后日期添加额外的字段,因为您只需从 event_date 中计算这些字段即可。
我认为更好的解决方案就是实现hook_cron() 并让该函数处理查询数据库中事件日期为 TODAY() +1 的所有事件。对于所有这些结果,请发送电子邮件。执行另一个查询,查找 event_date 为 TODAY() - 1 的任何事件,并发送一封电子邮件以获取这些结果。您需要确保每 24 小时仅运行一次此过程。
I don't think you are approaching your problem the correct way. If you want to try to go down the path you are proposing then you would want to look at hook_nodeapi(). You can add some code for the 'insert' and/or 'save' (or maybe even 'presave') operations so you can update your
$node->field_event_day_before'][0]['#value']
and$node->field_event_day_after'][0]['#value']
fields based on the event_date value.However, you really don't want to add extra fields for date before and date after when you can just calculate those from the event_date.
What I think the better solution is to just implement hook_cron() and have that function handle querying for all events in your database whose event day is TODAY() +1. For all those results, send out an email. Do another query that looks for any event whose event_date is TODAY() - 1 and send out an email for those results. You'll want to make sure you only run this process once in every 24 hour period.
感谢社区的帮助,我想分享答案。如果您遇到同样的问题,请尝试以下操作:
规则调度程序可以从 day_before/day_after 字段中获取令牌,并且您可以使用其接口进行调度。
I want to share the answer, thanks to help from the community. If you run into this same problem, try this:
The rules scheduler can then take tokens from the day_before/day_after fields, and you can use their interface for scheduling.
您可以通过使用规则模块来做到这一点,我在我的一个项目中做到了这一点,基本上您必须创建两个规则,一个用于前一天,另一个用于后一天。如果您需要任何说明,请告诉我。
谢谢
K
You can do this by using the rules module, i did this in my one project, basically you have to create two rules, one for one day before, and another for one day after. Let me know if you want any clarification.
Thanks
K