Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Hook; | |
| 6 | ||
| 7 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 8 | use Drupal\Core\Form\FormStateInterface; | |
| 9 | use Drupal\Core\Hook\Attribute\Hook; | |
| 10 | use Drupal\Core\StringTranslation\StringTranslationTrait; | |
| 11 | use Drupal\Core\StringTranslation\TranslationInterface; | |
| 12 | use Drupal\user\UserDataInterface; | |
| 13 | use Drupal\visitors\VisitorsVisibilityInterface; | |
| 14 | ||
| 15 | /** | |
| 16 | * Implements hook_form_user_form_alter(). | |
| 17 | */ | |
| 18 | class UserHooks { | |
| 19 | use StringTranslationTrait; | |
| 20 | ||
| 21 | /** | |
| 22 | * Constructs a UserHooks object. | |
| 23 | * | |
| 24 | * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | |
| 25 | * The config factory. | |
| 26 | * @param \Drupal\user\UserDataInterface $userData | |
| 27 | * The user data service. | |
| 28 | * @param \Drupal\Core\StringTranslation\TranslationInterface $translation | |
| 29 | * The string translation service. | |
| 30 | */ | |
| 31 | public function __construct( | |
| 32 | protected ConfigFactoryInterface $configFactory, | |
| 33 | protected UserDataInterface $userData, | |
| 34 | TranslationInterface $translation, | |
| 35 | ) { | |
| 36 | 6 | $this->setStringTranslation($translation); |
| 37 | } | |
| 38 | ||
| 39 | /** | |
| 40 | * Implements hook_form_FORM_ID_alter(). | |
| 41 | * | |
| 42 | * Allow users to decide if tracking code will be added to pages or not. | |
| 43 | */ | |
| 44 | #[Hook('form_user_form_alter')] | |
| 45 | public function formUserFormAlter(&$form, FormStateInterface $form_state) { | |
| 46 | 6 | $config = $this->configFactory->get('visitors.settings'); |
| 47 | 6 | $visibility_users = $config->get('visibility.user_account_mode'); |
| 48 | 6 | if ($visibility_users == VisitorsVisibilityInterface::USER_NO_PERSONALIZATION) { |
| 49 | 1 | return; |
| 50 | } | |
| 51 | /** @var \Drupal\user\AccountForm $user_form */ | |
| 52 | 5 | $user_form = $form_state->getFormObject(); |
| 53 | /** @var \Drupal\user\UserInterface $account */ | |
| 54 | 5 | $account = $user_form->getEntity(); |
| 55 | 5 | if (!$account->hasPermission('opt-out of visitors tracking')) { |
| 56 | 1 | return; |
| 57 | } | |
| 58 | 4 | $account_data_visitors = $this->userData->get('visitors', $account->id()); |
| 59 | 4 | $form['visitors'] = [ |
| 60 | 4 | '#type' => 'details', |
| 61 | 4 | '#title' => $this->t('Visitors settings'), |
| 62 | 4 | '#weight' => 3, |
| 63 | 4 | '#open' => TRUE, |
| 64 | 4 | ]; |
| 65 | 4 | $description = ''; |
| 66 | switch ($visibility_users) { | |
| 67 | 4 | case VisitorsVisibilityInterface::USER_OPT_OUT: |
| 68 | 2 | $description = $this->t('Users are tracked by default, but you are able to opt out.'); |
| 69 | 2 | break; |
| 70 | ||
| 71 | 2 | case VisitorsVisibilityInterface::USER_OPT_IN: |
| 72 | 2 | $description = $this->t('Users are <em>not</em> tracked by default, but you are able to opt in.'); |
| 73 | break; | |
| 74 | } | |
| 75 | 4 | $form['visitors']['user_account_users'] = [ |
| 76 | 4 | '#type' => 'checkbox', |
| 77 | 4 | '#title' => $this->t('Enable user tracking'), |
| 78 | 4 | '#description' => $description, |
| 79 | 4 | '#default_value' => $account_data_visitors['user_account_users'] ?? $visibility_users, |
| 80 | 4 | ]; |
| 81 | // Custom submit handler. | |
| 82 | 4 | $form['actions']['submit']['#submit'][] = 'visitors_user_profile_form_submit'; |
| 83 | } | |
| 84 | ||
| 85 | } |