analytics-8.x-1.0-alpha5/analytics_piwik/src/Plugin/AnalyticsService/Piwik.php

analytics_piwik/src/Plugin/AnalyticsService/Piwik.php
<?php

namespace Drupal\analytics_piwik\Plugin\AnalyticsService;

use Drupal\analytics\Plugin\ServicePluginBase;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;

/**
 * Analytics service type.
 *
 * @AnalyticsService(
 *   id = "piwik",
 *   label = @Translation("Piwik"),
 * )
 */
class Piwik extends ServicePluginBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'url' => '',
      'id' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm($form, FormStateInterface $form_state) {
    $form['url'] = [
      '#type' => 'textfield',
      '#title' => t('URL'),
      '#description' => t('The URL to your Piwik base directory.'),
      '#default_value' => $this->configuration['url'],
      // @todo Add validation
      //'#element_validate' => [$this->validateUrl],
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'number',
      '#title' => t('Site ID'),
      '#default_value' => $this->configuration['id'],
      '#min' => 0,
      '#required' => TRUE,
      '#size' => 15,
    ];
    return $form;
  }

  function validateUrl($element, &$form_state) {
    $value = $element['#value'];
    if ($value != '') {
      // Make sure the URL is normalized.
      $value = rtrim($value, '/') . '/';
      $form_state->setValueForElement($element, $value);

      if (!UrlHelper::isValid($value, TRUE)) {
        $form_state->setError($element, t('%name is not a valid URL.', ['%name' => $element['#title']]));
      }
      else {
        \Drupal::httpClient()->request('GET', $value . '/piwik.js');
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getOutput() {
    $output = [];
    // This is just placeholder code.
    $output['analytics_' . $this->getServiceId()] = [
      '#type' => 'html_tag',
      '#tag' => 'piwik',
      '#attributes' => [
        'src' => $this->configuration['url'],
        'site_id' => $this->configuration['id'],
      ],
    ];
    return $output;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc