You are here

piwik_reports.module in Piwik Reports 8

File

piwik_reports.module
View source
<?php

/**
 * @file
 * Contains piwik_reports.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function piwik_reports_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the piwik_reports module.
    case 'piwik_reports.piwik_reports_settings':
      return t('<a href=":pk_url">Piwik - Web analytics</a> is an open source (GPL license) web analytics software. It gives interesting reports on your website visitors, your popular pages, the search engines keywords they used, the language they speak... and so much more. Piwik aims to be an open source alternative to Google Analytics.', [
        ':pk_url' => 'http://www.piwik.org/',
      ]);
  }
}

/**
 * Implements hook_theme().
 */
function piwik_reports_theme() {
  return [
    'piwik_reports' => [
      'variables' => [
        'data_url' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function piwik_reports_form_user_form_alter(&$form, $form_state, $form_id) {
  $x = 1;
  $config = \Drupal::config('piwik_reports.piwikreportssettings');
  $account = $form_state
    ->getFormObject()
    ->getEntity();
  $userData = \Drupal::service('user.data');
  if ($account
    ->hasPermission('access piwik reports') && $config
    ->get('piwik_reports_token_auth') == '') {
    $account = \Drupal::currentUser();
    $form['piwik_reports'] = array(
      '#type' => 'fieldset',
      '#title' => t('Piwik reports configuration'),
      '#weight' => 3,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['piwik_reports']['piwik_reports_token_auth'] = array(
      '#type' => 'textfield',
      '#title' => t('Piwik authentication string'),
      '#default_value' => !empty($userData
        ->get('piwik_reports', $account
        ->id(), 'piwik_reports_token_auth')) ? $userData
        ->get('piwik_reports', $account
        ->id(), 'piwik_reports_token_auth') : '',
      '#size' => 40,
      '#maxlength' => 40,
      '#description' => t('Click the <strong>Settings</strong> link in your Piwik account, then the <strong>Users</strong> tab and copy and paste your personal <strong>token_auth</strong> value into this field. If anonymous users have view permissions in Piwik you can set this value to <strong>anonymous</strong>. Or just ask the Piwik server administrator.'),
    );

    // hook_user_update() is missing in D8, add custom submit handler.
    $form['actions']['submit']['#submit'][] = 'piwik_reports_user_profile_form_submit';
  }
}

/**
 * Submit callback for user profile form to save the Piwik Reports setting.
 */
function piwik_reports_user_profile_form_submit($form, FormStateInterface $form_state) {
  $account = $form_state
    ->getFormObject()
    ->getEntity();
  $y = $account
    ->id();
  if ($account
    ->id() && $form_state
    ->hasValue('piwik_reports_token_auth')) {
    \Drupal::service('user.data')
      ->set('piwik_reports', $account
      ->id(), 'piwik_reports_token_auth', $form_state
      ->getValue('piwik_reports_token_auth'));
  }
}

Functions

Namesort descending Description
piwik_reports_form_user_form_alter Implements hook_form_FORM_ID_alter().
piwik_reports_help Implements hook_help().
piwik_reports_theme Implements hook_theme().
piwik_reports_user_profile_form_submit Submit callback for user profile form to save the Piwik Reports setting.