我正在尝试向愿望清单共享表单添加 2 个额外的表单字段,用户输入将在电子邮件中呈现。我已经能够将字段添加到表单中,但我不确定如何在电子邮件树枝模板中添加用户的输入。
以下是我更新 form() 函数的方法:
public function form(array $form, FormStateInterface $form_state) {
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
// Workaround for core bug #2897377.
$form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']);
$form['to'] = [
'#type' => 'email',
'#title' => $this->t('Recipient Email'),
'#required' => TRUE,
];
// COMBAK my edit
$form['sender_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Your Name'),
'#required' => FALSE,
];
$form['sender_message'] = [
'#type' => 'textarea',
'#title' => $this->t('Your Message'),
'#required' => FALSE,
];
// COMBAK eo my edit
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Send email'),
'#submit' => ['::submitForm'],
];
if ($this->isAjax()) {
$actions['submit']['#ajax']['callback'] = '::ajaxSubmit';
}
return $actions;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_wishlist\Entity\WishlistInterface $wishlist */
$wishlist = $this->entity;
$to = $form_state->getValue('to');
// COMBAK: my added vars
$sender_name = $form_state->getValue('sender_name');
$sender_message = $form_state->getValue('sender_message');
$this->wishlistShareMail->send($wishlist, $to, $sender_name, $sender_message);
$this->messenger()->addStatus($this->t('Shared the wishlist to @recipient.', [
'@recipient' => $to,
]));
$form_state->setRedirectUrl($wishlist->toUrl('user-form'));
}
这是调用我已更新的 mailHandler 的函数:
public function send(WishlistInterface $wishlist, $to, $sender_name, $sender_message) {
$owner = $wishlist->getOwner();
$subject = $this->t('Check out my @site-name wishlist', [
'@site-name' => $this->configFactory->get('system.site')->get('name'),
]);
$body = [
'#theme' => 'commerce_wishlist_share_mail',
'#wishlist_entity' => $wishlist,
// COMBAK: my added vars
'#sender_name' => $sender_name,
'#sender_message' => $sender_message,
];
$params = [
'id' => 'wishlist_share',
'from' => $owner->getEmail(),
'wishlist' => $wishlist,
];
return $this->mailHandler->sendMail($to, $subject, $body, $params);
}
这是 commerce Wishlist 模块提供的 preprocees 函数:
function template_preprocess_commerce_wishlist_share_mail(array &$variables) {
/** @var Drupal\commerce_wishlist\Entity\WishlistInterface $wishlist */
$wishlist = $variables['wishlist_entity'];
$wishlist_url = $wishlist->toUrl('canonical', ['absolute' => TRUE]);
$variables['wishlist_url'] = $wishlist_url->toString();
// COMBAK: my added vars
//$sender_name = $variables['sender_name'];
}
最后是电子邮件本身的树枝模板:
{#
/**
* @file
* Template for the wishlist share email.
*
* Available variables:
* - wishlist_entity: The wishlist entity.
* - wishlist_url: The wishlist url.
*
* @ingroup themeable
*/
#}
<p>
{% trans %}Check out my wishlist!{% endtrans %}
</p>
<p>
{% trans %}I use my wishlist for keeping track of items I am interested in.{% endtrans %} <br>
{% trans %}To see the list in the store and buy items from it, <a href="{{ wishlist_url }}">click here</a>.{% endtrans %}
</p>
<p>
{% trans %}Thanks for having a look!{% endtrans %}
</p>
我无法弄清楚如何访问我添加到 twig 模板中 body[] 数组的变量。
任何帮助将不胜感激。
谢谢!