1

I'm a bit new to drupal and I have a task, where I have to create a node when cron runs. I set up the node like this in my custom module's cron hook:

$node = new stdClass();
$node->type = 'reports';
$node->language = LANGUAGE_NONE;
$node->uid = 1;
$node->status = 1;
$node->promote = 0;
$node->comment = 0;
$node->sticky = 0;
$node->moderate = 0;
$node->title = 'Daily Report for ' . date('d.m.Y');
$node->field_reporting_cycle[LANGUAGE_NONE][0]['value'] = 1;
$fid = generate_periodic_report(1);
$node->field_period_of_reporting[LANGUAGE_NONE][0]['value'] = date('Y-m-d', strtotime('-1 days'));
$node->field_period_of_reporting[LANGUAGE_NONE][0]['value2'] = date('Y-m-d');
$node->field_reciver[LANGUAGE_NONE][0]['value'] = 2;
$file = file_load($fid);
$file = (array)$file;
$file['display'] = 1;
$node->field_report_file[LANGUAGE_NONE][] = $file;
node_object_prepare($node);
$node = node_submit($node);
node_save($node);

The node doesn't get created. I don't get any error messages in the log. I don't know what I'm doing wrong.

closed as off-topic by Mołot, Елин Й., mpdonadio Sep 23 '14 at 15:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – mpdonadio
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Has the cron run? – Wtower Sep 22 '14 at 13:23
  • Even when you launch the cron manually? – Djouuuuh Sep 22 '14 at 13:27
  • Please post whole hook implementation. And test if it runs at all, with watchdog. – Mołot Sep 22 '14 at 13:29
  • You should also show what generate_periodic_report function does.. – xurshid29 Sep 22 '14 at 15:37
2

So I got my answer by myself: I ran clear cache and it performs everything perfectly. @Andrew Morris: I haven't used Rules before but I think it can't be performed by that. The generate function creates 2 xls files via PHPExcel and gets all files uploaded by user in a period of time. Then it compresses all the files into a zip archive via ZipArchive and saves the data for the zip file. Then it returns the fid of the saved file.

1

This is an oddly similar answer to the last question I answered but... Can't you just do this with Rules?

Event: Cron maintenance tasks are performed

Condition:

Action: Create a new Entity > Type - Node

Then you can use individual 'Set a data value' to set the parameters you need.

0

Either way, you can use your custom code with the drupal function hook_cron().

MyModulename_cron(){

$node = new stdClass();
$node->type = 'reports';
$node->language = LANGUAGE_NONE;
$node->uid = 1;
$node->status = 1;
$node->promote = 0;
$node->comment = 0;
$node->sticky = 0;
$node->moderate = 0;
$node->title = 'Daily Report for ' . date('d.m.Y');
$node->field_reporting_cycle[LANGUAGE_NONE][0]['value'] = 1;
$fid = generate_periodic_report(1);
$node->field_period_of_reporting[LANGUAGE_NONE][0]['value'] = date('Y-m-d', strtotime('-1 days'));
$node->field_period_of_reporting[LANGUAGE_NONE][0]['value2'] = date('Y-m-d');
$node->field_reciver[LANGUAGE_NONE][0]['value'] = 2;
$file = file_load($fid);
$file = (array)$file;
$file['display'] = 1;
$node->field_report_file[LANGUAGE_NONE][] = $file;
node_object_prepare($node);
$node = node_submit($node);
node_save($node);

}

Everytime the cron runs, a new node is created.

I hope it works for you searching on!

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