I'm trying to create a custom module which will display a message after my webform. I know that in order to display a message, I could use the following code:

$message = "Message here";

But rather than having a specific message, I want to have a conditional message based on PHP if statements. E.g.,

    if($class == A) {
    print "Correct!";

So, my question is how can I get the results of this PHP code to display on my webform? I'm not familiar with PHP code, so I don't know if I can combine the $message code with the if statement somehow. (I've tried variations of this with no success.)

Thanks for your help.

  • Please post more details to the question like what does your correct code looks like and are you able to show the static message using the above code? You can put the $message variable in the inside if block and change it based on the conditions and then put that $message in the drupal_set_message.
    – sorabh.v6
    May 11, 2018 at 7:08
  • For your information, the proper way to pass a variable inside the t() function is to use a place holder. Ex: drupal_set_message(t(@message', array('@message' => $message))); For more info on how to properly use the t() function, see DRUPAL CODE STANDARDS: THE T() FUNCTION
    – No Sssweat
    May 11, 2018 at 7:18
  • which will display a message after my webform Are you referring to after the webform is submitted? or do you reefer to literally after (below) the webform?
    – No Sssweat
    May 11, 2018 at 7:23

1 Answer 1


One of the features and APIs of the Webform module for Drupal 8 that I personally enjoy using is support for inline messages using the WebformMessage element

The WebformMessage element supports dismissal messages and #states API.

Here is the YAML version of a Message element

  '#type': webform_message 
  '#message_type': warning 
  '#message_close': true,
  '#message_storage': user,
  '#message_message': 'This message can be dismissed by a user'

Here is the PHP syntax, which can be used in your custom code.

$build['message'] = [
  '#type' => 'webform_message',
  '#message_id' => 'custom_message_id',
  '#message_type' => 'warning',
  '#message_close' => TRUE,
  '#message_storage' => WebformMessage::STORAGE_USER,
  '#message_message' => $this->t('This message can be dismissed by a user'),

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.