class DataPolicyAgreement in Data Policy 8
Class DataPolicyAgreement.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\data_policy\Form\DataPolicyAgreement
Expanded class hierarchy of DataPolicyAgreement
1 string reference to 'DataPolicyAgreement'
File
- src/
Form/ DataPolicyAgreement.php, line 20
Namespace
Drupal\data_policy\FormView source
class DataPolicyAgreement extends FormBase {
/**
* The Data Policy consent manager.
*
* @var \Drupal\data_policy\DataPolicyConsentManagerInterface
*/
protected $dataPolicyConsentManager;
/**
* The redirect destination helper.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $destination;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* DataPolicyAgreement constructor.
*
* @param \Drupal\data_policy\DataPolicyConsentManagerInterface $data_policy_manager
* The Data Policy consent manager.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $destination
* The redirect destination helper.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(DataPolicyConsentManagerInterface $data_policy_manager, RedirectDestinationInterface $destination, DateFormatterInterface $date_formatter) {
$this->dataPolicyConsentManager = $data_policy_manager;
$this->destination = $destination;
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('data_policy.manager'), $container
->get('redirect.destination'), $container
->get('date.formatter'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'data_policy_data_policy_agreement';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue('data_policy');
$result = [];
foreach ($values as $name => $value) {
$result[$name] = [
'value' => $value,
'required' => $form['account']['data_policy'][$name]['#required'],
'entity_id' => (int) filter_var($name, FILTER_SANITIZE_NUMBER_INT),
'state' => !empty($values[$name]),
];
}
$this->dataPolicyConsentManager
->saveConsent($this
->currentUser()
->id(), 'submit', $result);
foreach ($result as $item) {
// If the user does not agree and it is enforced, we will redirect to
// the cancel account page.
if ($item['required'] && !$item['value']) {
$this
->getRequest()->query
->remove('destination');
$form_state
->setRedirect('entity.user.cancel_form', [
'user' => $this
->currentUser()
->id(),
]);
}
}
// If the user agrees or does not agree (but it is not enforced), check if
// we should redirect to the front page.
if ($this->destination
->get() === '/data-policy-agreement') {
$form_state
->setRedirect('<front>');
}
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$errors = $form_state
->getErrors();
if (!empty($errors)) {
$form_state
->clearErrors();
foreach ($errors as $id => $error) {
/** @var \Drupal\Core\StringTranslation\TranslatableMarkup $g */
if (strpos($id, 'data_policy') !== FALSE) {
$name = Markup::create($error
->getArguments()['@name']);
$form_state
->setErrorByName($id, t('@name field is required.', [
'@name' => $name,
]));
continue;
}
$form_state
->setErrorByName($id, $error);
}
}
parent::validateForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DataPolicyAgreement:: |
protected | property | The Data Policy consent manager. | |
DataPolicyAgreement:: |
protected | property | The date formatter service. | |
DataPolicyAgreement:: |
protected | property | The redirect destination helper. | |
DataPolicyAgreement:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
DataPolicyAgreement:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
DataPolicyAgreement:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
DataPolicyAgreement:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
DataPolicyAgreement:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
DataPolicyAgreement:: |
public | function | DataPolicyAgreement constructor. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |