public function ViewsBulkOperationsBulkForm::viewsForm in Views Bulk Operations (VBO) 8
Same name and namespace in other branches
- 8.3 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::viewsForm()
- 8.2 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::viewsForm()
- 4.0.x src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::viewsForm()
Form constructor for the bulk form.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
File
- src/
Plugin/ views/ field/ ViewsBulkOperationsBulkForm.php, line 421
Class
- ViewsBulkOperationsBulkForm
- Defines the Views Bulk Operations field plugin.
Namespace
Drupal\views_bulk_operations\Plugin\views\fieldCode
public function viewsForm(array &$form, FormStateInterface $form_state) {
// Make sure we do not accidentally cache this form.
// @todo Evaluate this again in https://www.drupal.org/node/2503009.
$form['#cache']['max-age'] = 0;
$use_revision = array_key_exists('revision', $this->view
->getQuery()
->getEntityTableInfo());
// Add select all and tableselect libraries for table display style.
if ($this->view->style_plugin instanceof Table) {
$form['#attached']['library'][] = 'core/drupal.tableselect';
$form['#attached']['library'][] = 'views_bulk_operations/selectAll';
}
// Only add the bulk form options and buttons if
// there are results and any actions are available.
$action_options = $this
->getBulkOptions();
if (!empty($this->view->result) && !empty($action_options)) {
// Prepare entity labels data so the view will not
// need to be executed again on possible confirmation
// or configuration forms.
$this->tempStoreData['entity_labels'] = [];
// Render checkboxes for all rows.
$form[$this->options['id']]['#tree'] = TRUE;
foreach ($this->view->result as $row_index => $row) {
$entity = $this
->getEntity($row);
$this->tempStoreData['entity_labels'][$row_index] = $entity
->label();
$form[$this->options['id']][$row_index] = [
'#type' => 'checkbox',
// We are not able to determine a main "title" for each row, so we can
// only output a generic label.
'#title' => $this
->t('Update this item'),
'#title_display' => 'invisible',
'#default_value' => !empty($form_state
->getValue($this->options['id'])[$row_index]) ? 1 : NULL,
'#return_value' => self::calculateEntityBulkFormKey($entity, $use_revision, $row_index),
];
}
// Ensure a consistent container for filters/operations
// in the view header.
$form['header'] = [
'#type' => 'container',
'#weight' => -100,
];
// Build the bulk operations action widget for the header.
// Allow themes to apply .container-inline on this separate container.
$form['header'][$this->options['id']] = [
'#type' => 'container',
'#attributes' => [
'id' => 'vbo-action-form-wrapper',
],
];
// Display actions buttons or selector.
if ($this->options['buttons']) {
unset($form['actions']['submit']);
foreach ($action_options as $id => $label) {
$form['actions'][$id] = [
'#type' => 'submit',
'#value' => $label,
];
}
}
else {
// Replace the form submit button label.
$form['actions']['submit']['#value'] = $this
->t('Apply to selected items');
$form['header'][$this->options['id']]['action'] = [
'#type' => 'select',
'#title' => $this->options['action_title'],
'#options' => [
'' => $this
->t('-- Select action --'),
] + $action_options,
];
}
// Add AJAX functionality if actions are configurable through this form.
if (empty($this->options['form_step'])) {
$form['header'][$this->options['id']]['action']['#ajax'] = [
'callback' => [
__CLASS__,
'viewsFormAjax',
],
'wrapper' => 'vbo-action-configuration-wrapper',
];
$form['header'][$this->options['id']]['configuration'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'vbo-action-configuration-wrapper',
],
];
$action_id = $form_state
->getValue('action');
if (!empty($action_id)) {
$action = $this->actions[$action_id];
if ($this
->isConfigurable($action)) {
$actionObject = $this->actionManager
->createInstance($action_id);
$form['header'][$this->options['id']]['configuration'] += $actionObject
->buildConfigurationForm($form['header'][$this->options['id']]['configuration'], $form_state);
$form['header'][$this->options['id']]['configuration']['#config_included'] = TRUE;
}
}
}
// Select all results checkbox.
$show_all_selector = FALSE;
if (!empty($this->view->pager) && method_exists($this->view->pager, 'hasMoreRecords')) {
$show_all_selector = $this->view->pager
->getCurrentPage() > 0 || $this->view->pager
->hasMoreRecords();
}
$this->tempStoreData['total_results'] = $this->viewData
->getTotalResults();
if ($show_all_selector) {
$form['header'][$this->options['id']]['select_all'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Select all@count results in this view', [
'@count' => $this->tempStoreData['total_results'] ? ' ' . $this->tempStoreData['total_results'] : '',
]),
'#attributes' => [
'class' => [
'vbo-select-all',
],
],
];
}
// Duplicate the form actions into the action container in the header.
$form['header'][$this->options['id']]['actions'] = $form['actions'];
}
else {
// Remove the default actions build array.
unset($form['actions']);
}
}