Views Bulk Operations development guide (Drupal 7)

This guide applies to Drupal 7, Views 3 and Views Bulk Operations 7.3-x branch. You can find the Drupal 6 version of this guide here.

VBO reuses and extends Drupal core Action system. Most of the Drupal core actions can be used as a VBO-compatible action. This guide focuses on some VBO-specific functionality. If you are looking for Drupal core actions, please check the Examples module.

(To get the syntax highlighting, this guide will have both opening and closing PHP tags. They are not necessary in your module - you still need the top opening PHP tag)

Define the actions

First, we need to implement hook_action_info().

<?php
function MYMODULE_action_info() {
  return array(
   
'MYMODULE_my_custom_action' => array(
     
'type' => 'node',
     
'label' => t('Search and replace text in a field'),
     
'behavior' => array('changes_property'),
     
'configurable' => FALSE,
     
'vbo_configurable' => TRUE,
     
'triggers' => array('any'),
    ),
  );
}
?>

An associative array of action descriptions. The keys of the array are the names of the action functions, and each corresponding value is an associative array with the following key-value pairs:

Bulk Remove File Entities

This example is used to bulk remove image files that are no longer associated with a media gallery. The site this code is used on is a drupal 7 site with the following modules among others:

  • Media Gallery 7.x-1.0-beta8
  • Views Bulk Operations 7.x-3.1

The attached text file is the views export of the view using the bulk operation. The view lists all files which in my case is fine since the only files uploaded to the site are images in galleries. There is a built in Delete Item option but this did not remove the files and that why I made my own action called Delete Media Files as shown in the image below.
bulk operation config
To make a new action head to /admin/config/system/actions and create a new Execute arbitrary PHP script and call it Delete Media Files if you want it to work with the attached view. The PHP script is below.
Note the php tags should not be pasted in the textbox, just the code.

<?php
$file = file_load($entity->fid);
$references = file_usage_list($file);
if (!empty($references)) {
drupal_set_message(t('The file %title is in use and cannot be deleted.', array('%title' => $file->filename)));
}else{
$result = file_delete($file);
if (is_array($result)) {

Pushing to Telvue with Views Bulk Operation (VBO)

View Bulk Operation can be enabled in any View, but is available in the [YOUT SITE]/admin/content

As shown in the screenshot below, the Push To Telvue option is on the select list.

Creating a simple site chat solution using shoutbox

Here is a simple way to leverage the power of rules, views bulk operations, private message, private message growl and shoutbox to create a simple chat solution for your site. Site runs on shared server with limited resources and php 5.2
Please note that after one month of using this solution I found envolve chat module which is what I am using on my site now as it reduces the burden on the system.
My Requirements:

  1. One single public chat on the site which auto updates
  2. Notifications to active logged in users on the site only
  3. Per user ability to opt out from the notifications messages

Step 1: Module Requirements (D7 versions):

  1. Private message
  2. Views
  3. Views Bulk Operations
  4. Private Message growl
  5. Shoutbox

Using Views Bulk Operation to delete orphan taxonomy terms (Terms that have no nodes associated with them)

Deleting orphan terms especially in a free tagging vocabulary is reasonably common task for site administrators. Orphan terms in this case means taxonomy terms that are not used by any nodes (content pieces). I had to look up various issue queues for this and found this in the issue queue of the term node count module. If interested here is the link to the page #603868: Rework Term Node Count for D7
Steps to get this working:

  1. Create a new view. Name it anything you want.
  2. Display: choose taxonomy term. If you want to delete orphan tags from a particular vocabulary, choose the vocabulary. Add the page title etc and click create and edit to move to the next page
  3. In the section Relationships choose "Taxonomy Term: Content with Term" from the list of options. Leave the default identifier "node" or choose a descriptive label if you wish.
  4. In the Fields section add Content: nid and Bulk Operations: Taxonomy Term. Label the Content: nid field with a descriptive label such as "node count". For the Bulk Operation field type in label as "select" . Ensure enable select all items and display processing result are checked . In the selected operation choose the delete operation. Check the delete without confirmation if you want to delete the terms without an intermediate confirmatory dialog page.

Views Bulk Operations (VBO) development guide

This guide applies to Drupal 6 only, last updated for release 6.x-1.13. There is a Drupal 7 VBO development guide available.

Developing operations for VBO

VBO reuses existing Drupal infrastructure to allow new operations to be added. The main method to create a new operation is by writing a Drupal Action. Please read the linked page first. VBO supports a superset of the capabilities of actions, so here are the steps to create an action that fully utilizes VBO.

Implement hook_action_info()

VBO supports a superset of the attributes that describe an action. Here are the added attributes:

  • parameters (optional): keyed array. This attribute gets appended as is to the $context array that is passed to action_function().
  • behavior (optional): array of flags. This attribute exists in the original Action specification, but is augmented here with additional flags:
    • views_node_property to signify that this action will show the node on-screen.
    • deletes_node_property to signify that this action might delete the node.
Subscribe with RSS Subscribe to RSS - Views Bulk Operations