You are here

private function GoogleAnalyticsCounterFilter::handleText in Google Analytics Counter 8.3

Finds 'gac' tags and replaces them by actual values.

Parameters

string $text: String to replace.

Return value

mixed Replaced string.

1 call to GoogleAnalyticsCounterFilter::handleText()
GoogleAnalyticsCounterFilter::process in src/Plugin/Filter/GoogleAnalyticsCounterFilter.php
Performs the filter processing.

File

src/Plugin/Filter/GoogleAnalyticsCounterFilter.php, line 112

Class

GoogleAnalyticsCounterFilter
Add filter to show google analytics counter number.

Namespace

Drupal\google_analytics_counter\Plugin\Filter

Code

private function handleText($text) {
  $matchlink = [];
  $original_match = [];

  // This allows more than one pipe sign (|) ...
  // does not hurt and leaves room for possible extension.
  preg_match_all("/(\\[)gac[^\\]]*(\\])/s", $text, $matches);
  foreach ($matches[0] as $match) {

    // Keep original value(s).
    $original_match[] = $match;

    // Display the page views.
    // [gac] will detect the current node's count.
    //
    // [gac|all] displays the totalsForAllResults for the given time period,
    // assuming cron has been run. Otherwise will print N/A.
    //
    // [gac|1234] displays the page views for node/1234. // Currently not working.
    //
    // [gac|node/1234] displays the page views for node/1234. // Currently not working.
    //
    // [gac|path/to/page] displays the pages views for path/to/page. // Currently not working.
    switch ($match) {
      case '[gac]':
        $matchlink[] = $this->appManager
          ->gacDisplayCount($this->currentPath
          ->getPath());
        break;
      case '[gac|all]':
        $matchlink[] = number_format($this->state
          ->get('google_analytics_counter.total_pageviews', 'N/A'));
        break;
      default:
        $path = substr($match, strpos($match, "/") + 1);
        $path = rtrim($path, ']');

        // Make sure the path starts with a slash.
        $path = '/' . trim($path, ' /');
        $matchlink[] = $this->appManager
          ->gacDisplayCount($this->aliasManager
          ->getAliasByPath($path));
        break;
    }
  }
  return str_replace($original_match, $matchlink, $text);
}