<?php

/**
 * @file
 * Contains social_gdpr.module.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function social_gdpr_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the social_gdpr module.
    case 'help.page.social_gdpr':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Integrate Data Policy module.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function social_gdpr_form_data_policy_data_policy_agreement_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['not_agree'])) {
    $form['not_agree']['#tag'] = 'small';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Move the GDPR checkbox into the account fieldset if it's available.
 */
function social_gdpr_form_user_register_form_alter(&$form, FormStateInterface $form_state) {
  if (isset($form['data_policy'], $form['account'])) {
    $form['account']['data_policy'] = $form['data_policy'];
    unset($form['data_policy']);

    // Provide it with a weight so it'll sit at the bottom relative to the other
    // user registration form fields.
    $form['account']['data_policy']['#weight'] = 100;
  }
}

/**
 * Implements hook_social_user_account_header_account_links().
 */
function social_gdpr_social_user_account_header_account_links(array $context): array {
  if (\Drupal::service('data_policy.manager')->needConsent()) {
    return [
      'data_policy_agreement' => [
        '#type' => 'link',
        '#attributes' => [
          'title' => new TranslatableMarkup('Data protection statement'),
        ],
        '#url' => Url::fromRoute('data_policy.data_policy.agreement'),
        '#title' => new TranslatableMarkup('Data protection'),
        '#weight' => 1500,
      ],
    ];
  }

  return [];
}
