You are here

public function DataPolicyAgreement::buildForm in Data Policy 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/DataPolicyAgreement.php, line 84

Class

DataPolicyAgreement
Class DataPolicyAgreement.

Namespace

Drupal\data_policy\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $this->dataPolicyConsentManager
    ->addCheckbox($form);
  $this->dataPolicyConsentManager
    ->saveConsent($this
    ->currentUser()
    ->id(), 'visit');

  // Add a message that the data policy was updated.
  $entity_ids = $this->dataPolicyConsentManager
    ->getEntityIdsFromConsentText();
  $revisions = $this->dataPolicyConsentManager
    ->getRevisionsByEntityIds($entity_ids);
  $timestamps = array_map(function (DataPolicyInterface $revision) {
    return $revision
      ->getChangedTime();
  }, $revisions);
  $timestamp = max($timestamps);
  $date = $this->dateFormatter
    ->format($timestamp, 'html_date');
  $form['date'] = [
    '#theme' => 'status_messages',
    '#message_list' => [
      'info' => [
        [
          '#type' => 'html_tag',
          '#tag' => 'strong',
          '#value' => $this
            ->t('Our data policy has been updated on %date', [
            '%date' => $date,
          ]),
        ],
      ],
    ],
    '#weight' => -2,
  ];
  if (!empty($this
    ->config('data_policy.data_policy')
    ->get('enforce_consent'))) {
    $form['data_policy']['#weight'] = 1;
    $link = Link::createFromRoute($this
      ->t('the account cancellation'), 'entity.user.cancel_form', [
      'user' => $this
        ->currentUser()
        ->id(),
    ]);
    $form['not_agree'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('Agreement to the data policy is required for continue using this platform. If you do not agree with the data policy, you will be guided to @url process.', [
        '@url' => $link
          ->toString(),
      ]),
      '#theme_wrappers' => [
        'form_element',
      ],
      '#weight' => -1,
    ];
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
  ];
  return $form;
}