-1

In my custom form module submit handler, I have written the following code:

function mymodule_form_submit($form, $form_state) {
db_insert('bds_user_expiry')->fields(array(
'uid' =>$user_id,
'active_user'=>strtotime("now"),
))->execute();
drupal_set_message(t('You have successfully set the expiry date'));
}
}

Here bds_user_expiry is my custom table name. When I click on submit button $user_id and the current time will be stored in my table.Now I want to define this scenario in hook_cron_queue_info() so that whenever cron runs this snippet will also run. For this first I have to define hook_cron(), issue is how can I define the above function in hook_cron,

function mymodule_form_submit($form, $form_state) {
function my_module_cron(){
$result=db_insert('bds_user_expiry')->fields(array(
'uid' =>$user_id,
'active_user'=>strtotime("now"),
))->execute();
$result=db_query($query);
$queue=DrupalQueue::get("item");
foreach($result as $data){
$queue->createItem($data);
}
}
}

Is this right? Can I define hook_cron in submit handler?

UPDATED(To check if my snippet runs with cron or not) Following code is written in my custom module file. when cron runs value of uid from bds_user_expiry will be inserted in my_cron_table just to check if my code is working with cron or not, but neither it gives me any error nor successful result.

function user_form_expiry_form_cron(){
$new=db_select('bds_user_expiry', 'b')
->fields('b',array('uid'))
->execute()->fetchassoc();
//print_r($new);
$result=db_query($new);
$queue=DrupalQueue::get("item");
foreach($result as $data){
$queue->createItem($data);
}
}
function user_form_expiry_cron_form_queue_info(){
$queues=array();
$queues['item']=array(
"worker callback"=>"myfunc",
);
return $queues;
}
function myfunc($data){
db_insert('my_cron_table')->fields(array(
'uid' =>$data,
))->execute();
}
1
  • 2
    Nesting functions in PHP serves no purpose, it's pretty much a side-effect of the parser. If you ever find yourself asking "should this function be inside another function?" for anything, the answer's almost certainly "no", unless you're talking about a closure/anonymous function
    – Clive
    Commented May 13, 2015 at 7:49

1 Answer 1

0

If you want to execute the same snippet of code, you might refactor it into a separate function, and call it from both mymodule_form_submit and mymodule_cron. Something like the following:

function _mymodule_set_expiry_date() {
  db_insert('bds_user_expiry')->fields(array(
    'uid' =>$user_id,
    'active_user'=>strtotime("now"),
  ))->execute();
  drupal_set_message(t('You have successfully set the expiry date'));
}

And then, on your hooks:

function mymodule_form_submit($form, $form_state) {
  _mymodule_set_expiry_date();
  // ... rest of your code
}

function mymodule_cron() {
  _mymodule_set_expiry_date();
  // ... rest of your code
}

You can see the following question on Drupal Answers for a more detailed explanation about hook_cron and hook_cron_queue_info: how to use hook_cron_queue_info in a custom module?

Hope that helps,

4
  • Thanku @dinopmi for your reply, I did not use hook_cron before that's why I checked if any of my custom code is working with cron or not, for this I have updated my answer, can you please point out where is my mistake?
    – nomeer
    Commented May 13, 2015 at 9:12
  • @nomeer: I think you will need to debug your calls to the db_* functions. For instance, you don't need to call db_query() after having called db_select(), you already have the results in the $new variable.
    – dinopmi
    Commented May 13, 2015 at 9:28
  • Yes that was my mistake, after fixing that mistake and ran cron, it successfully enter the value in my 2nd table as required but when I again try to run the cron it gives me the following error There are security updates available for one or more of your modules or themes. To ensure the security of your server, you should update immediately! See the available updates page for more information and to install your missing updates.
    – nomeer
    Commented May 13, 2015 at 9:51
  • When I used hardcoded value instead of $data in myfunc then it is working but with $data its not
    – nomeer
    Commented May 13, 2015 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.