WebformInvitationCodesForm.php in Webform Invitation 8
File
src/Form/WebformInvitationCodesForm.php
View source
<?php
namespace Drupal\webform_invitation\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformInvitationCodesForm extends FormBase {
protected $database;
public function getFormId() {
return 'webform_invitation_codes_form';
}
public function __construct(Connection $database) {
$this->database = $database;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL) {
$webform_id = $webform
->id();
$query = $this->database
->select('webform_invitation_codes', 'c')
->fields('c');
$query
->condition('webform', $webform_id);
$codes = $query
->execute()
->fetchAll();
$rows = [];
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;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}