Commit 93a6f561 authored by Binny Thomas's avatar Binny Thomas Committed by Binny Thomas
Browse files

Issue #3309986 by binnythomas: Port the affiliate admin configuration content form to Drupal 9

parent 00eb2bb8
Loading
Loading
Loading
Loading
+179 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\uber_affiliate\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Example ajax add remove buttons.
 *
 * This example demonstrates using ajax callbacks to add people's names
 * to a list of picnic attendees with an option to remove specific people.
 */
class PayoutForm extends ConfigFormBase {

  /**
   * Config settings.
   *
   * @var string
   */
  const SETTINGS = 'uber_affiliate.payout.settings';

  /**
   * Required by FormBase.
   */
  public function getFormId() {
    return 'uber_affiliate_payout_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      static::SETTINGS,
    ];
  }

  /**
   * PayoutForm.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['affiliate_module_settings']['payouts'] = array(
      '#type' => 'fieldset',
      '#title' => t('Payouts'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form['affiliate_module_settings']['payouts']['affiliate_module_payouts_symbol'] = array(
      '#type' => 'textfield',
      '#title' => t('Currency symbol'),
      '#default_value' => \Drupal::state()->get('affiliate_module_payouts_symbol', '$'),
      '#description' => t('Enter the symbol used to denote money in your locale. Example: in the US, France, and the UK, the symbols are typically <b>&dollar;</b>, <b>&euro;</b> and <b>&pound;</b>, respectively.'),
      '#size' => 1,
    );
    $form['affiliate_module_settings']['payouts']['affiliate_module_payouts_symbol_placement'] = array(
      '#type' => 'select',
      '#title' => t('Currency symbol placement'),
      '#default_value' => \Drupal::state()->get('affiliate_module_payouts_symbol_placement', 1),
      '#options' => array(1 => t('Before'), 2 => t('After')),
      '#description' => t('Select whether the currency symbol should be displayed before or after the amount.'),
    );
    $form['affiliate_module_settings']['payouts']['affiliate_module_payouts_per_click'] = array(
      '#type' => 'textfield',
      '#title' => t('Payouts per click'),
      '#default_value' => \Drupal::state()->get('affiliate_module_payouts_per_click', 0.00),
      '#description' => t('Enter the amount, using a decimal point as the separator, that each affiliate will receive per click. For example, if the amount is &dollar;0.50 enter <b>0.50</b> in the box. Or, if the amount is 1,07&euro; enter <b>1.07</b> in the box.'),
    );
    $form['affiliate_module_settings']['payouts']['referral'] = array(
      '#type' => 'fieldset',
      '#title' => t('Referrals'),
      '#collapsible' => TRUE,
      '#collapsed' => $referral_form_collapse,
    );
    $form['affiliate_module_settings']['payouts']['referral']['affiliate_module_payouts_per_referral'] = array(
      '#type' => 'textfield',
      '#title' => t('Payouts per referral'),
      '#default_value' => \Drupal::state()->get('affiliate_module_payouts_per_referral', 0.00),
      '#description' => t('Enter the amount, using a decimal point as the separator, that each affiliate will receive per referral. For example, if the amount is &dollar;0.50 enter <b>0.50</b> in the box. Or, if the amount is 1,07&euro; enter <b>1.07</b> in the box. <b>Note: this setting has no effect if the <a href="http://drupal.org/project/referral">Referral</a> module is not installed</b>.'),
    );
    $form['affiliate_module_settings']['payouts']['userpoints'] = array(
      '#type' => 'fieldset',
      '#title' => t('Userpoints'),
      '#collapsible' => TRUE,
      '#collapsed' => $userpoints_form_collapse,
    );
    $form['affiliate_module_settings']['payouts']['userpoints']['affiliate_module_payouts_userpoints_clicks'] = array(
      '#type' => 'textfield',
      '#title' => t('Points per click'),
      '#default_value' => \Drupal::state()->get('affiliate_module_payouts_userpoints_clicks', 0),
      '#description' => t('Enter the number of points each affiliate will receive per click. <b>Note: this setting has no effect if the <a href="http://drupal.org/project/userpoints">Userpoints</a> module is not installed</b>.'),
      '#size' => 1,
      '#disabled' => $userpoints_disabled,
    );
    $form['affiliate_module_settings']['payouts']['userpoints']['affiliate_module_payouts_userpoints_tid'] = array(
      '#type' => 'select',
      '#title' => t('Category'),
      '#default_value' => \Drupal::state()->get('affiliate_module_payouts_userpoints_tid', 0),
      '#options' => $userpoints_options,
      '#description' => t('Points for affilate click-thrus will be assigned to this category. <b>Note: this setting has no effect if the <a href="http://drupal.org/project/userpoints">Userpoints</a> module is not installed</b>.'),
      '#disabled' => $userpoints_disabled,
    );

    return $form;

  }

  /**
   * Callback for both ajax-enabled buttons.
   *
   * Selects and returns the fieldset with the names in it.
   */
  public function addmoreCallback(array &$form, FormStateInterface $form_state) {
    return $form['names_fieldset'];
  }

  /**
   * Submit handler for the "add-one-more" button.
   *
   * Increments the max counter and causes a rebuild.
   */
  public function addOne(array &$form, FormStateInterface $form_state) {
    $num_field = $form_state->get('num_lines');
    $add_button = $num_field + 1;
    $form_state->set('num_lines', $add_button);
    $form_state->setRebuild();
  }

  /**
   * Submit handler for the "remove" button.
   *
   * Removes the corresponding line.
   */
  public function removeCallback(array &$form, FormStateInterface $form_state) {
    /*
     * We use the name of the remove button to find
     * the element we want to remove
     * Line 72: '#name' => $i,.
     */
    $trigger = $form_state->getTriggeringElement();
    $indexToRemove = $trigger['#name'];

    // Remove the fieldset from $form (the easy way)
    unset($form['names_fieldset'][$indexToRemove]);

    // Remove the fieldset from $form_state (the hard way)
    // First fetch the fieldset, then edit it, then set it again
    // Form API does not allow us to directly edit the field.
    $namesFieldset = $form_state->getValue('names_fieldset');
    unset($namesFieldset[$indexToRemove]);
    // $form_state->unsetValue('names_fieldset');
    $form_state->setValue('names_fieldset', $namesFieldset);

    // Keep track of removed fields so we can add new fields at the bottom
    // Without this they would be added where a value was removed.
    $removed_fields = $form_state->get('removed_fields');
    $removed_fields[] = $indexToRemove;
    $form_state->set('removed_fields', $removed_fields);

    // Rebuild form_state.
    $form_state->setRebuild();
  }

  /**
   * Required by FormBase.
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

  }

  /**
   * Required by FormBase.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

  }

}