Version 1
[yaffs-website] / web / core / modules / views / tests / modules / views_test_data / src / Plugin / views / field / FieldFormButtonTest.php
1 <?php
2
3 namespace Drupal\views_test_data\Plugin\views\field;
4
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;
9
10 /**
11  * A handler to provide a field that is completely custom by the administrator.
12  *
13  * @ingroup views_field_handlers
14  *
15  * @ViewsField("field_form_button_test")
16  */
17 class FieldFormButtonTest extends FieldPluginBase {
18
19   use UncacheableFieldHandlerTrait;
20
21   /**
22    * {@inheritdoc}
23    */
24   public function getValue(ResultRow $row, $field = NULL) {
25     return '<!--form-item-' . $this->options['id'] . '--' . $row->index . '-->';
26   }
27
28   /**
29    * Form constructor for the views form.
30    *
31    * @param array $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.
35    */
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']);
42       return;
43     }
44
45     $form[$this->options['id']]['#tree'] = TRUE;
46     foreach ($this->view->result as $row_index => $row) {
47       $form[$this->options['id']][$row_index] = [
48         '#type' => 'submit',
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']],
54       ];
55     }
56   }
57
58   /**
59    * Submit handler for the views form.
60    *
61    * @param array $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.
65    */
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,
76       ]));
77     }
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function query() {
84     // Do nothing.
85   }
86
87 }