You are here

class FieldFormButtonTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/tests/modules/views_test_data/src/Plugin/views/field/FieldFormButtonTest.php \Drupal\views_test_data\Plugin\views\field\FieldFormButtonTest
  2. 9 core/modules/views/tests/modules/views_test_data/src/Plugin/views/field/FieldFormButtonTest.php \Drupal\views_test_data\Plugin\views\field\FieldFormButtonTest

A handler to provide a field that is completely custom by the administrator.

Plugin annotation

@ViewsField("field_form_button_test");

Hierarchy

Expanded class hierarchy of FieldFormButtonTest

File

core/modules/views/tests/modules/views_test_data/src/Plugin/views/field/FieldFormButtonTest.php, line 17

Namespace

Drupal\views_test_data\Plugin\views\field
View source
class FieldFormButtonTest extends FieldPluginBase {
  use UncacheableFieldHandlerTrait;

  /**
   * {@inheritdoc}
   */
  public function getValue(ResultRow $row, $field = NULL) {
    return '<!--form-item-' . $this->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.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldFormButtonTest::getValue public function Overrides UncacheableFieldHandlerTrait::getValue
FieldFormButtonTest::query public function
FieldFormButtonTest::viewsForm public function Form constructor for the views form.
FieldFormButtonTest::viewsFormSubmit public function Submit handler for the views form.
UncacheableFieldHandlerTrait::doRender protected function Actually renders the field markup.
UncacheableFieldHandlerTrait::getFieldTokenPlaceholder abstract protected function
UncacheableFieldHandlerTrait::postRender public function
UncacheableFieldHandlerTrait::render public function