You are here

public function WebformInvitationCodesForm::buildForm in Webform Invitation 8

Same name and namespace in other branches
  1. 2.0.x src/Form/WebformInvitationCodesForm.php \Drupal\webform_invitation\Form\WebformInvitationCodesForm::buildForm()

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/WebformInvitationCodesForm.php, line 53

Class

WebformInvitationCodesForm
Provides list of all invitation codes for the current webform.

Namespace

Drupal\webform_invitation\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL) {
  $webform_id = $webform
    ->id();

  // Get all codes from DB table.
  $query = $this->database
    ->select('webform_invitation_codes', 'c')
    ->fields('c');
  $query
    ->condition('webform', $webform_id);
  $codes = $query
    ->execute()
    ->fetchAll();
  $rows = [];

  // Create table if there are some codes.
  foreach ($codes as $row) {
    $rows[] = [
      'code' => $row->code,
      'used' => empty($row->used) ? $this
        ->t('no') : $this
        ->t('yes'),
      'sid' => empty($row->sid) ? '' : Link::createFromRoute($row->sid, 'entity.webform_submission.canonical', [
        'webform' => $webform_id,
        'webform_submission' => $row->sid,
      ]),
    ];
  }
  $form['webform_invitation'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Webform Invitation'),
    '#open' => TRUE,
  ];
  $form['webform_invitation']['codes'] = [
    '#type' => 'table',
    '#header' => [
      'code' => $this
        ->t('Code'),
      'used' => $this
        ->t('Used'),
      'sid' => $this
        ->t('Submission ID'),
    ],
    '#rows' => $rows,
    '#empty' => $this
      ->t('No codes present, yet. Click on "Generate" above to create codes.'),
  ];
  return $form;
}