trait PersonalInformationFormTrait in EU Cookie Compliance (GDPR Compliance) 8
Same name and namespace in other branches
- 2.0.x src/PersonalInformationFormTrait.php \Drupal\eu_cookie_compliance\PersonalInformationFormTrait
Trait that implements PersonalInformationFormInterface.
Hierarchy
- trait \Drupal\eu_cookie_compliance\PersonalInformationFormTrait uses StringTranslationTrait
File
- src/
PersonalInformationFormTrait.php, line 13
Namespace
Drupal\eu_cookie_complianceView source
trait PersonalInformationFormTrait {
use StringTranslationTrait;
/**
* Config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Consent storage manager service.
*
* @var \Drupal\eu_cookie_compliance\Plugin\ConsentStorageManagerInterface
*/
protected $consentStorageManager;
/**
* Inject the GDPR checkbox into the form.
*
* @param array $form
* Form structure.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state that accompanies the form.
*/
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;
}
}
/**
* Process form submission in regards to GDPR checkbox.
*
* @param array $form
* Form structure.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state with the submitted values.
*/
public function formSubmitGdprCheckbox(array $form, FormStateInterface $form_state) {
if ($form_state
->getValue('eu_compliance_cookie') && $this
->getConsentStorage()) {
$this
->getConsentStorage()
->registerConsent($this
->getFormId());
}
}
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public abstract function getFormId();
/**
* Translated wording that should accompany the GDPR checkbox.
*
* @return string
* String for the checkbox.
*/
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(),
]);
}
/**
* Get a config with a given name.
*
* @param string $config_name
* Name of the config to get.
*
* @return \Drupal\Core\Config\ImmutableConfig
* Config object.
*/
protected function getConfig($config_name) {
return $this->configFactory ? $this->configFactory
->get($config_name) : \Drupal::config($config_name);
}
/**
* Get active consent storage.
*
* @return null|\Drupal\eu_cookie_compliance\Plugin\ConsentStorageBase
* Consent storage object or NULL if one is not configured/available.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PersonalInformationFormTrait:: |
protected | property | Config factory service. | |
PersonalInformationFormTrait:: |
protected | property | Consent storage manager service. | |
PersonalInformationFormTrait:: |
public | function | Inject the GDPR checkbox into the form. | |
PersonalInformationFormTrait:: |
public | function | Process form submission in regards to GDPR checkbox. | |
PersonalInformationFormTrait:: |
protected | function | Get a config with a given name. | |
PersonalInformationFormTrait:: |
protected | function | Get active consent storage. | |
PersonalInformationFormTrait:: |
abstract public | function | Returns a unique string identifying the form. | |
PersonalInformationFormTrait:: |
protected | function | Translated wording that should accompany the GDPR checkbox. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |