You are here

public function Donation::buildPaneForm in Commerce Donate 8

Builds the pane form.

Parameters

array $pane_form: The pane form, containing the following basic properties:

  • #parents: Identifies the position of the pane form in the overall parent form, and identifies the location where the field values are placed within $form_state->getValues().

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the parent form.

array $complete_form: The complete form structure.

Overrides CheckoutPaneInterface::buildPaneForm

File

src/Plugin/Commerce/CheckoutPane/Donation.php, line 58

Class

Donation
Provides the donation pane.

Namespace

Drupal\commerce_donate\Plugin\Commerce\CheckoutPane

Code

public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
  $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();
  $predefined_amounts = [
    '50' => $currency_symbol . '50',
    '100' => $currency_symbol . '100',
    '250' => $currency_symbol . '250',
  ];
  $predefined_amount_keys = array_keys($predefined_amounts);
  $order_item = $this
    ->getOrderItem();
  $unit_price = $order_item
    ->getUnitPrice();
  $amount = $unit_price ? Calculator::trim($unit_price
    ->getNumber()) : reset($predefined_amount_keys);
  $pane_form['donation'] = [
    '#type' => 'checkbox',
    '#title' => t('I would like to make a donation'),
    '#default_value' => $unit_price ? '1' : '0',
  ];
  $pane_form['details'] = [
    '#type' => 'fieldset',
    '#states' => [
      'visible' => [
        ':input[name="donation[donation]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $pane_form['details']['amount'] = [
    '#type' => 'select_or_other_buttons',
    '#title' => t('I would like to Donate'),
    '#options' => $predefined_amounts,
    '#default_value' => $amount,
    '#required' => TRUE,
  ];
  $pane_form['details']['in_memory'] = [
    '#type' => 'checkbox',
    '#title' => t('I wish to make this donation in memory of someone'),
    '#default_value' => $order_item->field_in_memory->value,
  ];
  $pane_form['details']['in_memory_name'] = [
    '#type' => 'textfield',
    '#title' => t('Donate in memory of'),
    '#placeholder' => t("Enter person's name"),
    '#default_value' => $order_item->field_in_memory_name->value,
    '#states' => [
      'visible' => [
        ':input[name="donation[details][in_memory]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $pane_form['details']['in_memory_memorial'] = [
    '#type' => 'checkbox',
    '#title' => t('Receive an In Memory Card.'),
    '#default_value' => $order_item->field_in_memory_memorial->value,
    '#states' => [
      'visible' => [
        ':input[name="donation[details][in_memory]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  return $pane_form;
}