matomo.install 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * @file
  4. * Installation file for Matomo Analytics module.
  5. */
  6. use Drupal\Core\Url;
  7. use Drupal\user\Entity\Role;
  8. /**
  9. * Implements hook_install().
  10. */
  11. function matomo_install() {
  12. // Make the default install more user and GDPR friendly.
  13. $role = Role::load('authenticated');
  14. $role->grantPermission('opt-in or out of matomo tracking');
  15. $success = $role->save();
  16. if ($success) {
  17. drupal_set_message(t('Module %module granted %permission permission to authenticated users.', ['%module' => 'Matomo Analytics', '%permission' => t('Opt-in or out of tracking')]), 'status');
  18. }
  19. }
  20. /**
  21. * Implements hook_uninstall().
  22. *
  23. * Remove cache directory if module is uninstalled.
  24. */
  25. function matomo_uninstall() {
  26. matomo_clear_js_cache();
  27. }
  28. /**
  29. * Implements hook_requirements().
  30. */
  31. function matomo_requirements($phase) {
  32. $requirements = [];
  33. if ($phase == 'runtime') {
  34. $config = \Drupal::config('matomo.settings');
  35. // Raise warning if Matomo user account has not been set yet.
  36. if (!preg_match('/^\d{1,}$/', $config->get('site_id'))) {
  37. $requirements['matomo_site_id'] = [
  38. 'title' => t('Matomo module'),
  39. 'description' => t('Matomo module has not been configured yet. Please configure its settings from the <a href=":url">Matomo settings page</a>.', [':url' => Url::fromRoute('matomo.admin_settings_form')->toString()]),
  40. 'severity' => REQUIREMENT_WARNING,
  41. 'value' => t('Not configured'),
  42. ];
  43. }
  44. }
  45. return $requirements;
  46. }