You are here

matomo.install in Matomo Analytics 8

Same filename and directory in other branches
  1. 7.2 matomo.install

Installation file for Matomo Analytics module.

File

matomo.install
View source
<?php

/**
 * @file
 * Installation file for Matomo Analytics module.
 */
use Drupal\Core\Url;
use Drupal\user\Entity\Role;

/**
 * Implements hook_install().
 */
function matomo_install() {

  // Make the default install more user and GDPR friendly.
  $role = Role::load('authenticated');
  $role
    ->grantPermission('opt-in or out of matomo tracking');
  $success = $role
    ->save();
  if ($success) {
    $messenger = \Drupal::messenger();
    $messenger
      ->addMessage(t('Module %module granted %permission permission to authenticated users.', [
      '%module' => 'Matomo Analytics',
      '%permission' => t('Opt-in or out of tracking'),
    ]), 'status');
  }
}

/**
 * Implements hook_uninstall().
 *
 * Remove cache directory if module is uninstalled.
 */
function matomo_uninstall() {
  $path = 'public://matomo';
  if (\Drupal::service('file_system')
    ->prepareDirectory($path)) {
    \Drupal::service('file_system')
      ->scanDirectory($path, '/.*/', [
      'callback' => '_matomo_uninstall_file_delete',
    ]);
    \Drupal::service('file_system')
      ->rmdir($path);

    // Change query-strings on css/js files to enforce reload for all users.
    _drupal_flush_css_js();
    \Drupal::logger('matomo')
      ->info('Local cache has been purged.');
  }
}

/**
 * Removes unmanaged file from the file system.
 *
 * @param string $uri
 *   The file to delete.
 *
 * @internal
 */
function _matomo_uninstall_file_delete($uri) {
  \Drupal::service('file_system')
    ->delete($uri);
}

/**
 * Implements hook_requirements().
 */
function matomo_requirements($phase) {
  $requirements = [];
  if ($phase == 'runtime') {
    $config = \Drupal::config('matomo.settings');

    // Raise warning if Matomo user account has not been set yet.
    if (!preg_match('/^\\d{1,}$/', $config
      ->get('site_id'))) {
      $requirements['matomo_site_id'] = [
        'title' => t('Matomo module'),
        '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(),
        ]),
        'severity' => REQUIREMENT_WARNING,
        'value' => t('Not configured'),
      ];
    }
  }
  return $requirements;
}

/**
 * Add new disable cookies setting to Matomo default configuration.
 */
function matomo_update_8101() {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('matomo.settings');
  $config
    ->set('privacy.disablecookies', FALSE);
  $config
    ->save(TRUE);
}

Functions

Namesort descending Description
matomo_install Implements hook_install().
matomo_requirements Implements hook_requirements().
matomo_uninstall Implements hook_uninstall().
matomo_update_8101 Add new disable cookies setting to Matomo default configuration.
_matomo_uninstall_file_delete Removes unmanaged file from the file system.