class MiniDonationForm in Commerce Donate 8
Provides the donation form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait- class \Drupal\commerce_donate\Form\MiniDonationForm
 
Expanded class hierarchy of MiniDonationForm
1 file declares its use of MiniDonationForm
- MiniDonationBlock.php in src/Plugin/ Block/ MiniDonationBlock.php 
File
- src/Form/ MiniDonationForm.php, line 16 
Namespace
Drupal\commerce_donate\FormView source
class MiniDonationForm extends FormBase {
  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
  /**
   * The cart manager.
   *
   * @var \Drupal\commerce_cart\CartManagerInterface
   */
  protected $cartManager;
  /**
   * The cart provider.
   *
   * @var \Drupal\commerce_cart\CartProviderInterface
   */
  protected $cartProvider;
  /**
   * The current store.
   *
   * @var \Drupal\commerce_store\CurrentStoreInterface
   */
  protected $currentStore;
  /**
   * Constructs a new DonationForm object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce_cart\CartManagerInterface $cart_manager
   *   The cart manager.
   * @param \Drupal\commerce_cart\CartProviderInterface $cart_provider
   *   The cart provider.
   * @param \Drupal\commerce_store\CurrentStoreInterface $current_store
   *   The current store.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, CartManagerInterface $cart_manager, CartProviderInterface $cart_provider, CurrentStoreInterface $current_store) {
    $this->entityTypeManager = $entity_type_manager;
    $this->cartManager = $cart_manager;
    $this->cartProvider = $cart_provider;
    $this->currentStore = $current_store;
  }
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('commerce_cart.cart_manager'), $container
      ->get('commerce_cart.cart_provider'), $container
      ->get('commerce_store.current_store'));
  }
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'mini_donation_form';
  }
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['amount'] = [
      '#type' => 'number',
      '#title' => t('Enter the amount'),
      '#required' => TRUE,
      '#placeholder' => t('Enter amount'),
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Donate Today'),
      '#button_type' => 'primary',
    ];
    $form['frequency'] = [
      '#type' => 'radios',
      '#options' => [
        'onetime' => t('Single Donation'),
        'monthly' => t('Monthly Donation'),
      ],
      '#default_value' => 'onetime',
      '#required' => TRUE,
    ];
    return $form;
  }
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $amount = $form_state
      ->getValue('amount')[0];
    if (!is_numeric($amount)) {
      $form_state
        ->setError($form['amount'], t('The amount must be a valid number.'));
    }
  }
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $current_currency = \Drupal::service('commerce_currency_resolver.current_currency');
    $selected_currency = $current_currency
      ->getCurrency();
    $entity_type_manager = \Drupal::service('entity_type.manager');
    $currency = $entity_type_manager
      ->getStorage('commerce_currency')
      ->load($selected_currency);
    $currency_symbol = $currency
      ->getSymbol();
    $amount = $form_state
      ->getValue('amount');
    $currency_formatter = \Drupal::service('commerce_price.currency_formatter');
    $amount_label = $currency_formatter
      ->format($amount, $selected_currency);
    $donation_freq = $form_state
      ->getValue('frequency');
    // Single donation - add to cart and checkout.
    if ($donation_freq == 'onetime') {
      $donation_order_item = NULL;
      $store = $this->currentStore
        ->getStore();
      $cart = $this->cartProvider
        ->getCart('default', $store);
      if (!$cart) {
        $cart = $this->cartProvider
          ->createCart('default', $store);
      }
      // Try to find an existing order item.
      foreach ($cart
        ->getItems() as $order_item) {
        if ($order_item
          ->bundle() == 'donation') {
          $cart
            ->removeItem($order_item);
          break;
        }
      }
      $order_item = $this->entityTypeManager
        ->getStorage('commerce_order_item')
        ->create([
        'type' => 'donation',
        'title' => t('@amount donation', [
          '@amount' => $amount_label,
        ]),
        'unit_price' => [
          'number' => $amount,
          'currency_code' => $selected_currency,
        ],
        'field_in_memory' => '',
        'field_in_memory_name' => '',
        'field_in_memory_memorial' => '',
      ]);
      $this->cartManager
        ->addOrderItem($cart, $order_item, FALSE);
      // Go to checkout.
      $form_state
        ->setRedirect('commerce_checkout.form', [
        'commerce_order' => $cart
          ->id(),
      ]);
    }
    else {
      $options = [
        'webform' => 'monthly_donation',
        'donate_amount' => $amount,
        'in_memory' => $form_state
          ->getValue('in_memory'),
        'in_memory_name' => $form_state
          ->getValue('in_memory_name'),
        'in_memory_memorial' => $form_state
          ->getValue('in_memory_memorial'),
      ];
      $form_state
        ->setRedirect('entity.webform.canonical', $options);
    }
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| DependencySerializationTrait:: | protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| FormBase:: | protected | property | The config factory. | 1 | 
| FormBase:: | protected | property | The request stack. | 1 | 
| FormBase:: | protected | property | The route match. | |
| FormBase:: | protected | function | Retrieves a configuration object. | |
| FormBase:: | protected | function | Gets the config factory for this form. | 1 | 
| FormBase:: | private | function | Returns the service container. | |
| FormBase:: | protected | function | Gets the current user. | |
| FormBase:: | protected | function | Gets the request object. | |
| FormBase:: | protected | function | Gets the route match. | |
| FormBase:: | protected | function | Gets the logger for a specific channel. | |
| FormBase:: | protected | function | Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: | |
| FormBase:: | public | function | Resets the configuration factory. | |
| FormBase:: | public | function | Sets the config factory for this form. | |
| FormBase:: | public | function | Sets the request stack object to use. | |
| LinkGeneratorTrait:: | protected | property | The link generator. | 1 | 
| LinkGeneratorTrait:: | protected | function | Returns the link generator. | |
| LinkGeneratorTrait:: | protected | function | Renders a link to a route given a route name and its parameters. | |
| LinkGeneratorTrait:: | public | function | Sets the link generator service. | |
| LoggerChannelTrait:: | protected | property | The logger channel factory service. | |
| LoggerChannelTrait:: | protected | function | Gets the logger for a specific channel. | |
| LoggerChannelTrait:: | public | function | Injects the logger channel factory. | |
| MessengerTrait:: | protected | property | The messenger. | 29 | 
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| MiniDonationForm:: | protected | property | The cart manager. | |
| MiniDonationForm:: | protected | property | The cart provider. | |
| MiniDonationForm:: | protected | property | The current store. | |
| MiniDonationForm:: | protected | property | The entity type manager. | |
| MiniDonationForm:: | public | function | Form constructor. Overrides FormInterface:: | |
| MiniDonationForm:: | public static | function | Instantiates a new instance of this class. Overrides FormBase:: | |
| MiniDonationForm:: | public | function | Returns a unique string identifying the form. Overrides FormInterface:: | |
| MiniDonationForm:: | public | function | Form submission handler. Overrides FormInterface:: | |
| MiniDonationForm:: | public | function | Form validation handler. Overrides FormBase:: | |
| MiniDonationForm:: | public | function | Constructs a new DonationForm object. | |
| RedirectDestinationTrait:: | protected | property | The redirect destination service. | 1 | 
| RedirectDestinationTrait:: | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| RedirectDestinationTrait:: | protected | function | Returns the redirect destination service. | |
| RedirectDestinationTrait:: | public | function | Sets the redirect destination service. | |
| StringTranslationTrait:: | protected | property | The string translation service. | 1 | 
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | |
| UrlGeneratorTrait:: | protected | property | The url generator. | |
| UrlGeneratorTrait:: | protected | function | Returns the URL generator service. | |
| UrlGeneratorTrait:: | public | function | Sets the URL generator service. | |
| UrlGeneratorTrait:: | protected | function | Generates a URL or path for a specific route based on the given parameters. | 
