I have code that runs a Feed Import i.e.

function cronmonitor40_cron() {
	
		watchdog('cronmonitor40', 'cron 40 has run !!!');
		$name = "church_events_f3";		
		$source = feeds_source($name);
		$source->import();

}

When I run the import via the UI it runs ( as seen in the message log ) as an administrator - and it works ! However when cron runs the code above, it runs as Anonymous(not verified). A watchdog message shows the code is running but the import finds no data to import. But when the job is run via the UI immediately after the code, it does find data. It looks to me like a permission issue, that would be solved if my code was running with administrative privileges - leading to the administrator's name appearing as the user in the message log.

How do I need to change the above code so that cron recognises the code as having administrator privileges ? Thanks.

Comments

peterk900’s picture

I found this post.

global $user;
$original_user = $user;
$old_state = drupal_save_session();
drupal_save_session(FALSE);
$user = user_load(1);
$name = "church_events_f3";		
$source = feeds_source($name);
$source->import();
$user = $original_user;
drupal_save_session($old_state);

Code works like a dream ! - no more Anonymous(not verified) but the name of the administrator in the user column of the message log.

And it looks as though this has solved the problem of the code running but getting no data.