WebformSubmissionNotesEditField.php in Webform Views Integration 8.5
File
src/Plugin/views/field/WebformSubmissionNotesEditField.php
View source
<?php
namespace Drupal\webform_views\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\field\UncacheableFieldHandlerTrait;
use Drupal\views\ResultRow;
use Drupal\webform\WebformSubmissionInterface;
class WebformSubmissionNotesEditField extends FieldPluginBase {
use UncacheableFieldHandlerTrait;
public function getValue(ResultRow $row, $field = NULL) {
return '<!--form-item-' . $this->options['id'] . '--' . $row->index . '-->';
}
public function viewsForm(&$form, FormStateInterface $form_state) {
$form['#cache']['max-age'] = 0;
if (empty($this->view->result)) {
unset($form['actions']);
return;
}
$form[$this->options['id']]['#tree'] = TRUE;
foreach ($this->view->result as $row_index => $row) {
$webform_submission = $this
->getEntity($row);
$form[$this->options['id']][$row_index] = [
'#type' => 'textarea',
'#default_value' => $webform_submission
->getNotes(),
];
}
}
public function viewsFormSubmit(&$form, FormStateInterface $form_state) {
foreach ($this->view->result as $row_index => $row) {
$webform_submission = $this
->getEntity($row);
$notes = $form_state
->getValue($form[$this->options['id']][$row_index]['#parents']);
if ($webform_submission
->getNotes() != $notes) {
$webform_submission
->setNotes($notes);
$webform_submission
->save();
}
}
}
}