You are here

public function PersonalInformationFormTrait::formInjectGdprCheckbox in EU Cookie Compliance (GDPR Compliance) 8

Same name and namespace in other branches
  1. 2.0.x src/PersonalInformationFormTrait.php \Drupal\eu_cookie_compliance\PersonalInformationFormTrait::formInjectGdprCheckbox()

Inject the GDPR checkbox into the form.

Parameters

array $form: Form structure.

\Drupal\Core\Form\FormStateInterface $form_state: Form state that accompanies the form.

File

src/PersonalInformationFormTrait.php, line 39

Class

PersonalInformationFormTrait
Trait that implements PersonalInformationFormInterface.

Namespace

Drupal\eu_cookie_compliance

Code

public function formInjectGdprCheckbox(array &$form, FormStateInterface $form_state) {
  $gdpr_checkbox = [];
  $gdpr_checkbox['eu_compliance_cookie'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->getGdprWording(),
    '#required' => TRUE,
  ];

  // Try to inject right before the "actions" element. Otherwise just prepend
  // it to the end.
  if (isset($form['actions'])) {
    $actions_index = array_search('actions', array_keys($form));
    $before = array_slice($form, 0, $actions_index, TRUE);
    $after = array_slice($form, $actions_index, NULL, TRUE);
    $form = $before + $gdpr_checkbox + $after;
  }
  else {
    $form += $gdpr_checkbox;
  }
}