You are here

public function TawktoGenerator::getWidget in tawk.to Live Chat (Drupal 8) 8

Return widget details from the database.

1 call to TawktoGenerator::getWidget()
TawktoGenerator::widget in tawk_to/src/core/TawktoGenerator.php

File

tawk_to/src/core/TawktoGenerator.php, line 29

Class

TawktoGenerator

Namespace

Drupal\tawk_to\core

Code

public function getWidget() {
  $widgetVars = $this
    ->getWidgetVars();
  extract($widgetVars);
  if (!$page_id || !$widget_id) {
    return '';
  }
  $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()
    ->id());
  if ($user) {
    $username = $user
      ->get('name')->value;
    $usermail = $user
      ->get('mail')->value;
    $apiString = '$_Tawk_API.visitor = {
                name  : "' . $username . '",
                email : "' . $usermail . '",
            };';
  }
  ob_start();
  ?><!--Start of Tawk.to Script-->
          <script type="text/javascript">
          var $_Tawk_API={},$_Tawk_LoadStart=new Date();
          (function(){
          var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
          s1.async=true;
          s1.src="https://embed.tawk.to/<?php

  echo $page_id;
  ?>/<?php

  echo $widget_id;
  ?>";
          s1.charset="UTF-8";
          s1.setAttribute("crossorigin","*");
          s0.parentNode.insertBefore(s1,s0);
          })();
          <?php

  echo $apiString;
  ?>
          </script>
          <!--End of Tawk.to Script--><?php

  $output = ob_get_contents();
  ob_end_clean();
  return $output;
}