View source
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
function data_policy_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.data_policy':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module helps site managers inform users about data collection.') . '</p>';
return $output;
case 'entity.block.edit_form':
if ($route_match
->getRawParameter('block') != 'data_policy') {
break;
}
$url = Link::createFromRoute(t('Data usage explanation'), 'entity.informblock.collection');
return '<p>' . t('Go to @url page for setting block title on specific pages.', [
'@url' => $url
->toString(),
]) . '</p>';
}
}
function data_policy_hook_info() {
$hooks = [
'data_policy_destination_alter',
];
return array_fill_keys($hooks, [
'group' => 'data_policy',
]);
}
function data_policy_theme($existing, $type, $theme, $path) {
return [
'data_policy_data_policy' => [
'render element' => 'element',
'template' => 'data-policy-data-policy',
'variables' => [
'content' => NULL,
],
],
'data_policy_data_policy_revision' => [
'template' => 'data-policy-data-policy-revision',
'variables' => [
'date' => NULL,
'username' => NULL,
'current' => NULL,
'message' => NULL,
],
],
];
}
function data_policy_views_data_alter(array &$data) {
$plugins = [
'data_policy_revision_id' => 'user_consent_data_policy_revision',
'state' => 'user_consent_state',
];
foreach ($plugins as $field => $plugin) {
foreach ([
'field',
'filter',
] as $type) {
$data['user_consent'][$field][$type]['id'] = $plugin;
}
}
}
function data_policy_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$data_policy_manager = \Drupal::service('data_policy.manager');
if (!$data_policy_manager
->isDataPolicy()) {
return;
}
if (\Drupal::routeMatch()
->getRouteName() === 'user.admin_create') {
return;
}
$data_policy_manager
->addCheckbox($form);
$form['actions']['submit']['#submit'][] = '_data_policy_user_register_form_submit';
array_unshift($form['#validate'], '_data_policy_user_register_form_validate');
}
function _data_policy_user_register_form_submit(array &$form, FormStateInterface $form_state) {
$data_policy_manager = \Drupal::service('data_policy.manager');
$values = $form_state
->getValue('data_policy');
$result = [];
foreach ($values as $name => $value) {
$result[$name] = [
'value' => $value,
'entity_id' => (int) filter_var($name, FILTER_SANITIZE_NUMBER_INT),
'state' => !empty($values[$name]),
];
}
if ($uid = $form_state
->getValue('uid')) {
$data_policy_manager
->saveConsent($uid, 'submit', $result);
}
}
function _data_policy_user_register_form_validate(array &$form, FormStateInterface $form_state) {
$errors = $form_state
->getErrors();
if (!empty($errors)) {
$form_state
->clearErrors();
foreach ($errors as $id => $error) {
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);
}
}
}
function data_policy_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity
->getEntityTypeId() === 'data_policy') {
$operations['revisions'] = [
'title' => t('Revisions'),
'weight' => 1,
'url' => $entity
->toUrl('version-history')
->setRouteParameter('entity_id', $entity
->id()),
];
}
return $operations;
}
function data_policy_data_policy_delete(EntityInterface $entity) {
\Drupal::configFactory()
->getEditable('data_policy.data_policy')
->set('entity_id', 0)
->save();
}
function data_policy_preprocess_status_messages(&$variables) {
if (isset($variables['message_list']['error'])) {
$error_messages = $variables['message_list']['error'];
foreach ($error_messages as $delta => $message) {
if (strpos((string) $message, '<a href="#edit-data-policy-data-policy-') !== FALSE) {
$variables['message_list']['error'][$delta] = Markup::create(html_entity_decode((string) $message));
}
}
}
}