Hello, I've wrote a minimal custom handler for drupal webform 8-x, it works as expected;

now I'd like to send back to the webform some custom text to display to the user; the text

depends from the webform submission fields values; I've read I could use the "[webform:handler:?]" token

but I don't understand how to put values in it from the module. I think the token will be something like:

[webform:handler:my_custom_module:completed:mytext]

but I'm unable to find a (simple) working example: I'm not a drupal programmer, the example could help me a lot

Can anyone point me to the right direction ? And where can I find documentation about classes and methods of webform module ?

Thank you

Marco

Comments

wombatbuddy’s picture

To display a message you may use the code like this: 

$this->messenger()->addStatus($this->t('Your message'));

See Drupal 8 Set Message Example.

phonecall’s picture

Hello wombatbuddy, thank you for the answer; I think I've already tried this approach, but if I'm not wrong it just

prints a status message on top of the confirmation message; what I need is to use one (or more) results from the handler to build the

confirmation message

wombatbuddy’s picture

One way is to create a "Settings" webfrom handler instead of an "Action" handler. The example: 

namespace Drupal\my_module\Plugin\WebformHandler;

use Drupal\webform\Plugin\WebformHandler\SettingsWebformHandler;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Dynamic confirm message.
 *
 * @WebformHandler(
 *   id = "dynamic_confirm_message",
 *   label = @Translation("Dynamic confirm message"),
 *   category = @Translation("Settings"),
 *   description = @Translation("Change confirmation message after form submission"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */
class ConfirmMessageSettingsWebformHandler extends SettingsWebformHandler {
  
  public function overrideSettings(array &$settings, WebformSubmissionInterface $webform_submission) {
    $settings['confirmation_message'] = $this->t('My new confirmation message');
  }
}

The screenshot: https://ibb.co/w6wmpSm

wombatbuddy’s picture

But if you want to use the token, you can create a hidden element of the "value" type and populate in the webform handler. The screenshots of the example:
1. https://ibb.co/w6Xzb0D
2. https://ibb.co/bzzSpNR
3. https://ibb.co/WGsY12f
4. https://ibb.co/0VxSzpN

The code: 

<?php

namespace Drupal\my_module\Plugin\WebformHandler;

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

/**
 * Set confirm message after submission.
 *
 * @WebformHandler(
 *   id = "custom_webfrom_handler",
 *   label = @Translation("Set confirmation message"),
 *   category = @Translation("Action"),
 *   description = @Translation("Set confirmation message after form submission"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */
class ConfirmMessageWebformHandler extends WebformHandlerBase {
  
  /**
   * {@inheritdoc}
   */
  public function preSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {  
    $webform_submission->setElementData('confirm_message', '<h1>My confirm message</h1>');
  }

}
phonecall’s picture

Hello wombatbuddy, thank you very much for your very useful answers, they clarified me a lot of

things and solve my problem.

It's not important, but Just for curiosity: when are tokens like [webform:handler:handler_id:state:key] useful ?

Thank you very much

Marco

wombatbuddy’s picture

Perhaps, this can be applied in case of remote posting of webfrom submissions. For instance, this approach is used in the "webform_example_remote_post" module, which is distributed with the Webform module. You may look at the "remotePost()" method of the "RemotePostWebformHandler" class and at the "WebformExampleRemotePostController" class.
References: 
1. https://git.drupalcode.org/project/webform/-/blob/6.x/src/Plugin/Webform...
2. https://git.drupalcode.org/project/webform/-/blob/6.x/modules/webform_ex...
3. https://www.drupal.org/project/webform/issues/2913491

phonecall’s picture

Thank you very much again

Marco