You are here

public function RegcodeAdminCreateForm::buildForm in Registration codes 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/RegcodeAdminCreateForm.php, line 23

Class

RegcodeAdminCreateForm
Form for creation of registration codes.

Namespace

Drupal\regcode\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $regcode_config = $this
    ->config('regcode.settings');

  // Basics.
  $form = [];
  $form['regcode_create'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('General settings'),
  ];
  $form['regcode_create']['regcode_create_code'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Registration code'),
    '#description' => $this
      ->t('Leave blank to have code generated. Used as prefix when <em>Number of codes</em> is greater than 1.'),
  ];
  $form['regcode_create']['regcode_create_maxuses'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Maximum uses'),
    '#description' => $this
      ->t('How many times this code can be used to register (enter 0 for unlimited).'),
    '#size' => 10,
    '#default_value' => 1,
    '#required' => TRUE,
  ];
  $form['regcode_create']['regcode_create_length'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Code size'),
    '#size' => 10,
    '#default_value' => 12,
  ];
  $form['regcode_create']['regcode_create_format'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Format of the generated codes'),
    '#options' => [
      'alpha' => $this
        ->t('Letters'),
      'numeric' => $this
        ->t('Numbers'),
      'alphanum' => $this
        ->t('Numbers & Letters'),
      'hexadec' => $this
        ->t('Hexadecimal'),
    ],
    '#default_value' => $regcode_config
      ->get('regcode_generate_format'),
  ];
  $form['regcode_create']['regcode_create_case'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Convert generated codes to uppercase'),
    '#default_value' => $regcode_config
      ->get('regcode_generate_case'),
  ];
  $form['regcode_create']['regcode_create_begins'] = [
    '#type' => 'date',
    '#title' => $this
      ->t('Active from'),
    '#description' => $this
      ->t('When this code should activate (leave blank to activate immediately). Accepts any date format that strtotime can handle.'),
    '#default_value' => [
      'day' => 0,
      'month' => 0,
      'year' => 0,
    ],
    '#element_validate' => [
      [
        $this,
        'validateDate',
      ],
    ],
  ];
  $form['regcode_create']['regcode_create_expires'] = [
    '#type' => 'date',
    '#title' => $this
      ->t('Expires on'),
    '#description' => $this
      ->t('When this code should expire (leave blank for no expiry). Accepts any date format that strtotime can handle.'),
    '#default_value' => [
      'day' => 0,
      'month' => 0,
      'year' => 0,
    ],
    '#element_validate' => [
      [
        $this,
        'validateDate',
      ],
    ],
  ];

  // Bulk.
  $form['regcode_create_bulk'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Bulk settings'),
    '#description' => $this
      ->t('Multiple codes can be created at once, use these settings to configure the code generation.'),
  ];
  $form['regcode_create_bulk']['regcode_create_number'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Number of codes to generate'),
    '#size' => 10,
    '#default_value' => 1,
  ];
  $form['regcode_create_bulk_submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Create codes'),
  ];
  return $form;
}