Lines: 92.67%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Form; | |
| 6 | ||
| 7 | use Drupal\Core\Condition\ConditionManager; | |
| 8 | use Drupal\Core\Config\Entity\ConfigEntityType; | |
| 9 | use Drupal\Core\Entity\EntityTypeManagerInterface; | |
| 10 | use Drupal\Core\Form\ConfigFormBase; | |
| 11 | use Drupal\Core\Form\FormStateInterface; | |
| 12 | use Drupal\Core\Form\SubformState; | |
| 13 | use Drupal\Core\Plugin\Context\ContextRepositoryInterface; | |
| 14 | use Drupal\Core\Url; | |
| 15 | use Drupal\visitors\VisitorsLocationPluginManager; | |
| 16 | use Drupal\visitors\VisitorsVisibilityInterface; | |
| 17 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 18 | ||
| 19 | /** | |
| 20 | * Visitors Settings Form. | |
| 21 | */ | |
| 22 | final class SettingsForm extends ConfigFormBase { | |
| 23 | ||
| 24 | /** | |
| 25 | * Visitors settings. | |
| 26 | * | |
| 27 | * @var string | |
| 28 | */ | |
| 29 | const SETTINGS = 'visitors.settings'; | |
| 30 | ||
| 31 | /** | |
| 32 | * Condition plugins that are not offered for tracking visibility. | |
| 33 | * | |
| 34 | * The theme condition has no empty option, so saving settings would | |
| 35 | * always store a theme and stop tracking on every other theme. | |
| 36 | */ | |
| 37 | private const UNSUPPORTED_VISIBILITY_CONDITIONS = [ | |
| 38 | 'current_theme', | |
| 39 | ]; | |
| 40 | ||
| 41 | /** | |
| 42 | * An extension discovery instance. | |
| 43 | * | |
| 44 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface | |
| 45 | */ | |
| 46 | protected EntityTypeManagerInterface $entityTypeManager; | |
| 47 | ||
| 48 | /** | |
| 49 | * The condition plugin manager. | |
| 50 | * | |
| 51 | * @var \Drupal\Core\Condition\ConditionManager | |
| 52 | */ | |
| 53 | protected ConditionManager $conditionManager; | |
| 54 | ||
| 55 | /** | |
| 56 | * The context repository service. | |
| 57 | * | |
| 58 | * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface | |
| 59 | */ | |
| 60 | protected ContextRepositoryInterface $contextRepository; | |
| 61 | ||
| 62 | /** | |
| 63 | * The location plugin manager. | |
| 64 | * | |
| 65 | * @var \Drupal\visitors\VisitorsLocationPluginManager | |
| 66 | */ | |
| 67 | protected VisitorsLocationPluginManager $locationPluginManager; | |
| 68 | ||
| 69 | /** | |
| 70 | * Constructs a Settings form. | |
| 71 | * | |
| 72 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | |
| 73 | * The entity type manager. | |
| 74 | * @param \Drupal\Core\Condition\ConditionManager $condition_manager | |
| 75 | * The condition plugin manager. | |
| 76 | * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository | |
| 77 | * The context repository. | |
| 78 | * @param \Drupal\visitors\VisitorsLocationPluginManager $location_plugin_manager | |
| 79 | * The location plugin manager. | |
| 80 | */ | |
| 81 | public function __construct( | |
| 82 | EntityTypeManagerInterface $entity_type_manager, | |
| 83 | ConditionManager $condition_manager, | |
| 84 | ContextRepositoryInterface $context_repository, | |
| 85 | VisitorsLocationPluginManager $location_plugin_manager, | |
| 86 | ) { | |
| 87 | 9 | $this->entityTypeManager = $entity_type_manager; |
| 88 | 9 | $this->conditionManager = $condition_manager; |
| 89 | 9 | $this->contextRepository = $context_repository; |
| 90 | 9 | $this->locationPluginManager = $location_plugin_manager; |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * {@inheritdoc} | |
| 95 | */ | |
| 96 | public static function create(ContainerInterface $container) { | |
| 97 | 9 | return new self( |
| 98 | 9 | $container->get('entity_type.manager'), |
| 99 | 9 | $container->get('plugin.manager.condition'), |
| 100 | 9 | $container->get('context.repository'), |
| 101 | 9 | $container->get('visitors.location_plugin_manager'), |
| 102 | 9 | ); |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * {@inheritdoc} | |
| 107 | */ | |
| 108 | public function getFormId() { | |
| 109 | 1 | return 'visitors_admin_settings'; |
| 110 | } | |
| 111 | ||
| 112 | /** | |
| 113 | * {@inheritdoc} | |
| 114 | */ | |
| 115 | protected function getEditableConfigNames() { | |
| 116 | 5 | return [ |
| 117 | 5 | static::SETTINGS, |
| 118 | 5 | ]; |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * {@inheritdoc} | |
| 123 | */ | |
| 124 | public function buildForm(array $form, FormStateInterface $form_state) { | |
| 125 | 3 | $settings = $this->config(self::SETTINGS); |
| 126 | 3 | $form = parent::buildForm($form, $form_state); |
| 127 | ||
| 128 | 3 | $form['#attached']['library'][] = 'visitors/visitors.admin'; |
| 129 | ||
| 130 | 3 | $form['visitors_disable_tracking'] = [ |
| 131 | 3 | '#type' => 'radios', |
| 132 | 3 | '#title' => $this->t('Track visitors'), |
| 133 | 3 | '#options' => [ |
| 134 | 3 | $this->t('Enabled'), |
| 135 | 3 | $this->t('Disabled'), |
| 136 | 3 | ], |
| 137 | 3 | '#description' => $this->t('Enable or disable tracking of visitors.'), |
| 138 | 3 | '#default_value' => (int) $settings->get('disable_tracking'), |
| 139 | 3 | ]; |
| 140 | ||
| 141 | 3 | $script_type = $settings->get('script_type'); |
| 142 | 3 | $form['script_type'] = [ |
| 143 | 3 | '#type' => 'radios', |
| 144 | 3 | '#title' => $this->t('Script type'), |
| 145 | 3 | '#options' => [ |
| 146 | 3 | 'minified' => $this->t('Minified'), |
| 147 | 3 | 'full' => $this->t('Full'), |
| 148 | 3 | ], |
| 149 | 3 | '#default_value' => $script_type == 'full' ? 'full' : 'minified', |
| 150 | 3 | '#description' => $this->t('Full script is for debugging purposes. Minified script is for production.'), |
| 151 | 3 | ]; |
| 152 | 3 | $form['client_debug'] = [ |
| 153 | 3 | '#type' => 'checkbox', |
| 154 | 3 | '#title' => $this->t('Client debug'), |
| 155 | 3 | '#default_value' => $settings->get('client_debug'), |
| 156 | 3 | '#description' => $this->t('Log tracker commands in the browser console. Open the browser developer tools and look at the Console tab.'), |
| 157 | 3 | ]; |
| 158 | 3 | $form['scroll_depth'] = [ |
| 159 | 3 | '#type' => 'checkbox', |
| 160 | 3 | '#title' => $this->t('Record scroll depth'), |
| 161 | 3 | '#default_value' => (bool) $settings->get('scroll_depth'), |
| 162 | 3 | '#description' => $this->t('Record when a visitor reaches 25%, 50%, 75%, and 100% of the page.'), |
| 163 | 3 | ]; |
| 164 | 3 | $form['javascript_errors'] = [ |
| 165 | 3 | '#type' => 'radios', |
| 166 | 3 | '#title' => $this->t('JavaScript errors'), |
| 167 | 3 | '#options' => [ |
| 168 | 3 | 'off' => $this->t('Do not record'), |
| 169 | 3 | 'activity' => $this->t('Activity log'), |
| 170 | 3 | 'drupal' => $this->t('Drupal log'), |
| 171 | 3 | ], |
| 172 | 3 | '#default_value' => $this->javascriptErrorsMode( |
| 173 | 3 | $settings->get('javascript_errors') |
| 174 | 3 | ), |
| 175 | 3 | '#description' => $this->t('Record uncaught browser errors. Activity log stores them with other events. Drupal log writes them to the Visitors log and does not store them.'), |
| 176 | 3 | ]; |
| 177 | 3 | $form['track_downloads'] = [ |
| 178 | 3 | '#type' => 'checkbox', |
| 179 | 3 | '#title' => $this->t('Track file downloads'), |
| 180 | 3 | '#default_value' => (bool) ($settings->get('track_downloads') ?? TRUE), |
| 181 | 3 | '#description' => $this->t('Record clicks on file links, such as PDF and ZIP files.'), |
| 182 | 3 | ]; |
| 183 | 3 | $form['track_outlinks'] = [ |
| 184 | 3 | '#type' => 'checkbox', |
| 185 | 3 | '#title' => $this->t('Track outbound links'), |
| 186 | 3 | '#default_value' => (bool) ($settings->get('track_outlinks') ?? TRUE), |
| 187 | 3 | '#description' => $this->t('Record clicks that leave this site.'), |
| 188 | 3 | ]; |
| 189 | 3 | $form['track_media'] = [ |
| 190 | 3 | '#type' => 'checkbox', |
| 191 | 3 | '#title' => $this->t('Track audio and video'), |
| 192 | 3 | '#default_value' => (bool) ($settings->get('track_media') ?? TRUE), |
| 193 | 3 | '#description' => $this->t('Record HTML5 audio and video plays, pauses, and how much was heard or watched.'), |
| 194 | 3 | ]; |
| 195 | 3 | $form['heartbeat_interval'] = [ |
| 196 | 3 | '#type' => 'number', |
| 197 | 3 | '#title' => $this->t('Heartbeat interval'), |
| 198 | 3 | '#field_suffix' => $this->t('seconds'), |
| 199 | 3 | '#description' => $this->t('How often to record that a page is still open. Use 0 to turn this off. Values from 1 to 4 are saved as 5.'), |
| 200 | 3 | '#default_value' => $this->heartbeatIntervalValue($settings->get('heartbeat_interval')), |
| 201 | 3 | '#min' => 0, |
| 202 | 3 | '#step' => 1, |
| 203 | 3 | ]; |
| 204 | 3 | $form['site_search'] = [ |
| 205 | 3 | '#type' => 'details', |
| 206 | 3 | '#title' => $this->t('Site search'), |
| 207 | 3 | '#open' => FALSE, |
| 208 | 3 | ]; |
| 209 | 3 | $form['site_search']['site_search_views'] = [ |
| 210 | 3 | '#type' => 'textarea', |
| 211 | 3 | '#title' => $this->t('Views to track as search'), |
| 212 | 3 | '#description' => $this->t('One view per line: view id, display id, and the exposed filter that holds the keyword, separated by |. Core Search and Search API are tracked without a line here. A listed display is a search. An empty keyword is not recorded.'), |
| 213 | 3 | '#default_value' => $this->siteSearchViewsText($settings->get('site_search_views')), |
| 214 | 3 | ]; |
| 215 | 3 | $location_options = $this->locationPluginOptions(); |
| 216 | 3 | $location_plugin = $settings->get('location_plugin'); |
| 217 | 3 | $known_plugin = is_string($location_plugin) |
| 218 | 3 | && isset($location_options[$location_plugin]); |
| 219 | 3 | if (!$known_plugin) { |
| 220 | 3 | $location_plugin = isset($location_options['browser_language']) |
| 221 | 3 | ? 'browser_language' |
| 222 | 0 | : (string) array_key_first($location_options); |
| 223 | } | |
| 224 | 3 | $form['location_plugin'] = [ |
| 225 | 3 | '#type' => 'radios', |
| 226 | 3 | '#title' => $this->t('Location'), |
| 227 | 3 | '#options' => $location_options, |
| 228 | 3 | '#default_value' => $location_plugin, |
| 229 | 3 | '#description' => $this->t('How to set the visitor country and city. None stores no location. Browser language uses the region in the first Accept-Language value.'), |
| 230 | 3 | ]; |
| 231 | ||
| 232 | 3 | $form['user_visibility_settings'] = [ |
| 233 | 3 | '#type' => 'markup ', |
| 234 | 3 | '#markup' => '<h2>' . $this->t('User Settings') . '</h2>', |
| 235 | 3 | ]; |
| 236 | ||
| 237 | 3 | $visibility_user_account_mode = $settings->get('visibility.user_account_mode'); |
| 238 | ||
| 239 | 3 | $t_permission = ['%permission' => $this->t('opt-out of visitors tracking')]; |
| 240 | 3 | $form['user_visibility_settings']['visitors_visibility_user_account_mode'] = [ |
| 241 | 3 | '#type' => 'radios', |
| 242 | 3 | '#title' => $this->t('Allow users to customize tracking on their account page'), |
| 243 | 3 | '#options' => [ |
| 244 | 3 | VisitorsVisibilityInterface::USER_NO_PERSONALIZATION => $this->t('No customization allowed'), |
| 245 | 3 | VisitorsVisibilityInterface::USER_OPT_OUT => $this->t('Tracking on by default, users with %permission permission can opt out', $t_permission), |
| 246 | 3 | VisitorsVisibilityInterface::USER_OPT_IN => $this->t('Tracking off by default, users with %permission permission can opt in', $t_permission), |
| 247 | 3 | ], |
| 248 | 3 | '#default_value' => !empty($visibility_user_account_mode) ? $visibility_user_account_mode : 0, |
| 249 | 3 | ]; |
| 250 | 3 | $form['user_visibility_settings']['visitors_trackuserid'] = [ |
| 251 | 3 | '#type' => 'checkbox', |
| 252 | 3 | '#title' => $this->t('Track User ID'), |
| 253 | 3 | '#default_value' => $settings->get('track.userid'), |
| 254 | 3 | '#description' => $this->t('User ID enables the analysis of groups of sessions, across devices, using a unique, persistent, and representing a user. <a href=":url">Learn more about the benefits of using User ID</a>.', [':url' => 'https://matomo.org/docs/user-id/']), |
| 255 | 3 | ]; |
| 256 | 3 | $form['user_visibility_settings']['visitors_userid_anonymize'] = [ |
| 257 | 3 | '#type' => 'checkbox', |
| 258 | 3 | '#title' => $this->t('Pseudo-anonymize User ID'), |
| 259 | 3 | '#default_value' => $settings->get('track.userid_anonymize') ?? FALSE, |
| 260 | 3 | '#description' => $this->t('Store a stable pseudonym instead of the Drupal user id. The same person keeps the same value. Existing visits are left unchanged.'), |
| 261 | 3 | '#states' => [ |
| 262 | 3 | 'visible' => [ |
| 263 | 3 | ':input[name="visitors_trackuserid"]' => ['checked' => TRUE], |
| 264 | 3 | ], |
| 265 | 3 | ], |
| 266 | 3 | ]; |
| 267 | ||
| 268 | 3 | $form['user_visibility_settings']['visibility_exclude_user1'] = [ |
| 269 | 3 | '#type' => 'checkbox', |
| 270 | 3 | '#title' => $this->t('Exclude user1 from statistics'), |
| 271 | 3 | '#default_value' => $settings->get('visibility.exclude_user1'), |
| 272 | 3 | '#description' => $this->t('Exclude hits of user1 from statistics.'), |
| 273 | 3 | ]; |
| 274 | ||
| 275 | 3 | $form['retention'] = [ |
| 276 | 3 | '#type' => 'markup', |
| 277 | 3 | '#markup' => '<h2>' . $this->t('Log Retention') . '</h2>', |
| 278 | 3 | ]; |
| 279 | ||
| 280 | 3 | $form['retention']['flush_log_timer'] = [ |
| 281 | 3 | '#type' => 'select', |
| 282 | 3 | '#title' => $this->t('Discard visitors logs older than'), |
| 283 | 3 | '#default_value' => $settings->get('flush_log_timer'), |
| 284 | 3 | '#options' => [ |
| 285 | 3 | 0 => $this->t('Never'), |
| 286 | 3 | 3600 => $this->t('1 hour'), |
| 287 | 3 | 10800 => $this->t('3 hours'), |
| 288 | 3 | 21600 => $this->t('6 hours'), |
| 289 | 3 | 32400 => $this->t('9 hours'), |
| 290 | 3 | 43200 => $this->t('12 hours'), |
| 291 | 3 | 86400 => $this->t('1 day'), |
| 292 | 3 | 172800 => $this->t('2 days'), |
| 293 | 3 | 259200 => $this->t('3 days'), |
| 294 | 3 | 604800 => $this->t('1 week'), |
| 295 | 3 | 1209600 => $this->t('2 weeks'), |
| 296 | 3 | 4838400 => $this->t('1 month 3 weeks'), |
| 297 | 3 | 9676800 => $this->t('3 months 3 weeks'), |
| 298 | 3 | 31536000 => $this->t('1 year'), |
| 299 | 3 | 34214400 => $this->t('13 months'), |
| 300 | 3 | ], |
| 301 | 3 | '#description' => |
| 302 | 3 | $this->t('Older visitors log entries (including referrer statistics) will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', |
| 303 | 3 | ['@cron' => Url::fromRoute('system.status')->toString()] |
| 304 | 3 | ), |
| 305 | 3 | ]; |
| 306 | ||
| 307 | 3 | $form['retention']['bot_retention_log'] = [ |
| 308 | 3 | '#type' => 'select', |
| 309 | 3 | '#title' => $this->t('Discard bot logs older than'), |
| 310 | 3 | '#default_value' => $settings->get('bot_retention_log'), |
| 311 | 3 | '#options' => [ |
| 312 | 3 | -1 => $this->t('Do not log'), |
| 313 | 3 | 0 => $this->t('Never'), |
| 314 | 3 | 3600 => $this->t('1 hour'), |
| 315 | 3 | 10800 => $this->t('3 hours'), |
| 316 | 3 | 21600 => $this->t('6 hours'), |
| 317 | 3 | 32400 => $this->t('9 hours'), |
| 318 | 3 | 43200 => $this->t('12 hours'), |
| 319 | 3 | 86400 => $this->t('1 day'), |
| 320 | 3 | 172800 => $this->t('2 days'), |
| 321 | 3 | 259200 => $this->t('3 days'), |
| 322 | 3 | 604800 => $this->t('1 week'), |
| 323 | 3 | 1209600 => $this->t('2 weeks'), |
| 324 | 3 | 4838400 => $this->t('1 month 3 weeks'), |
| 325 | 3 | 9676800 => $this->t('3 months 3 weeks'), |
| 326 | 3 | 31536000 => $this->t('1 year'), |
| 327 | 3 | 34214400 => $this->t('13 months'), |
| 328 | 3 | ], |
| 329 | 3 | '#description' => |
| 330 | 3 | $this->t('Control how long or if visits by bots are logged.'), |
| 331 | 3 | ]; |
| 332 | ||
| 333 | 3 | $form['privacy'] = [ |
| 334 | 3 | '#type' => 'markup', |
| 335 | 3 | '#markup' => '<h2>' . $this->t('Privacy') . '</h2>', |
| 336 | 3 | ]; |
| 337 | ||
| 338 | 3 | $form['privacy']['ip_anonymize'] = [ |
| 339 | 3 | '#type' => 'checkbox', |
| 340 | 3 | '#title' => $this->t('Anonymize visitor IP addresses'), |
| 341 | 3 | '#default_value' => $settings->get('ip_anonymize') ?? FALSE, |
| 342 | 3 | '#description' => $this->t('Store a masked IP address instead of the full address. Existing visits are left unchanged.'), |
| 343 | 3 | ]; |
| 344 | ||
| 345 | 3 | $form['privacy']['ip_mask_length'] = [ |
| 346 | 3 | '#type' => 'radios', |
| 347 | 3 | '#title' => $this->t('How much of the IP address to remove'), |
| 348 | 3 | '#options' => [ |
| 349 | 3 | 1 => $this->t('1 octet — 192.168.100.0 (IPv6 /64)'), |
| 350 | 3 | 2 => $this->t('2 octets — 192.168.0.0 (IPv6 /48)'), |
| 351 | 3 | 3 => $this->t('3 octets — 192.0.0.0 (IPv6 /32)'), |
| 352 | 3 | ], |
| 353 | 3 | '#default_value' => (int) ($settings->get('ip_mask_length') ?? 1), |
| 354 | 3 | '#description' => $this->t('Location is looked up from the stored address, so city precision drops as more of the address is removed.'), |
| 355 | 3 | '#states' => [ |
| 356 | 3 | 'visible' => [ |
| 357 | 3 | ':input[name="ip_anonymize"]' => ['checked' => TRUE], |
| 358 | 3 | ], |
| 359 | 3 | ], |
| 360 | 3 | ]; |
| 361 | 3 | $form['privacy']['honor_do_not_track'] = [ |
| 362 | 3 | '#type' => 'checkbox', |
| 363 | 3 | '#title' => $this->t('Honor Do Not Track'), |
| 364 | 3 | '#default_value' => $settings->get('honor_do_not_track') ?? FALSE, |
| 365 | 3 | '#description' => $this->t('Ignore a tracking hit when the browser sends DNT or X-Do-Not-Track.'), |
| 366 | 3 | ]; |
| 367 | 3 | $form['privacy']['cookies'] = [ |
| 368 | 3 | '#type' => 'checkbox', |
| 369 | 3 | '#title' => $this->t('Use analytics cookies'), |
| 370 | 3 | '#default_value' => $settings->get('cookies') ?? FALSE, |
| 371 | 3 | '#description' => $this->t('Off by default. Without cookies, an open visit is matched by browser fingerprint and IP, so people who share a browser and network can be counted as one visitor. IP anonymization makes that fingerprint weaker.'), |
| 372 | 3 | ]; |
| 373 | ||
| 374 | 3 | $form['entity'] = [ |
| 375 | 3 | '#type' => 'markup', |
| 376 | 3 | '#markup' => '<h2>' . $this->t('Entity Views Counter') . '</h2>', |
| 377 | 3 | ]; |
| 378 | ||
| 379 | 3 | $form['entity']['counter_enabled'] = [ |
| 380 | 3 | '#type' => 'checkbox', |
| 381 | 3 | '#title' => $this->t('Count entity views'), |
| 382 | 3 | '#default_value' => $settings->get('counter.enabled'), |
| 383 | 3 | '#description' => $this->t('Count the number of times entities are viewed.'), |
| 384 | 3 | ]; |
| 385 | 3 | $form['entity']['entity_types'] = [ |
| 386 | 3 | '#type' => 'checkboxes', |
| 387 | 3 | '#title' => $this->t('Entity Types'), |
| 388 | 3 | '#options' => $this->entityTypes(), |
| 389 | 3 | '#default_value' => $settings->get('counter.entity_types') ?? [], |
| 390 | 3 | '#description' => $this->t('Which entity types should be tracked.'), |
| 391 | 3 | ]; |
| 392 | ||
| 393 | // Build condition-based visibility UI. | |
| 394 | 3 | $this->buildVisibilityConditions($form, $form_state, $settings); |
| 395 | ||
| 396 | 3 | return $form; |
| 397 | } | |
| 398 | ||
| 399 | /** | |
| 400 | * {@inheritdoc} | |
| 401 | */ | |
| 402 | public function validateForm(array &$form, FormStateInterface $form_state): void { | |
| 403 | 1 | parent::validateForm($form, $form_state); |
| 404 | ||
| 405 | 1 | $mask_length = (int) $form_state->getValue('ip_mask_length'); |
| 406 | 1 | if (!in_array($mask_length, [1, 2, 3], TRUE)) { |
| 407 | 0 | $form_state->setErrorByName('ip_mask_length', $this->t('Choose 1, 2, or 3 octets to remove.')); |
| 408 | } | |
| 409 | ||
| 410 | 1 | $interval = (int) $form_state->getValue('heartbeat_interval'); |
| 411 | 1 | if ($interval < 0) { |
| 412 | 0 | $form_state->setErrorByName('heartbeat_interval', $this->t('The heartbeat interval cannot be negative.')); |
| 413 | } | |
| 414 | 1 | elseif ($interval > 0 && $interval < 5) { |
| 415 | 0 | $form_state->setValue('heartbeat_interval', 5); |
| 416 | } | |
| 417 | ||
| 418 | 1 | $location_plugin = (string) $form_state->getValue('location_plugin'); |
| 419 | 1 | if (!array_key_exists($location_plugin, $this->locationPluginOptions())) { |
| 420 | 0 | $form_state->setErrorByName('location_plugin', $this->t('Choose a location plugin.')); |
| 421 | } | |
| 422 | ||
| 423 | 1 | $site_search = (string) $form_state->getValue('site_search_views'); |
| 424 | 1 | foreach ($this->siteSearchViewLines($site_search) as $line) { |
| 425 | 0 | $parts = array_map('trim', explode('|', $line)); |
| 426 | 0 | $complete = count($parts) === 3 && !in_array('', $parts, TRUE); |
| 427 | 0 | if (!$complete) { |
| 428 | 0 | $form_state->setErrorByName('site_search_views', $this->t('Each line needs a view id, a display id, and a keyword parameter, separated by |.')); |
| 429 | break; | |
| 430 | } | |
| 431 | } | |
| 432 | ||
| 433 | // Validate visibility condition settings. | |
| 434 | 1 | foreach ($form_state->getValue('visibility') as $condition_id => $condition_values) { |
| 435 | 1 | if ($this->isUnsupportedVisibilityCondition($condition_id)) { |
| 436 | continue; | |
| 437 | } | |
| 438 | // Get the condition plugin instance. | |
| 439 | 1 | $condition = $form_state->get(['conditions', $condition_id]); |
| 440 | // Validate the condition configuration. | |
| 441 | 1 | $condition->validateConfigurationForm( |
| 442 | 1 | $form['visibility'][$condition_id], |
| 443 | 1 | SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state) |
| 444 | 1 | ); |
| 445 | } | |
| 446 | } | |
| 447 | ||
| 448 | /** | |
| 449 | * {@inheritdoc} | |
| 450 | */ | |
| 451 | public function submitForm(array &$form, FormStateInterface $form_state): void { | |
| 452 | 2 | $settings = $this->config(self::SETTINGS); |
| 453 | 2 | $values = $form_state->getValues(); |
| 454 | ||
| 455 | // Process tracking conditions. | |
| 456 | 2 | $tracking_conditions = []; |
| 457 | 2 | foreach ($form_state->getValue('visibility') as $condition_id => $condition_values) { |
| 458 | 1 | if ($this->isUnsupportedVisibilityCondition($condition_id)) { |
| 459 | continue; | |
| 460 | } | |
| 461 | // Get the condition plugin instance. | |
| 462 | 1 | $condition = $form_state->get(['conditions', $condition_id]); |
| 463 | // Submit the condition configuration. | |
| 464 | 1 | $condition->submitConfigurationForm( |
| 465 | 1 | $form['visibility'][$condition_id], |
| 466 | 1 | SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state) |
| 467 | 1 | ); |
| 468 | // Store the configuration. | |
| 469 | 1 | $tracking_conditions[$condition_id] = $condition->getConfiguration(); |
| 470 | } | |
| 471 | ||
| 472 | 2 | $settings |
| 473 | 2 | ->set('flush_log_timer', $values['flush_log_timer']) |
| 474 | 2 | ->set('bot_retention_log', $values['bot_retention_log']) |
| 475 | 2 | ->set('track.userid', (bool) ($values['visitors_trackuserid'] ?? FALSE)) |
| 476 | 2 | ->set('track.userid_anonymize', (bool) ($values['visitors_userid_anonymize'] ?? FALSE)) |
| 477 | 2 | ->set('counter.enabled', $values['counter_enabled']) |
| 478 | 2 | ->set('counter.entity_types', array_filter($values['entity_types'] ?? [])) |
| 479 | 2 | ->set('disable_tracking', $values['visitors_disable_tracking']) |
| 480 | 2 | ->set('visibility.user_account_mode', $values['visitors_visibility_user_account_mode']) |
| 481 | 2 | ->set('visibility.exclude_user1', (bool) ($values['visibility_exclude_user1'] ?? FALSE)) |
| 482 | 2 | ->set('script_type', $values['script_type'] ?? 'minified') |
| 483 | 2 | ->set('client_debug', (bool) ($values['client_debug'] ?? FALSE)) |
| 484 | 2 | ->set('scroll_depth', (bool) ($values['scroll_depth'] ?? FALSE)) |
| 485 | 2 | ->set('javascript_errors', $this->javascriptErrorsMode($values['javascript_errors'] ?? 'off')) |
| 486 | 2 | ->set('track_downloads', (bool) ($values['track_downloads'] ?? FALSE)) |
| 487 | 2 | ->set('track_outlinks', (bool) ($values['track_outlinks'] ?? FALSE)) |
| 488 | 2 | ->set('track_media', (bool) ($values['track_media'] ?? FALSE)) |
| 489 | 2 | ->set('heartbeat_interval', $this->heartbeatIntervalValue($values['heartbeat_interval'] ?? NULL)) |
| 490 | 2 | ->set('ip_anonymize', (bool) ($values['ip_anonymize'] ?? FALSE)) |
| 491 | 2 | ->set('ip_mask_length', (int) ($values['ip_mask_length'] ?? 1)) |
| 492 | 2 | ->set('honor_do_not_track', (bool) ($values['honor_do_not_track'] ?? FALSE)) |
| 493 | 2 | ->set('cookies', (bool) ($values['cookies'] ?? FALSE)) |
| 494 | 2 | ->set('site_search_views', $this->siteSearchViewsConfig((string) ($values['site_search_views'] ?? ''))) |
| 495 | 2 | ->set('location_plugin', (string) ($values['location_plugin'] ?? 'browser_language')) |
| 496 | 2 | ->set('tracking_conditions', $tracking_conditions) |
| 497 | 2 | ->save(); |
| 498 | ||
| 499 | 2 | parent::submitForm($form, $form_state); |
| 500 | } | |
| 501 | ||
| 502 | /** | |
| 503 | * Location plugins, ordered by weight. | |
| 504 | * | |
| 505 | * @return array | |
| 506 | * Plugin labels keyed by plugin id. | |
| 507 | */ | |
| 508 | protected function locationPluginOptions(): array { | |
| 509 | 4 | $definitions = $this->locationPluginManager->getDefinitions(); |
| 510 | 4 | uasort($definitions, static function (array $left, array $right): int { |
| 511 | 4 | return ($left['weight'] ?? 0) <=> ($right['weight'] ?? 0); |
| 512 | 4 | }); |
| 513 | 4 | $options = []; |
| 514 | 4 | foreach ($definitions as $id => $definition) { |
| 515 | 4 | $options[$id] = $definition['label'] ?? $id; |
| 516 | } | |
| 517 | 4 | return $options; |
| 518 | } | |
| 519 | ||
| 520 | /** | |
| 521 | * Helper function for building the visibility UI form. | |
| 522 | * | |
| 523 | * @param array $form | |
| 524 | * An associative array containing the structure of the form. | |
| 525 | * @param \Drupal\Core\Form\FormStateInterface $form_state | |
| 526 | * The current state of the form. | |
| 527 | * @param \Drupal\Core\Config\ImmutableConfig $settings | |
| 528 | * The configuration object. | |
| 529 | * | |
| 530 | * @return array | |
| 531 | * The form array with the visibility UI added in. | |
| 532 | */ | |
| 533 | protected function buildVisibilityConditions(array &$form, FormStateInterface $form_state, $settings): array { | |
| 534 | 3 | $form['visibility_tabs'] = [ |
| 535 | 3 | '#type' => 'vertical_tabs', |
| 536 | 3 | '#title' => $this->t('Visibility'), |
| 537 | 3 | '#title_display' => 'invisible', |
| 538 | 3 | '#parents' => ['visibility_tabs'], |
| 539 | 3 | '#group' => 'visibility_tabs', |
| 540 | 3 | ]; |
| 541 | ||
| 542 | // Get current tracking conditions. | |
| 543 | 3 | $tracking_conditions = $settings->get('tracking_conditions') ?: []; |
| 544 | ||
| 545 | // Get available contexts for condition plugins. | |
| 546 | 3 | $contexts = $this->contextRepository->getAvailableContexts(); |
| 547 | 3 | $form_state->setTemporaryValue('gathered_contexts', $contexts); |
| 548 | ||
| 549 | // Initialize visibility container. | |
| 550 | 3 | $form['visibility'] = [ |
| 551 | 3 | '#type' => 'container', |
| 552 | 3 | '#tree' => TRUE, |
| 553 | 3 | ]; |
| 554 | ||
| 555 | 3 | $excluded_conditions = $settings->get('excluded_conditions') ?: []; |
| 556 | 3 | $excluded_conditions = array_merge( |
| 557 | 3 | $excluded_conditions, |
| 558 | 3 | self::UNSUPPORTED_VISIBILITY_CONDITIONS, |
| 559 | 3 | ); |
| 560 | // Get all available condition plugin definitions. | |
| 561 | 3 | $all_definitions = $this->conditionManager->getDefinitions(); |
| 562 | 3 | $definitions = array_diff_key($all_definitions, array_flip($excluded_conditions)); |
| 563 | 3 | foreach ($definitions as $condition_id => $definition) { |
| 564 | // Get existing condition configuration or create new instance. | |
| 565 | 1 | $condition_config = $tracking_conditions[$condition_id] ?? ['id' => $condition_id]; |
| 566 | 1 | $condition = $this->conditionManager->createInstance( |
| 567 | 1 | $condition_id, |
| 568 | 1 | $condition_config |
| 569 | 1 | ); |
| 570 | ||
| 571 | // Store condition for validation and submission. | |
| 572 | 1 | $form_state->set(['conditions', $condition_id], $condition); |
| 573 | ||
| 574 | // Build condition form. | |
| 575 | 1 | $condition_form = $condition->buildConfigurationForm([], $form_state); |
| 576 | 1 | $condition_form['#type'] = 'details'; |
| 577 | 1 | $condition_form['#title'] = $definition['label']; |
| 578 | 1 | $condition_form['#group'] = 'visibility_tabs'; |
| 579 | ||
| 580 | 1 | $form['visibility'][$condition_id] = $condition_form; |
| 581 | } | |
| 582 | ||
| 583 | 3 | return $form; |
| 584 | } | |
| 585 | ||
| 586 | /** | |
| 587 | * Returns a list of entity types. | |
| 588 | */ | |
| 589 | protected function entityTypes() { | |
| 590 | 3 | $entity_types_list = []; |
| 591 | 3 | $entity_definitions = $this->entityTypeManager->getDefinitions(); |
| 592 | 3 | foreach ($entity_definitions as $entity_name => $entity_definition) { |
| 593 | 3 | if ($entity_definition instanceof ConfigEntityType) { |
| 594 | continue; | |
| 595 | } | |
| 596 | 3 | $entity_types_list[$entity_name] = (string) $entity_definition->getLabel(); |
| 597 | } | |
| 598 | 3 | asort($entity_types_list); |
| 599 | ||
| 600 | 3 | return $entity_types_list; |
| 601 | } | |
| 602 | ||
| 603 | /** | |
| 604 | * Normalizes the JavaScript errors setting. | |
| 605 | * | |
| 606 | * A missing or unknown value does not record errors. | |
| 607 | * | |
| 608 | * @param mixed $value | |
| 609 | * The stored or submitted value. | |
| 610 | * | |
| 611 | * @return string | |
| 612 | * off, activity, or drupal. | |
| 613 | */ | |
| 614 | protected function javascriptErrorsMode(mixed $value): string { | |
| 615 | 5 | if (in_array($value, ['off', 'activity', 'drupal'], TRUE)) { |
| 616 | 2 | return $value; |
| 617 | } | |
| 618 | 3 | return 'off'; |
| 619 | } | |
| 620 | ||
| 621 | /** | |
| 622 | * Normalizes the heartbeat interval. | |
| 623 | * | |
| 624 | * A missing value is 15 seconds. Zero turns the heartbeat off. | |
| 625 | * The tracker will not use a delay below 5 seconds. | |
| 626 | * | |
| 627 | * @param mixed $value | |
| 628 | * The stored or submitted value. | |
| 629 | * | |
| 630 | * @return int | |
| 631 | * The interval in seconds. | |
| 632 | */ | |
| 633 | protected function heartbeatIntervalValue(mixed $value): int { | |
| 634 | 5 | if ($value === NULL || $value === '') { |
| 635 | 5 | return 15; |
| 636 | } | |
| 637 | 0 | $interval = (int) $value; |
| 638 | 0 | if ($interval <= 0) { |
| 639 | 0 | return 0; |
| 640 | } | |
| 641 | 0 | return max(5, $interval); |
| 642 | } | |
| 643 | ||
| 644 | /** | |
| 645 | * Whether a condition plugin is not used for tracking visibility. | |
| 646 | * | |
| 647 | * @param string $condition_id | |
| 648 | * The condition plugin id. | |
| 649 | * | |
| 650 | * @return bool | |
| 651 | * TRUE when the condition must be ignored. | |
| 652 | */ | |
| 653 | private function isUnsupportedVisibilityCondition(string $condition_id): bool { | |
| 654 | 2 | return in_array( |
| 655 | 2 | $condition_id, |
| 656 | 2 | self::UNSUPPORTED_VISIBILITY_CONDITIONS, |
| 657 | 2 | TRUE, |
| 658 | 2 | ); |
| 659 | } | |
| 660 | ||
| 661 | /** | |
| 662 | * Textarea value for the opted-in view displays. | |
| 663 | * | |
| 664 | * @param mixed $views | |
| 665 | * The stored site_search_views value. | |
| 666 | * | |
| 667 | * @return string | |
| 668 | * One view|display|keyword line per display. | |
| 669 | */ | |
| 670 | private function siteSearchViewsText(mixed $views): string { | |
| 671 | 3 | if (!is_array($views)) { |
| 672 | 3 | return ''; |
| 673 | } | |
| 674 | 0 | $lines = []; |
| 675 | 0 | foreach ($views as $view) { |
| 676 | 0 | if (!is_array($view)) { |
| 677 | continue; | |
| 678 | } | |
| 679 | 0 | $id = trim((string) ($view['view'] ?? '')); |
| 680 | 0 | $display = trim((string) ($view['display'] ?? '')); |
| 681 | 0 | $keyword = trim((string) ($view['keyword'] ?? '')); |
| 682 | 0 | if ($id === '' || $display === '' || $keyword === '') { |
| 683 | continue; | |
| 684 | } | |
| 685 | 0 | $lines[] = $id . '|' . $display . '|' . $keyword; |
| 686 | } | |
| 687 | 0 | return implode("\n", $lines); |
| 688 | } | |
| 689 | ||
| 690 | /** | |
| 691 | * Non-empty lines from the site search textarea. | |
| 692 | * | |
| 693 | * @param string $text | |
| 694 | * The submitted textarea. | |
| 695 | * | |
| 696 | * @return string[] | |
| 697 | * The trimmed lines. | |
| 698 | */ | |
| 699 | private function siteSearchViewLines(string $text): array { | |
| 700 | 3 | $lines = preg_split('/\r\n|\r|\n/', $text); |
| 701 | 3 | if ($lines === FALSE) { |
| 702 | 0 | return []; |
| 703 | } | |
| 704 | 3 | $kept = []; |
| 705 | 3 | foreach ($lines as $line) { |
| 706 | 3 | $line = trim($line); |
| 707 | 3 | if ($line !== '') { |
| 708 | 0 | $kept[] = $line; |
| 709 | } | |
| 710 | } | |
| 711 | 3 | return $kept; |
| 712 | } | |
| 713 | ||
| 714 | /** | |
| 715 | * Config value for the opted-in view displays. | |
| 716 | * | |
| 717 | * @param string $text | |
| 718 | * The submitted textarea. | |
| 719 | * | |
| 720 | * @return array<int, array<string, string>> | |
| 721 | * View, display, and keyword rows. | |
| 722 | */ | |
| 723 | private function siteSearchViewsConfig(string $text): array { | |
| 724 | 2 | $views = []; |
| 725 | 2 | foreach ($this->siteSearchViewLines($text) as $line) { |
| 726 | 0 | $parts = array_map('trim', explode('|', $line)); |
| 727 | 0 | if (count($parts) !== 3 || in_array('', $parts, TRUE)) { |
| 728 | continue; | |
| 729 | } | |
| 730 | 0 | $views[] = [ |
| 731 | 0 | 'view' => $parts[0], |
| 732 | 0 | 'display' => $parts[1], |
| 733 | 0 | 'keyword' => $parts[2], |
| 734 | 0 | ]; |
| 735 | } | |
| 736 | 2 | return $views; |
| 737 | } | |
| 738 | ||
| 739 | } |