TawkToEmbedRender.php in Tawk.to - Live chat application (Drupal 8) 8.2
File
src/Service/TawkToEmbedRender.php
View source
<?php
namespace Drupal\tawk_to\Service;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\tawk_to\Cache\TawkToCacheManager;
use Drupal\Core\Utility\Token;
class TawkToEmbedRender {
const EMBED_URL = 'https://embed.tawk.to';
protected $widgetPageId;
protected $widgetId;
protected $userName;
protected $userEmail;
protected $cacheManager;
protected $conditionPluginsHandler;
public function __construct(ConfigFactoryInterface $configFactory, Token $token, TawkToConditionPluginsHandler $conditionPluginsHandler, TawkToCacheManager $cacheManager) {
$this->conditionPluginsHandler = $conditionPluginsHandler;
$this->cacheManager = $cacheManager;
$this->widgetPageId = $configFactory
->get('tawk_to.settings')
->get('tawk_to_widget_page_id');
$this->widgetId = $configFactory
->get('tawk_to.settings')
->get('tawk_to_widget_id');
if ($configFactory
->get('tawk_to.settings')
->get('show_user_name')) {
$userName = $configFactory
->get('tawk_to.settings')
->get('user_name');
$this->userName = $token
->replace($userName, [], [
'clear' => TRUE,
]);
}
if ($configFactory
->get('tawk_to.settings')
->get('show_user_email')) {
$userEmail = $configFactory
->get('tawk_to.settings')
->get('user_email');
$this->userEmail = $token
->replace($userEmail, [], [
'clear' => TRUE,
]);
}
}
public function render() {
if ($this->widgetPageId === '' || $this->widgetId === '') {
return NULL;
}
if ($this->conditionPluginsHandler
->checkAccess()) {
return [
'#theme' => 'tawk_to',
'#items' => [
'page_id' => $this->widgetPageId,
'widget_id' => $this->widgetId,
'embed_url' => self::EMBED_URL,
'user_name' => $this->userName,
'user_email' => $this->userEmail,
],
'#cache' => [
'contexts' => $this->cacheManager
->getCacheContexts(),
'tags' => $this->cacheManager
->getCacheTags(),
],
];
}
return NULL;
}
}