You are here

function draggableviews_form_alter in DraggableViews 7.2

Same name and namespace in other branches
  1. 8 draggableviews.module \draggableviews_form_alter()
  2. 2.0.x draggableviews.module \draggableviews_form_alter()

Implements hook_form_alter().

Alter views form to change button label.

File

./draggableviews.module, line 48

Code

function draggableviews_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'views_form_') === 0) {
    $draggableviews = _draggableviews_get_field($form_state['build_info']['args'][0]);
  }

  // Not a draggableviews-enabled views form.
  if (empty($draggableviews)) {
    return;
  }

  // Remove the default submit button.
  // The submit button added by Views Form API might be used by a non-Draggableviews
  // form handler. If there is no such handler on the view, hide the button.
  $has_other_views_form_handlers = FALSE;
  foreach ($draggableviews->view->field as $field) {
    if (property_exists($field, 'views_form_callback') || method_exists($field, 'views_form')) {
      if (!$field instanceof draggableviews_handler_field_draggable) {
        $has_other_views_form_handlers = TRUE;
      }

      // If there is another handler that wants remove the button, however, will leave it there
      // it if there is another handler exist, then this will causes a problem, as the button will
      // show up when neither one wanted it. A known example is the Views Bulk Operations module.
      if ($field instanceof views_bulk_operations_handler_field_operations) {

        // VBO Does wants it on the confirmation page, but not the initial view form page.
        if ($form_state['step'] == 'views_form_views_form') {

          // Reset it back to FALSE.
          $has_other_views_form_handlers = FALSE;
        }
      }
    }
  }
  if (!$has_other_views_form_handlers) {
    $form['actions']['submit']['#access'] = FALSE;
  }

  // Create draggableviews save order button.
  $form['actions']['save-order'] = array(
    '#type' => 'submit',
  );

  // If there is no results remove the save-order button.
  if (!isset($form['draggableviews'][0])) {
    $form['actions']['save-order']['#access'] = FALSE;
    return;
  }

  // Check permissions and number of results.
  if (!user_access('access draggableviews') || count($draggableviews->view->result) < 2) {
    $form['actions']['save-order']['#access'] = FALSE;
    return;
  }

  // Do not show "Submit" button on preview and warn user they cannot save in Preview mode.
  $current_path = current_path();
  if (strpos($current_path, 'admin/structure/views/nojs/preview') !== FALSE) {
    $form['actions']['message'] = array(
      '#markup' => '<div class="messages warning">' . t('It is not possible to save order in Preview mode.') . '</div>',
    );
    $form['actions']['save-order']['#access'] = FALSE;
    return;
  }
  $options = $draggableviews->view->field['draggableviews']->options['draggableviews'];
  $form['actions']['save-order']['#value'] = t($options['save_button_label']);
  $form['actions']['save-order']['#submit'][] = 'draggableviews_views_submit';
  if ($options['ajax']) {
    $form['actions']['save-order']['#ajax'] = array(
      'callback' => 'draggableviews_view_draggabletable_form_ajax',
    );
    $form['actions']['save-order']['#attributes']['class'][] = 'js-draggableviews-save-order';
    $form['actions']['save-order']['#attributes']['class'][] = 'element-invisible';
  }

  // Set action as current path.
  $form['#action'] = url(current_path());

  // Keep destination and other GET params.
  if (count($_GET) > 1) {
    $get = $_GET;
    unset($get['q']);
    if (!isset($_GET['destination'])) {
      $get['destination'] = current_path() . '?' . drupal_http_build_query($get);
    }
    $form['#action'] .= '?' . drupal_http_build_query($get);
  }
}