0

I'm using Drupal 8, and I'm trying to block a user after submitting a certain webform. I have the below scenario:

  • Only authenticated user can submit and use the webform
  • Webform submission is limited to once per user
  • Users can save drafts of the webform and continue filling it later on
  • Rules action or otherwise should take place when the user has logged in

I tried to use Rules with rules_webform module but to no avail. The actions in rules are all there, but I need a condition to detect if the user has submitted the webform completely, not saved it as a draft only.

Appreciate any help given

Thanks

1

The best way to do this is to write your own webform handler and attach it to the webform's postSave() method. You can also experiment and try another method if needed from WebformHandlerBase.

Webform handlers are not documented well, but they should be because they are quite useful.

modules/custom/mymodule/src/Plugin/WebformHandler/MyModuleWebformHandler.php

<?php

namespace Drupal\mymodule\Plugin\WebformHandler;

use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Webform example handler.
 *
 * @WebformHandler(
 *   id = "mymodule_webform_handler",
 *   label = @Translation("My Module Webform Handler"),
 *   category = @Translation("My Module"),
 *   description = @Translation("Block users after submission"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_OPTIONAL,
 * )
 */

class MyModuleWebformHandler extends WebformHandlerBase
{

  /**
   * {@inheritdoc}
   */
  public function postSave(WebformSubmissionInterface $webformSubmission, $update = TRUE)
  {
    if (!$webformSubmission->isDraft()) {
      $webformSubmission->getOwner()->block();
    }
  }

}

After getting that setup, enable your new module (don't forget an info.yml file) and then edit your webform settings > handlers to add this to that particular form.

  • Hi, thank you for your swift reply, I did what you suggested and handler showed in the settings->handlers, but it didn't take effect, is there anything i'm missing here? – ram_griever Mar 19 at 20:16
  • never mind, it worked, thank you so much – ram_griever Mar 19 at 20:19
  • Hi again, just wanted to point out that the code above also happen on 'save draft' not just on the final submission. I didn't find a condition to make the above code happen on the final submission only, i'm still trying to figure it out, cheers... – ram_griever Mar 23 at 13:18
  • 1
    done, the code to check if it's a draft or not is $webformSubmission->isDraft() – ram_griever Mar 23 at 15:24
  • Thanks I updated the answer. I usually disable drafts so this wasn't something I ran into. – Ryan Hartman Mar 23 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.