Views

Actions in Views Bulk Operations

Views Bulk Operations come handy when doing bulk operations over a selected list. This is great because it combines the power of views to filter the selection and specific actions to do on each selected entity. For Ex: Updating some field in the record, deleting selected entity and so on.

Views also provide an easier way to define your own set of custom functions to perform on each selected row of entities.

First, we need to define an action :

Organic Groups Views Exposed Filter Selection

It becomes very frustrating when one had to try various options but not be able to create drop down list of groups for selection via an exposed filter option in a view.

There is a very simple solution to it. In the Group Audience field settings, select "Render Views filters as select list" option under ADDITIONAL BEHAVIORS. This selection would make the Group Audience field selected as an exposed filter display drop down options of all the groups available.

Change the text of Apply Button on views Exposed form

The following snippet changes the views exposed form Apply Button to Search Jobs. Make the changes in template.php file of the currently enabled default theme. Clear cache to see it work.(Clear class registry cache to be precise.)

<?php

function YOUR_THEME_NAME_preprocess_views_exposed_form(&$vars, $hook){
// only alter the required form based on id
// drupal_set_message(t("form id: ".$vars['form']['#id']), 'status', FALSE);
if ($vars['form']['#id'] == 'views-exposed-form-search-openings-page') {
// Change the text on the submit button

How to Modify Output of a View

Using views_pre_render hook one can alter the output of a view

<?php
/**
* Implements hook_views_pre_render().
*/
function jicustom_views_pre_render(&$view) {
if($view->name=='stock_report'){
$results = $view->result;
foreach ($results as $key => $result) {
$collection_name = $result->field_collection_item_field_name;
if($collection_name == 'field_transaction_items_outgoing'){
//Modify output
$results[$key]->field_field_trans_item_quantity[0]['rendered']['#markup'] = -($result->field_field_trans_item_quantity[0]['raw']['value']);
}

Provide a link to add content from VIEWS

I found out a better way to provide "Add content" link at the footer region of a VIEW. We can achieve followings:

- Pass any number of arguments.
- User can access link only if he/she has permission.

N.B- use Entity Reference for prepopulating valued from URL

Example:

<?php
global $user;
$view = views_get_current_view();
$args = $view->args[0];
if (user_access('create test_report content', $user)) {
$output=l(t('Add new Biochemistry Test '), 'node/add/test-report', array(
'query' => array(
'field_tr_patient_ref' =>$args,

Executing Views Bulk Operations Using Drush

Many times we use Views Buk Operations with Batch Api for doing a big set of operations. When the count of operations is huge, even Batch API gets short. A faster and more efficient way to do that is using drush to do the heavy backlifting.

The commands are:

vbo-execute      Execute a bulk operation based on a Views Bulk Operations (VBO) view.                                                   
vbo-list             List all Views Bulk Operations (VBO) views, along with the operations associated with each.

Examples:

How to Set Filters for a View in Drupal 7 programmatically

Technologies: 

Accessing a view programmatically

<?php
$view = views_get_view('view_name');
$view->init();
$view->set_display('default'); // or display id like page_1, block_1
$view->set_arguments(array($ARGS));
//display_name = 'default' or 'page' and so on
//filter_name is the name of the filter
//debug($view->display['display_name']->handler->options['filters']['filter_name']);
$view->display['default']->handler->options['filters']['date_filter']['value']['min']='2012-05-01';
$view->display['default']->handler->options['filters']['date_filter']['value']['max']='2012-06-30';

Extract Result Data From Drupal View

Technologies: 

The code to do the trick is provided below:

<?php
    $view
= views_get_view('view_name');
   
$view->init();
   
$view->set_display('default'); // or display id like page_1, block_1
    //$view->set_arguments(array('arg_data'));
    //$view->set_arguments(array(arg(0),'arg_data2'));  // 2nd  Argument
   
$view->pre_execute();
   
$view->execute();
   
//print_r($view->result);
   
drupal_set_message("Field data value: ".$view->result[0]->node_data_field_as_it_appears_in_views_query_sql);
?>

Update for Drupal 7:

<?php