3

My problem is that I can't override the URL where VBO redirects me to after I performed a default action. When I write my own actions I simply put $_GET['destination'] = 'my/target'; inside my custom form submit and that works. I tried to alter VBO's form submit to attach my own submit but that doesn't work:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_bulk_operations_form') {
    array_push($form['#submit'], 'MYMODULE_vbo_redirect');
  };
}

function MYMODULE_vbo_redirect() {
  $_GET['destination'] = 'my/target';
}

When I testhack the VBO module and inside views_bulk_operations_form_submit($form, &$form_state) replace the final redirect with my custom one, it works. How can I get inside there and get my custom redirect executed without hacking?

  • This shouldn't be different for VBO forms, I think the accepted answer to this question is the sort of thing you're looking for – Clive Jul 22 '14 at 10:42
2

I tried to alter the wrong form. VBO somehow merges its form stuff with the views form itself. So I had to look there to attach my custom submit. Then I had to pay attention to attach it in the same moment the views_bulk_operations_form_submit is attached. Then I set the cancel button to also bring me back where I want. And finally set $form_state['redirect'] in my attached submit.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_form_MYVIEW_page') {
    $form['#action'] = '/my/target'; // the url that is displayed when in VBO multistep

    if (!empty($form['actions']['submit']['#submit'])) {

      if (in_array('views_bulk_operations_form_submit', $form['actions']['submit']['#submit'])) {
        $form['actions']['submit']['#submit'][] = 'MYMODULE_vbo_redirect';
        $form['actions']['cancel']['#href'] = 'my/target';
        $form['actions']['cancel']['#options']['path'] = 'my/target';
        dpm($form);
      }
    }
  }
}

function MYMODULE_vbo_redirect($form, &$form_state) {
  $form_state['redirect'] = array('my/target');
}

Only problem left is that this redirect always gets triggered when performing any VBO action from this view. But I can live with that for now.

  • this is just excellent :) – Kojo Apr 2 '16 at 16:12
1

Has for VBO, there is a hook dedicated for form alter: hook_views_bulk_operations_form_alter(&$form, &$form_state, $vbo)

Example (modifying the email action):

function YOURMODULE_views_bulk_operations_form_alter(&$form, &$form_state, $vbo) {
  $form_id = $form['#form_id'];
  if ($form_state['step'] == 'views_form_views_form') {
    // Alter the first step of the VBO form (the selection page).
  }
  elseif ($form_state['step'] == 'views_bulk_operations_config_form') {
    // Alter the configuration step of the VBO form.
    if( $form_id == 'views_form_YOURVIEWSMACHINENAME_YOURVIEWSDISPLAY' ) {
      if(isset($form['recipient'])) {
        $form['recipient']['#default_value'] = '[user:mail]';
      }
      $form['message']['#type'] = 'text_format'; //> sends an array...
      $form['message']['#default_value'] = variable_get('NAMEOFAVAR');
      //add token list
      $form['token'] = array(
          '#type' => 'fieldset',
          '#title' => t('Replacement tokens'),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
      );
      $token_types = array('user');
      $form['token']['tokens'] = array(
          '#markup' => theme('token_tree', array('token_types' => $token_types))
      );
    }
  }
  elseif ($form_state['step'] == 'views_bulk_operations_confirm_form') {
    // Alter the confirmation step of the VBO form.
    array_unshift($form['actions']['submit']['#submit'], 'YOURMODULE_sendmail_vbo_submit');
    $form_state['operation']->formOptions['message'] = $form_state['operation']->formOptions['message']['value'];
  }
}

The only thing to notice is that in your custom submit function, you don't get the form_state values has usual. I must dig more to find how to get them.

EDIT: To alter/save/do stuff with $form_state['values'], better to put your custom submit or validate function in the second "if":

elseif ($form_state['step'] == 'views_bulk_operations_config_form') {
      $form['actions']['submit']['#validate'][] = 'YOURMODULE_sendmail_vbo_validate';
}
  • Be aware my code is work in progress, don't just rely on it... Must need some more "if" to be perfect... ;-) – anou Apr 24 '15 at 19:35
0

In general you can use hook_drupal_goto_alter, for example:

/**
 * Implements hook_drupal_goto_alter().
 */
function MYMODULE_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  if (CONDITION) {
    $path = 'my/target';
  }
}

Where in CONDITION you can check for the specific desired path which you want to override by checking the value of current_path() or arg().

Not ideal, but it's simpler than implementing custom submit handler (e.g. in $form['select']['action::views_bulk_operations_argument_selector_action']['#submit']) using hook_views_bulk_operations_form_alter and finding the right place to override the path.

-1
function MYMODULE_form_alter(&$form, &$form_state, $form_id)
{
 if ($form_id == 'views_bulk_operations_form') {
$form['#submit']='MYMODULE_vbo_redirect';
}
}
function MYMODULE_vbo_redirect() {
// Use die('here') and submit the form.if it dies then redirect it 
drupal_goto('my/target');
}

Hope this will helps you

  • I think drupal_goto() is no good idea here. It stops the whole VBO process before performing any action. – leymannx Jul 22 '14 at 12:43

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.