Question

I am using Firebase module to send push notifications to a specific mobile user when a webform is submitted.

Below is my custom webform handler:

<?php

namespace Drupal\my_custom_module\Plugin\WebformHandler;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;

use Drupal\node\Entity\Node;

use Drupal\webform\WebformInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;


/**
 * Send Push Notification when a webform submission is submitted.
 *
 * @WebformHandler(
 *   id = "Send Push Notification when a webform submission is submitted",
 *   label = @Translation("Send Push Notification when a webform submission is submitted"),
 *   category = @Translation("Push Notification"),
 *   description = @Translation("Send Push Notification when a webform submission is submitted"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */
class PushNotificationWebformHandler extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */

  // Function to be fired while submitting the Webform.
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    // Get an array of the values from the submission.
    $values = $webform_submission->getData();

    // Get (NID) from text element available on the webform.
    $nidvalue = $values["attached_node"];

    // Load the node.
    $node = \Drupal\node\Entity\Node::load($nidvalue);

    // Get (selected_node_owner) value from the User Entity reference element available on the webform.
    $nodeowner = $values["selected_node_owner"];

    // Get (token) value from the loaded node through the 'field_node_owner' Entity field.
    $usertoken = $node->field_node_owner->entity->field_user_push_token->value;

    // Send the Push Notification.
    // Token is generated by app. You'll have to send the token to Drupal.
    $messageService = \Drupal::service('firebase.message');
    $messageService->setRecipients($usertoken);
    $messageService->setNotification([
        'title' => 'Hello',
        'body' => 'Hello $nodeowner this is your body message',
        'badge' => 1,
        'icon' => 'optional-icon',
        'sound' => 'optional-sound',
        'click_action' => 'optional-action',
    ]);
    $messageService->setData([
        'score' => '3x1',
        'date' => '2019-04-05',
        'optional' => 'Data is used to send silent pushes. Otherwise, optional.',
    ]);
    $messageService->setOptions(['priority' => 'normal']);
    $messageService->send();
    }
}

Everything is working as expected but the 'Variables' are not rendering any values and printed as they are.

Example:

I am expecting the body of the notification message to be something like below (Say the selected_node_owner name is "Elie Masaad"):

Hello Elie Masaad this is your body message

but instead I am getting the below:

Hello $nodeowner this is your body message

Module maintainer gave the below hint:

You should use a Token or simple str_replace for replacing something in string.

Can anyone guide me on how to pragmatically use tokens for my above requirement ?

Was it helpful?

Solution

As Clive also mentioned use double quotes instead of single quotes to allow the variables to get replaced inside the string

'body' => "Hello $nodeowner this is your body message"

if $values["selected_node_owner"] gives you the UID here is how to get the username

$account = \Drupal\user\Entity\User::load($values["selected_node_owner"]);
$nodeowner = $account->getUsername();
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top