Score:0

How update a webform submitted value clicking in a button?

in flag

How can I update a submitted data just clicking in a button? I have created a View table of webform subissions and I would like to add a collumn with a button "Aprroval". When user click on this button, the data value must be updated. My intention is optimize the "edit" and "save", avoiding the Form edit screen. Add a custom action in a "Webform submission operations bulk form" solves my problem too.

Score:4
ne flag

You can use hook_entity_operation to add a custom entity operation for webform submissions. With the Webform Views Integration module you should already be able to add an "Operations" field to your view. It lists the default operations depending on access levels (Edit, Delete etc.). There your custom operation will appear.

MYMODULE.module

<?php

/**
 * @file
 * Hooks implemented by the MYMODULE module.
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Implements hook_entity_operation().
 */
function MYMODULE_entity_operation(EntityInterface $submission) {
  $operations = [];
  if ($submission instanceof WebformSubmissionInterface) {
    if ($submission->getElementData('MY_HIDDEN_REVIEW_ELEMENT') !== 'approved') {
      $operations['approve'] = [
        'title' => t('Approve'),
        'weight' => 15,
        'url' => Url::fromRoute('MYMODULE.webform_submission_approve', [
          'submission' => $submission->id(),
        ], [
          'query' => \Drupal::destination()->getAsArray(),
        ]),
      ];
    }
  }
  return $operations;
}

MYMODULE.routing.yml

MYMODULE.webform_submission_approve:
  path: '/admin/MYMODULE/submission/{submission}/approve'
  defaults:
    _controller: '\Drupal\MYMODULE\Controller\WebformSubmissionApproveController::approve'
  options:
    parameters:
      submission:
        type: 'entity:webform_submission'
  requirements:
    _custom_access: '\Drupal\MYMODULE\Controller\WebformSubmissionApproveController::access'

src/Controller/WebformSubmissionApproveController.php

<?php

namespace Drupal\MYMODULE\Controller;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\webform\Entity\WebformSubmission;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class WebformSubmissionApproveController.
 *
 * @package Drupal\MYMODULE\Controller
 */
class WebformSubmissionApproveController extends ControllerBase {

  /**
   * Approve method.
   *
   * @param \Drupal\webform\Entity\WebformSubmission $submission
   *   A webform submission.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current HTTP request.
   *
   * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
   *   The response.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function approve(WebformSubmission $submission, Request $request) {

    $submission->setElementData('MY_HIDDEN_REVIEW_ELEMENT', 'approved');
    $submission->save();

    $this->messenger()->addMessage($this->t('Submission @serial approved.', [
      '@serial' => $submission->serial(),
    ]));

    return $request->query->get('destination') ? new RedirectResponse($request->query->get('destination')) : [];
  }

  /**
   * Checks access for a specific request.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   The access result.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function access(WebformSubmission $submission) {
    return AccessResult::allowedIf(!$submission->isDraft() && in_array('administrator', $this->currentUser()->getRoles()));
  }

}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.