You are here

public function AgreementForm::buildForm in Agreement 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/AgreementForm.php \Drupal\agreement\Form\AgreementForm::buildForm()

Build the agreement page.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

\Drupal\agreement\Entity\Agreement|null $agreement: The agreement entity.

Return value

array The form array.

Overrides FormInterface::buildForm

File

src/Form/AgreementForm.php, line 134

Class

AgreementForm
Agreement page form.

Namespace

Drupal\agreement\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Agreement $agreement = NULL) {
  $settings = $agreement
    ->getSettings();
  $agreed = $this->agreementHandler
    ->hasAgreed($agreement, $this->account);
  $canAgree = $this->agreementHandler
    ->canAgree($agreement, $this->account);
  $canRevoke = $this->account
    ->hasPermission('revoke own agreement');
  $form_state
    ->setStorage([
    'agreement' => $agreement,
    'agreed' => $agreed,
  ]);
  $form['agreement'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    'text' => [
      '#type' => 'processed_text',
      '#text' => $agreement
        ->get('agreement'),
      '#format' => $settings['format'],
      '#language' => $this->languageManager
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId(),
    ],
    'agree' => [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('@agree', [
        '@agree' => $settings['checkbox'],
      ]),
      '#default_value' => $agreed,
      '#access' => (!$agreed || $canRevoke) && $canAgree,
      '#parents' => [
        'agree',
      ],
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
    'submit' => [
      '#type' => 'submit',
      '#name' => 'submit',
      '#value' => $this
        ->t('@agree_submit', [
        '@agree_submit' => $settings['submit'],
      ]),
      '#access' => (!$agreed || $canRevoke) && $canAgree,
    ],
  ];
  return $form;
}