Is there anyway to do this? I have a daily email sent out to site administrators that contains an embedded view of a content type that is only accessible by the admin role. Since the scheduler apparently executes the rule as Anonymous, all that is generated in the email is the default empty text for the view. Making the content type accessible to everyone fixes the problem, but is not really a solution in this case since it could contain personal data.

Comments

tevans’s picture

Status: Closed (fixed) » Active

It took some digging, but I figured it out. The problem is that in D7, cron is always run as "anonymous", so I had to update the PHP code in the message field to impersonate the super user before embedding the view, then switch back to "anonymous" afterwards:

<?php
  global $user;
  $original_user = $user;
  $old_state = drupal_save_session(FALSE);
  $user = user_load(1);

  $view = views_get_view('interactions' , TRUE);
  $views_result = $view->preview('block_1');
  $output = drupal_html_to_text($views_result);
  print $output;

  $user = $original_user;
  drupal_save_session($old_state);
?>
tevans’s picture

Status: Active » Closed (fixed)
timjh’s picture

Status: Active » Closed (fixed)

I have a very similar problem, and your solution may work for me, if I were to understand it. Could you please explain where exactly you put the PHP code above?

tevans’s picture

Sure. Add one of the following actions to your rule or component:

  • Send mail
  • Send mail to all users of a role

On the configuration screen for the action, there is a section labeled "Message". Put your code inside the value field in this section. If I recall correctly, you need to have the PHP filter module from Drupal core enabled for the PHP to be evaluated. You should see a "PHP Evaluation" section inside the "Message" section if it is enabled.

timjh’s picture

Ha! I didn't have PHP filter enabled. All is now clear. Many thanks.

timjh’s picture

Following recommendations, I have PHP filter restricted to administrator use only. Yet it works for anonymous user when invoked by Rules Scheduler!

Presumably Rules Scheduler impersonates the administrator in order to run PHP filter, but not when executing the filtered code (why not?). It must also impersonate the administrator for other rules actions not normally available to an anonymous user, such as publishing or unpublishing a node.

museumboy’s picture

Issue summary: View changes

I'm trying to do the exact thing as TEVANS. The PHP code did not work for me, but gave an out of memory error. I'm wondering if after 3 years a better way has come along.

museumboy’s picture

FYI the out of memory error was not related to the php code. It works now using the same code as above.