3 namespace Drupal\views_test_data\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\field\FieldPluginBase;
7 use Drupal\views\Plugin\views\field\UncacheableFieldHandlerTrait;
8 use Drupal\views\ResultRow;
11 * A handler to provide a field that is completely custom by the administrator.
13 * @ingroup views_field_handlers
15 * @ViewsField("field_form_button_test")
17 class FieldFormButtonTest extends FieldPluginBase {
19 use UncacheableFieldHandlerTrait;
24 public function getValue(ResultRow $row, $field = NULL) {
25 return '<!--form-item-' . $this->options['id'] . '--' . $row->index . '-->';
29 * Form constructor for the views form.
32 * An associative array containing the structure of the form.
33 * @param \Drupal\Core\Form\FormStateInterface $form_state
34 * The current state of the form.
36 public function viewsForm(&$form, FormStateInterface $form_state) {
37 // Make sure we do not accidentally cache this form.
38 $form['#cache']['max-age'] = 0;
39 // The view is empty, abort.
40 if (empty($this->view->result)) {
41 unset($form['actions']);
45 $form[$this->options['id']]['#tree'] = TRUE;
46 foreach ($this->view->result as $row_index => $row) {
47 $form[$this->options['id']][$row_index] = [
49 '#value' => t('Test Button'),
50 '#name' => 'test-button-' . $row_index,
51 '#test_button' => TRUE,
52 '#row_index' => $row_index,
53 '#attributes' => ['class' => ['test-button']],
59 * Submit handler for the views form.
62 * An associative array containing the structure of the form.
63 * @param \Drupal\Core\Form\FormStateInterface $form_state
64 * The current state of the form.
66 public function viewsFormSubmit(&$form, FormStateInterface $form_state) {
67 $triggering_element = $form_state->getTriggeringElement();
68 if (!empty($triggering_element['#test_button'])) {
69 $row_index = $triggering_element['#row_index'];
70 $view_args = !empty($this->view->args) ? implode(', ', $this->view->args) : $this->t('no arguments');
71 drupal_set_message($this->t('The test button at row @row_index for @view_id (@display) View with args: @args was submitted.', [
72 '@display' => $this->view->current_display,
73 '@view_id' => $this->view->id(),
74 '@args' => $view_args,
75 '@row_index' => $row_index,
83 public function query() {