You are here

tawk_to.module in tawk.to Live Chat (Drupal 8) 8

File

tawk_to/tawk_to.module
View source
<?php

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\tawk_to\core\TawktoGenerator;

/**
 * Implements hook_help().
 */
function tawk_to_help($route_name, RouteMatchInterface $route_match) {
  if ($route_name !== 'tawk_to.help') {
    return;
  }
  $links = [
    ':homepage' => 'https://tawk.to',
    ':dashboard' => 'https://dashboard.tawk.to',
    ':project_page' => 'https://www.drupal.org/sandbox/tawk/2205433',
    ':mail_link' => 'support@tawk.to',
  ];
  $output = '';
  $output .= '<h3>' . t('About tawk.to') . '</h3>';
  $output .= '<p>' . t('tawk.to Drupal module allows you to easily choose which one of your tawk.to widgets you
    want to use on your drupal page. To create account please visit <a href=":project">https://tawk.to</a>.<br />
    To answer chat go to <a href="dashboard">https://dashboard.tawk.to</a>.<br />
    * For a full description of the module, visit the project page:<br />
    <a href=":project_page">https://www.drupal.org/sandbox/tawk/2205433</a><br /><br />
    * To submit bug reports and feature suggestions, or to track changes please email support@tawk.to
    ', $links) . '</p>';
  return $output;
}
function tawk_to_page_bottom(array &$page_bottom) {

  // check if current page is admin, so that chat widget is not shown.
  $route = \Drupal::routeMatch()
    ->getRouteObject();
  $is_admin_route = \Drupal::service('router.admin_context')
    ->isAdminRoute($route);
  $has_node_operation_option = $route
    ->getOption('_node_operation_route');
  $is_admin = $is_admin_route || $has_node_operation_option;
  if (!$is_admin) {
    $generator = new TawktoGenerator();

    // get visibility options
    $vars = $generator
      ->getWidgetVars();
    extract($vars);
    if ($options && !is_null($options)) {
      global $base_url;

      // $current_uri = \Drupal::request()->getRequestUri();
      $options = json_decode($options);

      // prepare visibility
      $currentUrl = $base_url . $_SERVER["REQUEST_URI"];
      if (false == $options->always_display) {
        $showPages = json_decode($options->show_oncustom);
        $show = false;
        foreach ($showPages as $slug) {
          if (empty(trim($slug))) {
            continue;
          }

          // if (stripos($currentUrl, $slug)!==false) {
          if ($currentUrl == $slug) {
            $show = true;
            break;
          }
        }

        // check if category/taxonomy page
        // taxonomy page
        if ("taxonomy_term" == strtolower(\Drupal::request()->attributes
          ->get('view_id'))) {
          if (false != $options->show_oncategory) {
            $show = true;
          }
        }

        // check if frontpage
        if (\Drupal::service('path.matcher')
          ->isFrontPage()) {
          if (false != $options->show_onfrontpage) {
            $show = true;
          }
        }

        // taxonomy page
        // code below is deprecated in Drupal 8
        // if (arg(0) ==  "taxonomy" && arg(1) == "term" && is_numeric(arg(2)) && arg(3) == "") {
        //     if (false==$options->show_oncategory) {
        //         return;
        //     }
        // }
        // $current_path = \Drupal::service('path.current')->getPath();
        // $path_args = explode('/', $current_path);
        if (!$show) {
          return;
        }
      }
      else {
        $hide_pages = json_decode($options->hide_oncustom);
        $show = true;

        // $currentUrl = urlencode($current_page);
        $currentUrl = (string) $currentUrl;
        foreach ($hide_pages as $slug) {
          if (empty(trim($slug))) {
            continue;
          }
          $slug = (string) htmlspecialchars($slug);

          // we need to add htmlspecialchars due to slashes added when saving to database
          // if (stripos($currentUrl, $slug)!==false) {
          if ($currentUrl == $slug) {
            $show = false;
            break;
          }
        }
        if (!$show) {
          return;
        }
      }
    }
    $page_bottom['tawk_to'] = array(
      '#type' => 'inline_template',
      '#template' => $generator
        ->widget(),
    );
  }
}

Functions

Namesort descending Description
tawk_to_help Implements hook_help().
tawk_to_page_bottom