Integrating Views into Rules

Events happening in the community are now at Drupal community events on www.drupal.org.
aantonop's picture

Hello everyone,

I'm building a project/task management system on Drupal for my company.

I need to integrate Views into Rules. The general idea is the following:

I want to trigger a rule on cron and once a week send an "overview" to each member of a project showing their tasks, by email.

Rather than writing a gnarly PHP SQL kludge, I'd rather just work on some basic views integration. The basic idea is to create an rules action in the views module that allows me to render a specific view (perhaps passing arguments) and store the output.

Here's the integration plan:

  • views.rules.inc that defines one action only (Render view as variable), takes 1 argument (for starters) which is the view id to be executed and stores one text-type variable.
  • Action renders a view into a text variable, with the human and machine readable labels for use as a token or PHP variable.
  • Next action takes the text variable and uses it in some way (in my case it is part of the body of an email)

A few questions:

  • Does this sound useful and interesting to anyone
  • Can a text variable hold as much data as a whole rendered view?
  • Is this the right approach to integrating views output into the rules engine?

I'll have code to show in a couple of days, so please help me with some feedback!

Thanks

Comments

Views Integration with Rules

aantonop's picture

Here's some code:

views.rules.inc is the integration stub. It can go into modules/rules/rules/modules/views.rules.inc

Once installed there, a new action appears in Rules called "Views == Render view to a variable"

Here's a rule that tests the integration for me. It simply displays the default page of a specific view (TaskOverview) whenever any node is updated and shows the view on screen. You will probably want to put some conditions in there or something to do your testing without making it appear on every node update.

In the test I use the rendered view in some PHP to display it in a configurable message (on screen). In practice I will use it to send people emails with an embedded view.

It's not particularly elegant, but it works. The specific view I'm rendering is an HTML table, so the result is full HTML, which will make a nice HTML email. You can however make a view that is simple and unformatted, if that's what you need.

I'd love some feedback or comments. One thing I'm wondering is whether I should use tokens instead of a variable to return the view. I don't know how to do that yet, so any suggestions welcome.

array (
'rules' =>
array (
'rules_20' =>
array (
'#type' => 'rule',
'#set' => 'event_node_update',
'#label' => 'Render a view',
'#active' => 1,
'#weight' => '0',
'#categories' =>
array (
),
'#status' => 'custom',
'#conditions' =>
array (
),
'#actions' =>
array (
0 =>
array (
'#weight' => 0,
'#info' =>
array (
'label' => 'Render a View to a variable',
'arguments' =>
array (
'view_name' =>
array (
'type' => 'string',
'label' => 'View Name',
),
'display_id' =>
array (
'type' => 'string',
'label' => 'Display ID',
'description' => 'To figure out the id of a display, hover your mouse over the tab to select that display. Everything after the \'#views-tab-\' is the id of that display. This ID is guaranteed never to change unless you delete the display and create a new one.',
),
),
'new variables' =>
array (
'rendered_view_task_overview' =>
array (
'label' => 'TaskOverview',
'label callback' => false,
'type' => 'string',
'save' => true,
),
),
'module' => 'Views',
),
'#name' => 'views_rules_action_render_view',
'#settings' =>
array (
'view_name' => 'TaskOverview',
'display_id' => 'default',
'#argument map' =>
array (
'rendered_view' => 'rendered_view_task_overview',
),
),
'#type' => 'action',
),
1 =>
array (
'#type' => 'action',
'#settings' =>
array (
'message' => 'Rendered View:

<?php
echo $rendered_view_task_overview;
?>
',
'error' => 0,
'#eval input' =>
array (
'rules_input_evaluator_php' =>
array (
'message' =>
array (
0 => 'rendered_view_task_overview',
),
),
),
),
'#name' => 'rules_action_drupal_message',
'#info' =>
array (
'label' => 'Show a configurable message on the site',
'module' => 'System',
'eval input' =>
array (
0 => 'message',
),
),
'#weight' => 0,
),
),
),
),
'rule_sets' =>
array (
),
)

This looks to be really

fago's picture

This looks to be really useful! What about submitting it to views as a patch? You have to take care to obey the coding style though.

Is there a way to retrieve the output without html, perhaps there is a suiting views display? Else you could add an optional, that uses drupal_html_to_text() on it or just strips the tags.

I'd love some feedback or comments. One thing I'm wondering is whether I should use tokens instead of a variable to return the view. I don't know how to do that yet, so any suggestions welcome.

I think we would just need token support for the "string" variable, which just outputs the string. This should go into rules, patch welcome.. ;)

Help me make it a good patch

aantonop's picture

fago,

I can submit it as a patch. I'll have to go read about the coding style. I bet I need to fix a lot of {} right?

The choice of HTML or not should really happen in the view. You can pick which of the view's many displays you want to use. You can define an HTML display or a plain text or unformatted display in the view. So I don't think I should manipulate the output in the rules integration. If HTML is what you want, you define the view to give you HTML. If not, then write a plaintext view. It can only add confusion if that decision is made in two different places (rules and views).

The question of token vs. variable. Is a token suitable for a very LARGE chunk of text, possibly including HTML? The output could be several pages. It could even be a CSV output (you can do that quite easily in views/templates). From that perspective, is a variable better than a token or not?

Of course I have no idea how to do tokens. Let me go do some reading.

Since you like this and welcome the patch, I need to go clean it up, re-code it, do some basic error checking and validation and wrap it in doxygen, right? Can you tell me where to submit the final? I've never submitted a patch for a module.

By the way, if you liked this and tell me how to submit a proper patch, I've got more code coming. I've almost finished integrating the "messaging" module. Next up, I'm integrating "notifications".

got the documentation

aantonop's picture

I found the docs on coding style, security and submitting patches in the Drupal Developers Documentation. I will get it done today

Thanks!

Submitted as views module patch

aantonop's picture

For latest code go here:

http://drupal.org/node/508550

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: