You are here

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

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::buildOptionsForm()
  2. 8 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 342

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.'),
    '#default_value' => $this->options['clear_on_exposed'],
  ];
  $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)) {
    $selected_actions = $this->options['selected_actions'];
    $preconfiguration = $this->options['preconfiguration'];
  }
  else {
    $selected_actions = [];
    $preconfiguration = [];
    foreach ($form_values as $id => $value) {
      $selected_actions[$id] = $value['state'] ? $id : 0;
      $preconfiguration[$id] = isset($value['preconfiguration']) ? $value['preconfiguration'] : [];
    }
  }
  foreach ($this->actions as $id => $action) {
    $form['selected_actions'][$id]['state'] = [
      '#type' => 'checkbox',
      '#title' => $action['label'],
      '#default_value' => empty($selected_actions[$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'][$id]['preconfiguration'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Preconfiguration for "@action"', [
        '@action' => $action['label'],
      ]),
      '#attributes' => [
        'data-for' => $id,
        'style' => empty($selected_actions[$id]) ? 'display: none' : NULL,
      ],
    ];

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

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