0

I am trying to add 2 additional form fields to the Wishlist Share form where the user input will be rendered in the email. I have been able to add the fields to the form, but I am not sure how to add the user's input in the email twig template.

Here is how I have updated the form() function:

  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'));
  }

This is the function that calls the mailHandler that I have updated:

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);
  }

And this is the preprocees function provided by the commerce wishlist module:

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'];
}

And finally the twig template for the email itself:

{#
/**
 * @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>

I haven't been able to figure out how to access the variables I added to the body[] array in the twig template.

Any help would be greatly appreciated.

Thanks!

1 Answer 1

0

This is probably not the right way to do it, but I included the form data in the $_SESSION variable as I could not find a more appropriate way.

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 CODE
    $_SESSION['sender_data'] = [];

    $sender_data = [
        '#theme' => 'commerce_wishlist_share_mail',
        '#sender_name' => $form_state->getValue('sender_name'),
        '#sender_message' => $form_state->getValue('sender_message')
    ];

    $_SESSION['sender_data'] = $sender_data;
    // COMBAK: EO MY CODE

    $this->wishlistShareMail->send($wishlist, $to);

    $this->messenger()->addStatus($this->t('Shared the wishlist to @sender_name at the following email address: @recipient.', [
      '@recipient' => $to,
      '@sender_name' => $sender_data['#sender_name']
    ]));
    $form_state->setRedirectUrl($wishlist->toUrl('user-form'));
  }
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 CODE
  $sender_data = $_SESSION['sender_data'];
  $variables['sender_name'] = $sender_data['#sender_name'];
  $variables['sender_message'] = $sender_data['#sender_message'];
  $_SESSION['sender_data'] = [];
  // COMBAK: EO MY CODE
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.