0

I have a view that list entities. It has a VBO field on it for executing a few custom actions.

I can get the actions working, but I need to add a field to the operations form on the view which lets the user select a date field. I then need to pass that date value on the VBO form submit, to the custom action, so that I can use it to manipulate some entity data.

I am able to add the date field via this method:

function mymodule_views_bulk_operations_form_alter(&$form, &$form_state, $vbo){
    $form['vbo_set_close_date'] = array (
        '#type' => 'date_popup',
        '#title' => t ('Set Close Date'),
        '#date_format' => 'm/d/Y',
        '#date_label_position' => 'within',
        '#weight' => 1000,
    );
}

Now I am stuck because that data doesn't get passed to my custom action, and I'm not sure what the correct method would be to accomplish that. I've though of adding a custom form submit handler, but still not sure if that's working because I can't seem to get it to trigger.

1 Answer 1

2

Awhile back I did come up with a work around for this. Basically you can create a custom submission handler for the VBO form, then have that custom handler perform the code you need.

/**
* Implements hook_views_bulk_operations_form_alter().
*
* This is the function that alters our VBO form
*
* @param $form
* @param $form_state
* @param $vbo
*/
function mymodule_views_bulk_operations_form_alter(&$form, &$form_state, $vbo){

  // Here I added a date popup to the form
  $form['vbo_set_close_date'] = array (
        '#type' => 'date_popup',
        '#title' => t ('Set Close Date'),
        '#date_format' => 'm/d/Y',
        '#date_label_position' => 'within',
        '#weight' => -10000,
  );

  // Here is where I added the custom submission handler. This calls the "mymodule_set_close_dates" method
  $form['select']['submit']['#submit'][] = "mymodule_set_close_dates";

}

/**
 * Callback function for VBO form
 *
 * @param $form
 * @param $form_state
 */
function mymodule_set_close_dates($form, &$form_state){

  $date = $form_state['values']['vbo_set_close_date'];
  $action = $form_state['operation']->operationId;
  if(!is_null($date) && $action == 'action::mymodule_close_job'){ // <------- custom action
      $value = strtotime($date);
      $relations = $form_state['build_info']['args'][0]->result;
      foreach ($relations as $relation){
        $rid = $relation->rid;
        $wrapper = entity_metadata_wrapper('relation', $rid);
        $wrapper->field_ended->set($value);
        $wrapper->save();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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