3

I have the following code and created a view for my custom entity type. I just want to create a new action button to be able to change a field's text to something else. I'm not sure why this custom action isn't showing up. Under which window am I suppose to see it? Execute arbitary php or modify multiple entity values?

Where is a good example code of getting a custom action working? Is there a pre-built VBO action example I can just download to see how it's made?

/**
 * Implements hook_action_info().
 */
function rubik_custom_action_info() {
  return array(
    'hideit' => array(
      'type' => 'entity',
      'label' => t('Custom Action'),
      'configurable' => FALSE,
      'triggers' => array('any'),
      'behavior' => array('changes_property'),
      'vbo_configurable' => FALSE,
      'pass rows' => TRUE,
    ),
  );
}

/**
 * Action function for MY_ACTION_NAME.
 */
function hideit(&$entity, $context = array()) {
  drupal_set_message('ok it works');
}
4
+50

There are several VBO examples within the VBO module itself. Take for instance delete.action.inc which allows you to delete any entity. I can't see anything explicitly wrong with your code, but I'd highly recommend you follow convention and rework it to look like the other hook_action_info() implementations:

/**
 * Implements hook_action_info().
 */
function MYMODULE_action_info() {
  $actions['MYMODULE_custom_action'] = array(
    'type' => 'node', // Here come's the name of your custom entity type
    'label' => t('Custom Action'),
    'configurable' => FALSE,
    'triggers' => array('any'),
    'behavior' => array('changes_property'),
    'vbo_configurable' => FALSE,
    'pass rows' => TRUE,
  );

  return $actions;
}

function MYMODULE_custom_action($entity, $context) {
  // Your action here.
}

Your function name is rubik_custom_action_info. You aren't trying to define this within a theme are you? Don't do that. Put it in a separate module. Themes are no place for this.

Another module that may be of help is the Commerce Bundle Helper sandbox. Not only does it declare VBO actions, but it shows how you can automatically add it into a View that already exists by altering the VBO configuration.

4

After your action has been defined, it can be added in the Views UI under Selected Bulk Operations in your Bulk operations field settings. It will be a separate checkbox alongside other actions like Execute arbitrary PHP script, etc. Once selected, it should show up as select option or action button (depending on how the VBO field is configured).

The VBO development guide on d.o. is a good example of working code for defining an action, adding a optional global/per-bulk setting form, & creating action handler.

Here's a very bare bones example:

//Will add a "User Action" option to Bulk Operations: User field in Views UI
function MYMODULE_action_info() {
  return array(
    'MYMODULE_user_action' => array(
      //Can be other entities used in a view (e.g. 'node')
      'type' => 'user',              
      'label' => t('User Action'),
      'behavior' => array('changes_property'),
      'triggers' => array('any'),

      //Set to true if you an intermediate options form for your action
      //'configurable' => true,      

      //Set to true if you want additional globaloptions
      //'vbo_configurable' => true,
    ),
  );
}

function MYMODULE_user_action(&$entity, $context){
  //$entity is the current entity being modified; it can
  //  modified directly to perform the action on the entity

  //$context is an array of contextual information about the 
  //  current state of bulk processing  (e.g. items remaining) 
  //  It also contains configurable options values if any were set (see below)
}

//If 'configurable' => true in hook_action_info() item this trio of functions 
// will add/process an intermediate form just like any Drupal form
function MYMODULE_user_action($settings, &$form_state){
 //$settings['settings'] will contains settings set in 
 //  MYMODULE_user_action_views_bulk_operations_form
}
function MYMODULE_user_action_validate($form, $form_state) {
}
function MYMODULE_user_action_submit($form, $form_state) {
  //return an array of values usable by MYMODULE_user_action
}

//If 'vbo_configurable' => true, in hook_action_info() this presents 
// additional configurable options for the  VBO action. 
// It returns a Drupal form array
function MYMODULE_user_action_views_bulk_operations_form($options) {
}
1

By the implementation of hook_action_info(), you need to write the name of your custom entity type, instead of just entity. For example; node, user, comment, taxonomy_term, field_collection_item etc. The action will be available as long as the base table for your view mathces the type of the given entity in hook_action_info().

/**
 * Implements hook_action_info().
 */
function rubik_custom_action_info() {
  return array(
    'hideit' => array(
      'type' => 'node', // Here come's the name of your custom entity type
      'label' => t('Custom Action'),
      'configurable' => FALSE,
      'triggers' => array('any'),
      'behavior' => array('changes_property'),
      'vbo_configurable' => FALSE,
      'pass rows' => TRUE,
    ),
  );
}
0

Took some time to figure this out, but its working. I have a custom entity called office and I needed to create a new vbo visible toggle button.

I put this code in the custom office module. Originally I put it in the theme template.php file to test, but it didn't work for some reason as I'm guessing it fires too late in the drupal boot process.

/**
* Views Bulk Operation Custom Visible Toggle Button
*/
function office_action_info() {  //office was my module name
  return array(
    'action_off' => array(  //name of your function call
      'type' => 'entity',  //targets all entities on the site
      'label' => t('Toggle Visible'),
      'configurable' => FALSE,
      'behavior' => array('changes_property'),  //set the behavior type
      'triggers' => array('any'),
    ),
  );
}

function action_off($entity, $context = array()) {

  //Populate the variable using entity_load of the associated id.
  $eload = entity_load('office', array($entity->office_id));

  //Changes the field value for the entity. In this case its the Visible field
  $eload[$entity->office_id]->visible = ($eload[$entity->office_id]->visible == 1 ? 0 : 1);

  //After editing the entity array, save it. Used current() to get 1st result.
  entity_save('office', current($eload));

  //not exactly sure what this does, but its to clear the entity cache?
  entity_get_controller('office')->resetCache(array($entity->office_id)); 
}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.