You are here

function views_bulk_operations_form in Views Bulk Operations (VBO) 7.3

Same name and namespace in other branches
  1. 5 views_bulk_operations.module \views_bulk_operations_form()
  2. 6.3 views_bulk_operations.module \views_bulk_operations_form()
  3. 6 views_bulk_operations.module \views_bulk_operations_form()

Extend the views_form multistep form with elements for executing an operation.

1 call to views_bulk_operations_form()
views_bulk_operations_form_alter in ./views_bulk_operations.module
Implements hook_form_alter().

File

./views_bulk_operations.module, line 474
Allows operations to be performed on items selected in a view.

Code

function views_bulk_operations_form($form, &$form_state, $vbo) {
  $form['#attached']['js'][] = drupal_get_path('module', 'views_bulk_operations') . '/js/views_bulk_operations.js';
  $form['#attached']['js'][] = array(
    'data' => array(
      'vbo' => array(
        'row_clickable' => $vbo
          ->get_vbo_option('row_clickable'),
      ),
    ),
    'type' => 'setting',
  );
  $form['#attached']['css'][] = drupal_get_path('module', 'views_bulk_operations') . '/css/views_bulk_operations.css';

  // Wrap the form in a div with specific classes for JS targeting and theming.
  $class = 'vbo-views-form';
  if (empty($vbo->view->result)) {
    $class .= ' vbo-views-form-empty';
  }
  $form['#prefix'] = '<div class="' . $class . '">';
  $form['#suffix'] = '</div>';

  // Force browser to reload the page if Back is hit.
  if (!empty($_SERVER['HTTP_USER_AGENT']) && preg_match('/msie/i', $_SERVER['HTTP_USER_AGENT'])) {
    drupal_add_http_header('Cache-Control', 'no-cache');

    // works for IE6+
  }
  else {
    drupal_add_http_header('Cache-Control', 'no-store');

    // works for Firefox and other browsers
  }

  // Set by JS to indicate that all rows on all pages are selected.
  $form['select_all'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => 'select-all-rows',
    ),
    '#default_value' => FALSE,
  );
  $form['select'] = array(
    '#type' => 'fieldset',
    '#title' => t('Operations'),
    '#collapsible' => FALSE,
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
  );
  if ($vbo
    ->get_vbo_option('display_type') == 0) {
    $options = array(
      0 => t('- Choose an operation -'),
    );
    foreach ($vbo
      ->get_selected_operations() as $operation_id => $operation) {
      $options[$operation_id] = $operation
        ->label();
    }

    // Create dropdown and submit button.
    $form['select']['operation'] = array(
      '#type' => 'select',
      '#options' => $options,
    );
    $form['select']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Execute'),
      '#validate' => array(
        'views_bulk_operations_form_validate',
      ),
      '#submit' => array(
        'views_bulk_operations_form_submit',
      ),
    );
  }
  else {

    // Create buttons for operations.
    foreach ($vbo
      ->get_selected_operations() as $operation_id => $operation) {
      $form['select'][$operation_id] = array(
        '#type' => 'submit',
        '#value' => $operation
          ->label(),
        '#validate' => array(
          'views_bulk_operations_form_validate',
        ),
        '#submit' => array(
          'views_bulk_operations_form_submit',
        ),
        '#operation_id' => $operation_id,
      );
    }
  }

  // Adds the "select all" functionality if the view has results.
  // If the view is using a table style plugin, the markup gets moved to
  // a table row below the header.
  // If we are using radio buttons, we don't use select all at all.
  if (!empty($vbo->view->result) && !$vbo
    ->get_vbo_option('force_single')) {
    $enable_select_all_pages = FALSE;

    // If the view is paginated, and "select all items on all pages" is
    // enabled, tell that to the theme function.
    if (isset($vbo->view->total_rows) && count($vbo->view->result) != $vbo->view->total_rows && $vbo
      ->get_vbo_option('enable_select_all_pages')) {
      $enable_select_all_pages = TRUE;
    }
    $enable_select_this_page = $vbo
      ->get_vbo_option('enable_select_this_page');
    $form['select_all_markup'] = array(
      '#type' => 'markup',
      '#markup' => theme('views_bulk_operations_select_all', array(
        'view' => $vbo->view,
        'enable_select_all_pages' => $enable_select_all_pages,
        'enable_select_this_page' => $enable_select_this_page,
      )),
    );
  }
  return $form;
}