I was trying to create a custom EmailWebformHandler using an example at the following URL.

In this example the sendMessage()-function has one argument: an array called $message.
However, when I use this example to create my own EmailWebformHandler I get an error in PhpStorm:
Declaration must be compatible with WebformHandlerMessageInterface->sendMessage(webform_submission: \Drupalwebform\WebformSubmissionInterface, message: array)

Because the official example contained the code this way, i tried it, but i couldn't add my EmailWebformHandler to the webform, so I tried solving this problem by adding the argument that was missing: WebformSubmissionInterface $webform_submission

This way I could add my custom EmailWebformHandler and change the needed settings, but when I tried sending my form, I got the following error:

TypeError: Argument 1 passed to Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::sendMessage() must implement interface Drupal\webform\WebformSubmissionInterface, array given, called in /Users/michaelsoetaert/Webdev/sites/tvh/git/www.tvh-d8.com/modules/custom/tvh_webform_handlers/src/Plugin/WebformHandler/SendToFriendEmailWebformHandler.php on line 26 in Drupal\webform\Plugin\WebformHandler\EmailWebformHandler->sendMessage() (line 754 of modules/contrib/webform/src/Plugin/WebformHandler/EmailWebformHandler.php).
Drupal\webform\Plugin\WebformHandler\EmailWebformHandler->sendMessage(Array) (Line: 26)
Drupal\tvh_webform_handlers\Plugin\WebformHandler\SendToFriendEmailWebformHandler->sendMessage(Object, Array) (Line: 529)
Drupal\webform\Plugin\WebformHandler\EmailWebformHandler->postSave(Object, , NULL) (Line: 1639)
Drupal\webform\Entity\Webform->invokeHandlers('postSave', Object, , NULL) (Line: 751)
Drupal\webform\WebformSubmissionStorage->invokeWebformHandlers('postSave', Object, ) (Line: 701)
Drupal\webform\WebformSubmissionStorage->doPostSave(Object, ) (Line: 395)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 768)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 364)
Drupal\Core\Entity\Entity->save() (Line: 681)
Drupal\webform\Entity\WebformSubmission->save() (Line: 944)
Drupal\webform\WebformSubmissionForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 585)
Drupal\Core\Form\FormBuilder->processForm('webform_submission_send_to_friend_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('webform_submission_send_to_friend_form', Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'default') (Line: 823)
Drupal\webform\Entity\Webform->getSubmissionForm() (Line: 82)
Drupal\webform\Controller\WebformController->addForm(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

The error is thrown because my first argument is an array, but they expect WebformSubmissionInterface. However in my code, my first argument actually is WebformSubmissionInterface:

public function sendMessage(WebformSubmissionInterface $webform_submission, array $message) {
    kint($message);
    parent::sendMessage($message);
  }

I'm guessing that this is a bug, since both the code in the official example and the code found in EmailWebformHandler.php don't work.

Comments

michaelsoetaert created an issue. See original summary.

jrockowitz’s picture

The below code

public function sendMessage(WebformSubmissionInterface $webform_submission, array $message) {
  parent::sendMessage($message);
}

...needs to include the $webform_submission with the parent::sendMessage call.

public function sendMessage(WebformSubmissionInterface $webform_submission, array $message) {
  parent::sendMessage($webform_submission, $message);
}
jrockowitz’s picture

@michaelsoetaert Can you please do me a favor and update the recipe?

https://www.drupal.org/docs/8/modules/webform/webform-cookbook/how-to-pr...

michaelsoetaert’s picture

Status: Active » Closed (works as designed)

@jrockowitz Thank you for the quick response! I didn't know what I was doing wrong, but I knew it had to be something small. I have also made the changes to the recipe.