options['id'] . '--' . $row->index . '-->'; } /** * Form constructor for the views form. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ public function viewsForm(&$form, FormStateInterface $form_state) { // Make sure we do not accidentally cache this form. $form['#cache']['max-age'] = 0; // The view is empty, abort. if (empty($this->view->result)) { unset($form['actions']); return; } $form[$this->options['id']]['#tree'] = TRUE; foreach ($this->view->result as $row_index => $row) { $form[$this->options['id']][$row_index] = [ '#type' => 'submit', '#value' => t('Test Button'), '#name' => 'test-button-' . $row_index, '#test_button' => TRUE, '#row_index' => $row_index, '#attributes' => ['class' => ['test-button']], ]; } } /** * Submit handler for the views form. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ public function viewsFormSubmit(&$form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); if (!empty($triggering_element['#test_button'])) { $row_index = $triggering_element['#row_index']; $view_args = !empty($this->view->args) ? implode(', ', $this->view->args) : $this->t('no arguments'); $this->messenger()->addStatus($this->t('The test button at row @row_index for @view_id (@display) View with args: @args was submitted.', [ '@display' => $this->view->current_display, '@view_id' => $this->view->id(), '@args' => $view_args, '@row_index' => $row_index, ])); } } /** * {@inheritdoc} */ public function query() { // Do nothing. } }