You are here

function tawk_to_page_bottom in tawk.to Live Chat (Drupal 8) 8

File

tawk_to/tawk_to.module, line 35

Code

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(),
    );
  }
}