whatsapp_password_reset-1.0.0-alpha1/whatsapp_password_reset.module
whatsapp_password_reset.module
<?php
/**
* @file
* Enables the organization of content into categories.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Render\PlainTextOutput;
/**
* Implements hook_help().
*/
function whatsapp_password_reset_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.whatsapp_password_reset':
$output = '';
$output .= '<h3>' . t('Whatsapp Password Reset') . '</h3>';
$output .= '<p>' . t('Whatsapp Business messages') . '</p>';
return $output;
}
}
function _user_whatsapp_notify($op, AccountInterface $account, $langcode = NULL) {
if($account->hasField('field_whatsapp_capi_number') && !$account->get('field_whatsapp_capi_number')->isEmpty()){
if (\Drupal::config('user.settings')->get('notify.' . $op)) {
$phone = $account->field_whatsapp_capi_number->value;
$variables = [
'user' => $account,
];
$langcode = $langcode ? $langcode : $account->getPreferredLangcode();
$token_service = \Drupal::token();
$mail_config = \Drupal::config('user.mail');
$token_options = [
'langcode' => $langcode,
'callback' => 'user_mail_tokens',
'clear' => TRUE,
];
$subject = $mail_config->get($op . '.subject');
$subject = $token_service->replace($subject, $variables, $token_options);
$message_subject = PlainTextOutput::renderFromHtml($subject);
$body = $mail_config->get($op . '.body');
$body = $token_service->replace($body, $variables, $token_options);
$message_body = PlainTextOutput::renderFromHtml($body);
$content = '*'.$message_subject."*\n";
$content .= $message_body;
$fieldData = [
'type' => 'text',
'text' => [
'body' => $content
]
];
$whatsapp = \Drupal::service('whatsapp_cloud_api');
$whatsapp->sendMessage($phone, $fieldData);
return TRUE;
}
return NULL;
}
return NULL;
}
