Question

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.

Was it helpful?

Solution

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 {
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top