5 * Views Bulk Operations conditions and actions for Rules.
9 * Implements hook_rules_condition_info().
11 function views_bulk_operations_rules_condition_info() {
12 $conditions = array();
13 $conditions['views_bulk_operations_condition_result_count'] = array(
14 'label' => t('Check number of results returned by a VBO View'),
18 'label' => t('View and display'),
19 'options list' => 'views_bulk_operations_views_list',
20 'description' => t('Select the VBO view and display you want to check'),
21 'restriction' => 'input',
25 'label' => t('Arguments'),
26 'description' => t('Any arguments to pass to the view, one per line.
27 You may use token replacement patterns.'),
32 'label' => t('Minimum number of results'),
33 'description' => t('This condition returns TRUE if the view has at
34 least the given number of results.'),
37 'group' => t('Views Bulk Operations'),
44 * Implements hook_rules_action_info().
46 function views_bulk_operations_rules_action_info() {
48 $actions['views_bulk_operations_action_load_list'] = array(
49 'label' => t('Load a list of entity objects from a VBO View.'),
53 'label' => t('View and display'),
54 'options list' => 'views_bulk_operations_views_list',
55 'description' => t('Select the view and display you want to use to
57 'restriction' => 'input',
61 'label' => t('Arguments'),
62 'description' => t('Any arguments to pass to the view, one per line.
63 You may use token replacement patterns.'),
68 'entity_list' => array(
69 'type' => 'list<entity>',
70 'label' => t('A list of entities'),
73 'group' => t('Views Bulk Operations'),
75 $actions['views_bulk_operations_action_load_id_list'] = array(
76 'label' => t('Load a list of entity ids from a VBO View.'),
80 'label' => t('View and display'),
81 'options list' => 'views_bulk_operations_views_list',
82 'description' => t('Select the view and display you want to use to
84 'restriction' => 'input',
88 'label' => t('Arguments'),
89 'description' => t('Any arguments to pass to the view, one per line.
90 You may use token replacement patterns.'),
95 'entity_id_list' => array(
96 'type' => 'list<integer>',
97 'label' => t('A list of entity ids'),
100 'group' => t('Views Bulk Operations'),
107 * Lists all available VBO Views and their displays.
108 * Naturally, only the displays that contain a VBO field are listed.
111 * An array of all views and their displays on the form 'view|display',
112 * formatted to be used as an select list.
114 function views_bulk_operations_views_list() {
115 $selectable_displays = array();
116 foreach (views_get_enabled_views() as $name => $base_view) {
117 foreach ($base_view->display as $display_name => $display) {
118 $view = $base_view->clone_view();
119 $view->build($display_name);
120 $vbo = _views_bulk_operations_get_field($view);
122 $selectable_displays[$view->name . '|' . $display_name] = check_plain($view->human_name) . ' | ' . check_plain($display->display_title);
126 return $selectable_displays;
130 * The 'views_bulk_operations_condition_result_count' condition.
133 * A string in the format "$view_name|$display_name".
135 * Arguments that should be passed to the View.
137 * An integer representing the minimum number of results that satisfies the
141 * TRUE if the view has more than $minimum results, FALSE otherwise.
143 function views_bulk_operations_condition_result_count($view, $args, $minimum) {
144 $vbo = _views_bulk_operations_rules_get_field($view, $args);
145 return (count($vbo->view->result) >= $minimum);
149 * The 'views_bulk_operations_action_views_load_list' action.
152 * A string in the format "$view_name|$display_name".
154 * Arguments that should be passed to the View.
156 * Array containing the entity_list, an array of entity objects.
157 * - array('entity_list' => array(...))
159 function views_bulk_operations_action_load_list($view, $args) {
160 $vbo = _views_bulk_operations_rules_get_field($view, $args);
162 // Get all entities, pass ids to the wrapper for lazy loading.
163 $entity_type = $vbo->get_entity_type();
164 $entities = entity_metadata_wrapper("list<$entity_type>", array());
165 foreach ($vbo->view->result as $row_index => $result) {
166 $entities[] = entity_metadata_wrapper($entity_type, $vbo->get_value($result));
169 return array('entity_list' => $entities);
173 * The 'views_bulk_operations_action_views_load_id_list' action.
176 * A string in the format "$view_name|$display_name".
178 * Arguments that should be passed to the View.
180 * Array containing the entity_id_list, an Array of entity ids as integer
182 * - array('entity_list' => array(...))
184 function views_bulk_operations_action_load_id_list($view, $args) {
185 $vbo = _views_bulk_operations_rules_get_field($view, $args);
187 // Get all entity ids.
189 foreach ($vbo->view->result as $row_index => $result) {
190 $ids[] = $vbo->get_value($result);
193 return array('entity_id_list' => $ids);
197 * Info alteration callback for the 'views_bulk_operations_action_views_load_list' action.
199 * The info hook specifies that the action returns a generic list of entities
200 * (list<entity>). All actions that require entities of specific type can't
201 * use such entities, so this alter hook specifies the exact entity type
202 * after the action has been configured, allowing the view to be loaded
203 * and its entity type extracted.
205 function views_bulk_operations_action_load_list_info_alter(&$element_info, RulesAbstractPlugin $element) {
206 // The action hasn't been configured yet, hence no view. Abort.
207 if (empty($element->settings['view'])) {
211 $entity_type = _views_bulk_operations_rules_get_entity_type($element->settings['view']);
213 $element_info['provides']['entity_list']['type'] = "list<$entity_type>";
218 * Helper function that loads and builds (but doesn't execute) the specified view,
219 * then determines the entity type on which the VBO field operates.
221 * @param $view_target
222 * A string in the format "$view_name|$display_name".
225 * The entity type on which the VBO field operates.
227 function _views_bulk_operations_rules_get_entity_type($view_target) {
228 $entity_types = &drupal_static(__FUNCTION__);
230 if (!isset($entity_types[$view_target])) {
231 $views_settings = explode('|', $view_target);
232 if ($view = views_get_view($views_settings[0])) {
233 $view->set_display($views_settings[1]);
236 $vbo = _views_bulk_operations_get_field($view);
238 $entity_type = !empty($vbo) ? $vbo->get_entity_type() : '';
239 $entity_types[$view_target] = $entity_type;
242 return $entity_types[$view_target];
246 * Helper function that loads, builds and executes the specified view,
247 * then returns its VBO field.
249 * @param $view_target
250 * A string in the format "$view_name|$display_name".
252 * Arguments that should be passed to the View.
255 * The VBO field. Contains a reference to the View.
257 function _views_bulk_operations_rules_get_field($view_target, $args) {
258 $views = &drupal_static(__FUNCTION__);
260 $views_settings = explode('|', $view_target);
261 $view_name = $views_settings[0];
262 $display_name = $views_settings[1];
263 // Create an array of arguments.
264 $view_arguments = explode("\n", $args);
265 $view_arguments = array_map('trim', $view_arguments);
266 $view_arguments = array_filter($view_arguments, 'strlen');
267 // Append the filtered list of arguments to $views_target, so that the correct
268 // View is fetched from cache.
269 if (!empty($view_arguments)) {
270 $view_target .= '|' . implode('&', $view_arguments);
273 // Don't execute the requested View more than once in a single page request.
274 if (isset($views[$view_target])) {
275 $vbo = _views_bulk_operations_get_field($views[$view_target]);
279 // Load the view and set the properties.
280 $view = views_get_view($view_name);
281 $view->set_display($display_name);
282 $view->set_arguments($view_arguments);
284 $vbo = _views_bulk_operations_get_field($view);
285 // Unset every field except the VBO one (which holds the entity id).
286 // That way the performance hit becomes much smaller, because there is no
287 // chance of views_handler_field_field::post_execute() firing entity_load().
288 foreach ($view->field as $field_name => $field) {
289 if ($field_name != $vbo->options['id']) {
290 unset($view->field[$field_name]);
293 $view->execute($view->current_display);
294 // Save the view in the static cache.
295 $views[$view_target] = $view;