You are here

OrderReceiptResendForm.php in Commerce Core 8.2

File

modules/order/src/Form/OrderReceiptResendForm.php
View source
<?php

namespace Drupal\commerce_order\Form;

use Drupal\commerce_order\Mail\OrderReceiptMailInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a confirmation form for resending order receipts.
 */
class OrderReceiptResendForm extends ContentEntityConfirmFormBase {

  /**
   * The order receipt mail.
   *
   * @var \Drupal\commerce_order\Mail\OrderReceiptMailInterface
   */
  protected $orderReceiptMail;

  /**
   * Constructs a new OrderReceiptResendForm object.
   *
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\commerce_order\Mail\OrderReceiptMailInterface $order_receipt_mail
   *   The order receipt mail service.
   */
  public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, OrderReceiptMailInterface $order_receipt_mail) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->orderReceiptMail = $order_receipt_mail;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.repository'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('datetime.time'), $container
      ->get('commerce_order.order_receipt_mail'));
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Are you sure you want to resend the receipt for order %label?', [
      '%label' => $this->entity
        ->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Resend receipt');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return $this->entity
      ->toUrl('collection');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $this->entity;
    $result = $this->orderReceiptMail
      ->send($order);

    // Drupal's MailManager sets an error message itself, if the sending failed.
    if ($result) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Order receipt resent.'));
    }
  }

}

Classes

Namesort descending Description
OrderReceiptResendForm Provides a confirmation form for resending order receipts.