开发者

Drupal hook to change date CCK field value

开发者 https://www.devze.com 2023-03-06 03:25 出处:网络
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\'

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'开发者_C百科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?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号