class FieldFormButtonTest in Drupal 10
Same name and namespace in other branches
- 8 core/modules/views/tests/modules/views_test_data/src/Plugin/views/field/FieldFormButtonTest.php \Drupal\views_test_data\Plugin\views\field\FieldFormButtonTest
- 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
- class \Drupal\views_test_data\Plugin\views\field\FieldFormButtonTest extends \Drupal\views\Plugin\views\field\FieldPluginBase uses UncacheableFieldHandlerTrait
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\fieldView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FieldFormButtonTest:: |
public | function |
Overrides UncacheableFieldHandlerTrait:: |
|
FieldFormButtonTest:: |
public | function | ||
FieldFormButtonTest:: |
public | function | Form constructor for the views form. | |
FieldFormButtonTest:: |
public | function | Submit handler for the views form. | |
UncacheableFieldHandlerTrait:: |
protected | function | Actually renders the field markup. | |
UncacheableFieldHandlerTrait:: |
abstract protected | function | ||
UncacheableFieldHandlerTrait:: |
public | function | ||
UncacheableFieldHandlerTrait:: |
public | function |