PersonalInformationFormTrait.php in EU Cookie Compliance (GDPR Compliance) 2.0.x
File
src/PersonalInformationFormTrait.php
View source
<?php
namespace Drupal\eu_cookie_compliance;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
trait PersonalInformationFormTrait {
use StringTranslationTrait;
protected $configFactory;
protected $consentStorageManager;
public function formInjectGdprCheckbox(array &$form, FormStateInterface $form_state) {
$gdpr_checkbox = [];
$gdpr_checkbox['eu_compliance_cookie'] = [
'#type' => 'checkbox',
'#title' => $this
->getGdprWording(),
'#required' => TRUE,
];
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;
}
}
public function formSubmitGdprCheckbox(array $form, FormStateInterface $form_state) {
if ($form_state
->getValue('eu_compliance_cookie') && $this
->getConsentStorage()) {
$this
->getConsentStorage()
->registerConsent($this
->getFormId());
}
}
public abstract function getFormId();
protected function getGdprWording() {
$popup_link = $this
->getConfig('eu_cookie_compliance.settings')
->get('popup_link');
if (UrlHelper::isExternal($popup_link)) {
$popup_link = Url::fromUri($popup_link);
}
else {
$popup_link = $popup_link === '<front>' ? '/' : $popup_link;
$popup_link = Url::fromUserInput($popup_link);
}
return $this
->t('I accept processing of my personal data. For more information, read the <a href="@url">privacy policy</a>,', [
'@url' => $popup_link
->toString(),
]);
}
protected function getConfig($config_name) {
return $this->configFactory ? $this->configFactory
->get($config_name) : \Drupal::config($config_name);
}
protected function getConsentStorage() {
$storage_manager = $this->consentStorageManager ? $this->consentStorageManager : \Drupal::service('plugin.manager.eu_cookie_compliance.consent_storage');
$active_storage = $this
->getConfig('eu_cookie_compliance.settings')
->get('consent_storage_method');
if ($active_storage && $storage_manager
->hasDefinition($active_storage)) {
return $storage_manager
->createInstance($active_storage);
}
return NULL;
}
}