1

I am very new to Drupal development though I am an experienced developer. I created a custom Webform Handler following these tutorials and postings:

These for what files to create and where:

Create a custom webform handler in Drupal 8

Create a Webform Handler that sends a notification to Slack

Add a custom submission handler to a form

This post for the code to redirect a user to another webform upon form submission:

Page redirect in custom WebformHandlerBase

I have looked at everything and everything looks correct. Here's what's in the YAML file modules\custom\redirect_to_ccbilling\redirect_to_ccbilling.info.yml:

name: 'Redirect to CC Billing' description: 'Provides a custom webform handler for the "submit an event" webform so it will redirect to another webform node' core_version_requirement: ^8.8 Core: 8.x package: Custom type: module 

Here's what's in the modules\custom\redirect_to_ccbilling\src\Plugin\WebformHandler\RedirectToCCBillingWebformHandler.php:

<?php namespace Drupal\redirect_to_ccbilling\Plugin\WebformHandler; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Serialization\Yaml; use Drupal\Core\Form\FormStateInterface; use Drupal\webform\Plugin\WebformHandlerBase; use Drupal\webform\src\WebformSubmissionInterface; /** * Form submission handler. * * @RedirectToCCBilling( * id = "Redirect to CC Billing", * label = @Translation("Redirect to CC Billing"), * category = @Translation("Webform Handler"), * description = @Translation("Redirect user to the credit card billing page."), * cardinality = * \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE, * results = * \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED, * ) */ class RedirectToCCBillingWebformHandler extends WebformHandlerBase { /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) { $form_id = 'webform_submission_' . $webform_submission->getWebform()->id() . '_form'; if($form_id == 'webform_submission_xxx_form') { $values = $webform_submission->getData(); /* Take the action based on the Webform submission values */ } } public function confirmForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) { $form_id = 'webform_submission_' . $webform_submission->getWebform()->id() . '_form'; if($form_id == 'webform_submission_xxx_form') { if(based_on_some_condition) { /* Here 12 is the node id where i wanted to redirect to */ $form_state->setRedirect('entity.node.canonical',['node' => 1764]); } } } ?> 

I went into the Extend menu and under Custom section I selected the Redirect to CC Billing module/handler and clicked Install at the bottom of the page. However, when I go back to my webform >> setting >> emails / handlers >> +add handler all I see is this:

enter image description here

I don't know what I am missing for my custom handler to not be appearing in the list of handlers.

2
  • 2
    For the plugin to be discovered you need to place it in a WebformHandler subfolder (might be a typo because there is only one / missing) and start the annotation with * @WebformHandler(, not the custom id. Commented Feb 3, 2021 at 21:43
  • Ah yes, the folder path was just a typo. I copy/pasted the folder structure and typed the filename but forgot to type a \ first. Commented Feb 4, 2021 at 0:29

1 Answer 1

3

A few things in addition to what @4k4 commented:

  1. Please replace your annotation plugin reference with @WebformHandler

  2. Use lower case / snake case names for your Webform Handler ID eg. redirect_to_ccbilling

  3. Ensure correct directory structure where the plugin discovery will look for Webform Handler classes. This should be /modules/custom/redirect_to_ccbilling/src/Plugin/WebformHandler/RedirectToCCBillingWebformHandler.php

  4. Ensure that your module is enabled. Either via drush en redirect_to_ccbilling -y or by enabling it on the Extend module page.

/** * Form submission handler. * * @WebformHandler( * id = "redirect_to_ccbilling", * label = @Translation("Redirect to CC Billing"), * category = @Translation("Webform Handler"), * description = @Translation("Redirect user to the credit card billing page."), * cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE, * results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED, * ) */ class RedirectToCCBillingWebformHandler extends WebformHandlerBase { 
6
  • Ok, so I made those changes so that the annotation plugin reference is @WebformHandler and the change the Webform Handler ID to "redirect_to_ccbilling". Being very new to Drupal I actually don't know how/where to execute step 4 for enabling the module. I have been going to the Extend page in Drupal, selecting the module and then clicking Install at the bottom of the page. Is that not the same thing. If not, I need to request RDC to the server as I assume that drush command is something I need to do in a command line. Commented Feb 4, 2021 at 0:33
  • Also, the reason I had changed the annotation plugin reference is due to a problem I had before changing it which has now returned once changing it back. When I go to the emails / handlers page for my webform and click +Add Handler, I get the wheel spin but the handler popup doesn't open. Commented Feb 4, 2021 at 0:35
  • Sorry, I didn't intend to confuse you. What you did is absolutely fine by going to that page. The command I shared is just an alternative to do it via the cli. I'll update my answer Commented Feb 4, 2021 at 0:35
  • A spinning wheel usually happens because of an Ajax error. I suggest you open your network tab and look for any 500 errors, or look in your syslog / watchdog (dblog) after that happens. Commented Feb 4, 2021 at 0:37
  • 1
    Ahh, awesome. Ok so 2 things were wrong in the RedirectToCCBillingWebformHandler.php file. It was missing the closing "}" for the class and in my attempts to get the +Add Handler to work I had added the "src" folder in the use statement for the WebformSubmissionInterface. Got that from this link: drupal.org/project/webform/issues/2857904. So the "}" caused an AJAX error and once I fixed that I got the 500 error and found that link .. thanks @baikho Commented Feb 4, 2021 at 22:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.