用于更改日期 CCK 字段值的 Drupal 挂钩

发布于 2024-11-07 04:18:27 字数 1336 浏览 4 评论 0原文

我需要在日历活动的前一天以及后一天发送提醒电子邮件。不幸的是,我无法使用规则调度程序,因为无法使用 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 技术交流群。

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

发布评论

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

评论(3

回眸一笑 2024-11-14 04:18:27

我认为您没有以正确的方式处理问题。如果您想尝试沿着您建议的路径走下去,那么您需要查看 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.

ˉ厌 2024-11-14 04:18:27

感谢社区的帮助,我想分享答案。如果您遇到同样的问题,请尝试以下操作:

function mymodule_form_event_node_form_alter(&$form, &$form_state) {
    // hide these dummy fields, will fill in programatically
    $form["field_event_day_before"]["#access"] = FALSE;
    $form["field_event_day_after"]["#access"] = FALSE;
}

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4  = NULL){
    switch ($op) {
    //if the node is inserted in the database
    case 'insert':
        if($node->type == 'event') {

        // Day before (+10 hours because I'm in Hawai`i, far from GMT)
        $daybefore = strtotime('-1 day +10 hours', strtotime($node->field_event_date[0]['value']));
        $daybefore = date('Y-m-j\TH:i:s', $daybefore);
        $node->field_event_day_before[0]['value'] = $daybefore;

        // Day after (+10 hours because I'm in Hawai`i)
        $dayafter = strtotime('+1 day +10 hours', strtotime($node->field_event_date[0]['value']));
        $dayafter = date('Y-m-j\TH:i:s', $dayafter);
        $node->field_event_day_after[0]['value'] = $dayafter;
        }
    }
}

规则调度程序可以从 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:

function mymodule_form_event_node_form_alter(&$form, &$form_state) {
    // hide these dummy fields, will fill in programatically
    $form["field_event_day_before"]["#access"] = FALSE;
    $form["field_event_day_after"]["#access"] = FALSE;
}

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4  = NULL){
    switch ($op) {
    //if the node is inserted in the database
    case 'insert':
        if($node->type == 'event') {

        // Day before (+10 hours because I'm in Hawai`i, far from GMT)
        $daybefore = strtotime('-1 day +10 hours', strtotime($node->field_event_date[0]['value']));
        $daybefore = date('Y-m-j\TH:i:s', $daybefore);
        $node->field_event_day_before[0]['value'] = $daybefore;

        // Day after (+10 hours because I'm in Hawai`i)
        $dayafter = strtotime('+1 day +10 hours', strtotime($node->field_event_date[0]['value']));
        $dayafter = date('Y-m-j\TH:i:s', $dayafter);
        $node->field_event_day_after[0]['value'] = $dayafter;
        }
    }
}

The rules scheduler can then take tokens from the day_before/day_after fields, and you can use their interface for scheduling.

谜泪 2024-11-14 04:18:27

您可以通过使用规则模块来做到这一点,我在我的一个项目中做到了这一点,基本上您必须创建两个规则,一个用于前一天,另一个用于后一天。如果您需要任何说明,请告诉我。

谢谢
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

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