You are here

function draggableviews_form_alter in DraggableViews 8

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

Implements hook_form_alter().

File

./draggableviews.module, line 127
Contains draggableviews.module.

Code

function draggableviews_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Filter the right form.
  if (strpos($form_id, 'views_form_') === FALSE) {
    return;
  }

  // Check whether the view is draggable.
  $view = $form_state
    ->getBuildInfo()['args'][0];
  if (!isset($view->field['draggableviews'])) {
    return;
  }

  // Attach library.
  $form['#attached']['library'][] = 'draggableviews/draggableviews';

  // Remove default submit button.
  $form['actions']['submit']['#access'] = FALSE;
  if (\Drupal::currentUser()
    ->hasPermission('access draggableviews')) {

    // Create draggableviews save order button.
    $form['actions']['save_order'] = [
      '#value' => t('Save order'),
      '#type' => 'submit',
    ];
  }

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