matomo.install 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. $messenger = \Drupal::messenger();
  18. $messenger->addMessage(t('Module %module granted %permission permission to authenticated users.', ['%module' => 'Matomo Analytics', '%permission' => t('Opt-in or out of tracking')]), 'status');
  19. }
  20. }
  21. /**
  22. * Implements hook_uninstall().
  23. *
  24. * Remove cache directory if module is uninstalled.
  25. */
  26. function matomo_uninstall() {
  27. matomo_clear_js_cache();
  28. }
  29. /**
  30. * Implements hook_requirements().
  31. */
  32. function matomo_requirements($phase) {
  33. $requirements = [];
  34. if ($phase == 'runtime') {
  35. $config = \Drupal::config('matomo.settings');
  36. // Raise warning if Matomo user account has not been set yet.
  37. if (!preg_match('/^\d{1,}$/', $config->get('site_id'))) {
  38. $requirements['matomo_site_id'] = [
  39. 'title' => t('Matomo module'),
  40. '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()]),
  41. 'severity' => REQUIREMENT_WARNING,
  42. 'value' => t('Not configured'),
  43. ];
  44. }
  45. }
  46. return $requirements;
  47. }