Webform-尝试传递值时断开的链接

问题描述 投票:0回答:1

我有一个自定义模块,该模块从表单获取电子邮件地址,并在电子邮件处理程序中插入一个链接(通过令牌),该链接在单击时将转到另一个Web表单,并在提交时将回复发送到原始电子邮件地址。

我想做的是将提交的值从第一个表单传递到此链接中,但是它不起作用:

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter() for webform.
 *
 * Add the custom validation handler to the 'webform-1'.
 */
function webformcustom_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'webform_submission_get_offers_add_form') {
    $form['#validate'][] = 'webformcustom_validator';
  }
}

/**
 * The custom validation handler.
 *
 * Get an email of a user, encrypt it and insert it
 * into the body of the notification email.
 * Generate a random token and store it with a user's email as a 'key'/'value'
 * using State API.
 * A token that is added to the link on the webform_2 as the argument,
 * like this: http://drupal8.dev/form/webform-2?token=fb41566252aec769.
 * This token then will be using in the hook_preprocess_HOOK() for extraction
 * of a user's email.
 */
function webformcustom_validator(&$form, FormStateInterface $form_state) {
  $email = $form_state->getValue('email_address');


  $webform = \Drupal::entityTypeManager()
    ->getStorage('webform')
    ->load('get_offers');

  $email_handler = $webform->getHandler('email');
  $configuration = $email_handler->getConfiguration();
  // Generate a random token.
  $token = bin2hex(openssl_random_pseudo_bytes(8));
  \Drupal::state()->set('webformcustom_' . $token, $email);

  $url = \Drupal::urlGenerator()->generateFromRoute('<front>', [], ['absolute' => TRUE]);
  $url .= 'provide-a-quote/?select_a_car=[webform-submission:values:select_a_car]&token=' . $token;

  // Insert the link to the 'webform-2' with the token into the email body.
  $configuration['settings']['body'] = '<a href=' . $url . '>Submit offer</a>';
  $email_handler->setConfiguration($configuration);
}

/**
 * Implements hook_preprocess_HOOK() for webform templates.
 *
 * Read the token from the url and extract related a user's email.
 * Then set a user's email in to the email handler configuration.
 * This enables to notify a user when the 'webform_2' will be submit.
 */
function webformcustom_preprocess_webform(&$variables) {
  if ($variables['element']['#webform_id'] == 'provide_a_quote') {

    $token = \Drupal::request()->query->get('token');
    $email = \Drupal::state()->get('webformcustom_' . $token);

    $webform = \Drupal::entityTypeManager()
      ->getStorage('webform')
      ->load('provide_a_quote');

    $email_handler = $webform->getHandler('email');
    $configuration = $email_handler->getConfiguration();

    $configuration['settings']['to_mail'] = $email;
    $email_handler->setConfiguration($configuration);
    $webform->save();
  }
}

作为电子邮件上的链接,它给我的是断开的链接:

Audi?token = 01f89ea29aa5251f>从2到Web的链接

drupal-webform
1个回答
0
投票

我不确定,但似乎缺少一些引号。您可以尝试这种修改:

© www.soinside.com 2019 - 2024. All rights reserved.