public function DonationForm::buildForm in Commerce Donate 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ DonationForm.php, line 87
Class
- DonationForm
- Provides the donation form.
Namespace
Drupal\commerce_donate\FormCode
public function buildForm(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();
$predefined_amounts = [
'50' => $currency_symbol . '50',
'100' => $currency_symbol . '100',
'250' => $currency_symbol . '250',
];
$predefined_amount_keys = array_keys($predefined_amounts);
$selected_amount = reset($predefined_amount_keys);
$form['frequency'] = [
'#type' => 'radios',
'#options' => [
'onetime' => t('Single Donation'),
'monthly' => t('Monthly Donation'),
],
'#default_value' => 'onetime',
'#required' => TRUE,
];
$form['amount_onetime'] = [
'#type' => 'fieldgroup',
'#states' => [
'visible' => [
':input[name="frequency"]' => [
'value' => 'onetime',
],
],
],
'#attributes' => [
'class' => [
'form-item-amount-other',
'js-form-item-amount-other',
'fieldgroup',
],
],
];
$form['amount_onetime']['amount_onetime'] = [
'#type' => 'select_or_other_buttons',
'#title' => t('I would like to Donate'),
'#options' => $predefined_amounts,
'#default_value' => $selected_amount,
'#required' => TRUE,
'#description' => t('If you donate €250 per year or more, we can claim back tax from the Revenue Commissioners and help even more people with your donation.'),
];
$predefined_amounts = [
'10' => $currency_symbol . '10',
'21' => $currency_symbol . '21',
'50' => $currency_symbol . '50',
];
$predefined_amount_keys = array_keys($predefined_amounts);
$form['amount_monthly'] = [
'#type' => 'fieldgroup',
'#states' => [
'visible' => [
':input[name="frequency"]' => [
'value' => 'monthly',
],
],
],
'#attributes' => [
'class' => [
'form-item-amount-other',
'js-form-item-amount-other',
'fieldgroup',
],
],
];
$form['amount_monthly']['amount_monthly'] = [
'#type' => 'select_or_other_buttons',
'#title' => t('I would like to Donate'),
'#options' => $predefined_amounts,
'#default_value' => $selected_amount,
'#required' => TRUE,
'#description' => t('If you donate €250 per year or more, we can claim back tax from the Revenue Commissioners and help even more people with your donation.'),
'#states' => [
'visible' => [
':input[name="frequency"]' => [
'value' => 'monthly',
],
],
],
];
$form['in_memory'] = [
'#type' => 'checkbox',
'#title' => t('I wish to make this donation in memory of someone'),
'#default_value' => FALSE,
'#states' => [
'visible' => [
':input[name="frequency"]' => [
'value' => 'onetime',
],
],
],
];
$form['in_memory_name'] = [
'#type' => 'textfield',
'#title' => t('Donate in memory of'),
'#placeholder' => t("Enter person's name"),
'#states' => [
'visible' => [
':input[name="in_memory"]' => [
'checked' => TRUE,
],
],
],
];
$form['in_memory_memorial'] = [
'#type' => 'checkbox',
'#title' => t('Receive an In Memory Card.'),
'#default_value' => FALSE,
'#states' => [
'visible' => [
':input[name="in_memory"]' => [
'checked' => TRUE,
],
],
],
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Continue'),
'#button_type' => 'primary',
];
return $form;
}