You are here

public function ViewsBulkOperationsBulkForm::buildOptionsForm in Views Bulk Operations (VBO) 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::buildOptionsForm()
  2. 8.2 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::buildOptionsForm()
  3. 4.0.x src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::buildOptionsForm()

Default options form that provides the label widget that all fields should have.

Overrides FieldPluginBase::buildOptionsForm

File

src/Plugin/views/field/ViewsBulkOperationsBulkForm.php, line 397

Class

ViewsBulkOperationsBulkForm
Defines the Views Bulk Operations field plugin.

Namespace

Drupal\views_bulk_operations\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {

  // If the view type is not supported, suppress form display.
  // Also display information note to the user.
  if (empty($this->actions)) {
    $form = [
      '#type' => 'item',
      '#title' => $this
        ->t('NOTE'),
      '#markup' => $this
        ->t('Views Bulk Operations will work only with normal entity views and contrib module views that are integrated. See \\Drupal\\views_bulk_operations\\EventSubscriber\\ViewsBulkOperationsEventSubscriber class for integration best practice.'),
      '#prefix' => '<div class="scroll">',
      '#suffix' => '</div>',
    ];
    return;
  }
  $form['#attributes']['class'][] = 'views-bulk-operations-ui';
  $form['#attached']['library'][] = 'views_bulk_operations/adminUi';
  $form['batch'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Process in a batch operation'),
    '#default_value' => $this->options['batch'],
  ];
  $form['batch_size'] = [
    '#title' => $this
      ->t('Batch size'),
    '#type' => 'number',
    '#min' => 1,
    '#step' => 1,
    '#description' => $this
      ->t('Only applicable if results are processed in a batch operation.'),
    '#default_value' => $this->options['batch_size'],
  ];
  $form['form_step'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Configuration form on new page (configurable actions)'),
    '#default_value' => $this->options['form_step'],
    // Due to #2879310 this setting must always be at TRUE.
    '#access' => FALSE,
  ];
  $form['buttons'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display selectable actions as buttons.'),
    '#default_value' => $this->options['buttons'],
  ];
  $form['clear_on_exposed'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Clear selection when exposed filters change.'),
    '#description' => $this
      ->t('With this enabled, selection will be cleared every time exposed filters are changed, select all will select all rows with exposed filters applied and view total count will take exposed filters into account. When disabled, select all selects all results in the view with empty exposed filters and one can change exposed filters while selecting rows without the selection being lost.'),
    '#default_value' => $this->options['clear_on_exposed'],
  ];
  $form['force_selection_info'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Always show selection info'),
    '#default_value' => $this->options['force_selection_info'],
    '#description' => $this
      ->t('Should the selection info fieldset be shown above the view even if there is only one page of results and full selection can be seen in the view itself?'),
  ];
  $form['action_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Action title'),
    '#default_value' => $this->options['action_title'],
    '#description' => $this
      ->t('The title shown above the actions dropdown.'),
  ];
  $form['selected_actions'] = [
    '#tree' => TRUE,
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Selected actions'),
    '#attributes' => [
      'class' => [
        'vbo-actions-widget',
      ],
    ],
  ];

  // Load values for display.
  $form_values = $form_state
    ->getValue([
    'options',
    'selected_actions',
  ]);
  if (is_null($form_values)) {
    $config_data = $this->options['selected_actions'];
    $selected_actions_data = [];
    foreach ($config_data as $key => $item) {
      $selected_actions_data[$item['action_id']] = $item;
    }
  }
  else {
    $selected_actions_data = $form_values;
  }
  $delta = 0;
  foreach ($this->actions as $id => $action) {
    $form['selected_actions'][$delta]['action_id'] = [
      '#type' => 'value',
      '#value' => $id,
    ];
    $form['selected_actions'][$delta]['state'] = [
      '#type' => 'checkbox',
      '#title' => $action['label'],
      '#default_value' => empty($selected_actions_data[$id]) ? 0 : 1,
      '#attributes' => [
        'class' => [
          'vbo-action-state',
        ],
      ],
    ];

    // There are problems with AJAX on this form when adding
    // new elements (Views issue), a workaround is to render
    // all elements and show/hide them when needed.
    $form['selected_actions'][$delta]['preconfiguration'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Preconfiguration for "@action"', [
        '@action' => $action['label'],
      ]),
      '#states' => [
        'visible' => [
          sprintf('[name="options[selected_actions][%d][state]"]', $delta) => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Default label_override element.
    $form['selected_actions'][$delta]['preconfiguration']['label_override'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Override label'),
      '#description' => $this
        ->t('Leave empty for the default label.'),
      '#default_value' => isset($selected_actions_data[$id]['preconfiguration']['label_override']) ? $selected_actions_data[$id]['preconfiguration']['label_override'] : '',
    ];

    // Also allow to force a default confirmation step for actoins that don't
    // have it implemented.
    if (empty($action['confirm_form_route_name'])) {
      $form['selected_actions'][$delta]['preconfiguration']['add_confirmation'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Add confirmation step'),
        '#default_value' => isset($selected_actions_data[$id]['preconfiguration']['add_confirmation']) ? $selected_actions_data[$id]['preconfiguration']['add_confirmation'] : FALSE,
      ];
    }

    // Load preconfiguration form if available.
    if (method_exists($action['class'], 'buildPreConfigurationForm')) {
      if (!isset($selected_actions_data[$id]['preconfiguration'])) {
        $selected_actions_data[$id]['preconfiguration'] = [];
      }
      $actionObject = $this->actionManager
        ->createInstance($id);

      // Set the view so the configuration form can access to it.
      if ($this->view instanceof ViewExecutable) {
        if ($this->view->inited !== TRUE) {
          $this->view
            ->initHandlers();
        }
        $actionObject
          ->setView($this->view);
      }
      $form['selected_actions'][$delta]['preconfiguration'] = $actionObject
        ->buildPreConfigurationForm($form['selected_actions'][$delta]['preconfiguration'], $selected_actions_data[$id]['preconfiguration'], $form_state);
    }
    $delta++;
  }
  parent::buildOptionsForm($form, $form_state);
}