You are here

class TawkToEmbedRender in Tawk.to - Live chat application (Drupal 8) 8

Same name and namespace in other branches
  1. 8.2 src/Service/TawkToEmbedRender.php \Drupal\tawk_to\Service\TawkToEmbedRender

Defines the rendering tawk.to service.

Hierarchy

Expanded class hierarchy of TawkToEmbedRender

1 string reference to 'TawkToEmbedRender'
tawk_to.services.yml in ./tawk_to.services.yml
tawk_to.services.yml
1 service uses TawkToEmbedRender
tawk_to.embed_render in ./tawk_to.services.yml
Drupal\tawk_to\Service\TawkToEmbedRender

File

src/Service/TawkToEmbedRender.php, line 12

Namespace

Drupal\tawk_to\Service
View source
class TawkToEmbedRender {

  /**
   * The tawk.to embed URL.
   */
  const EMBED_URL = 'https://embed.tawk.to';

  /**
   * The widget page id.
   *
   * @var string
   */
  protected $widgetPageId;

  /**
   * The widget id.
   *
   * @var string
   */
  protected $widgetId;

  /**
   * The user name.
   *
   * @var string
   */
  protected $userName;

  /**
   * The user email.
   *
   * @var string
   */
  protected $userEmail;

  /**
   * The cache manager.
   *
   * @var \Drupal\tawk_to\Cache\TawkToCacheManager
   */
  protected $cacheManager;

  /**
   * The condition plugin defination.
   *
   * @var \Drupal\tawk_to\Service\TawkToConditionPluginsHandler
   */
  protected $conditionPluginsHandler;

  /**
   * Constructs the TawkToEmbedRender.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Utility\Token $token
   *   The token service.
   * @param Drupal\tawk_to\Service\TawkToConditionPluginsHandler $conditionPluginsHandler
   *   The tawk.to access controller handler.
   * @param Drupal\tawk_to\Cache\TawkToCacheManager $cacheManager
   *   The cache manager.
   */
  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,
      ]);
    }
  }

  /**
   * Checks acess to the current requests and return renderable array or NULL.
   *
   * @return array|null
   *   The render renderable array or NULL.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TawkToEmbedRender::$cacheManager protected property The cache manager.
TawkToEmbedRender::$conditionPluginsHandler protected property The condition plugin defination.
TawkToEmbedRender::$userEmail protected property The user email.
TawkToEmbedRender::$userName protected property The user name.
TawkToEmbedRender::$widgetId protected property The widget id.
TawkToEmbedRender::$widgetPageId protected property The widget page id.
TawkToEmbedRender::EMBED_URL constant The tawk.to embed URL.
TawkToEmbedRender::render public function Checks acess to the current requests and return renderable array or NULL.
TawkToEmbedRender::__construct public function Constructs the TawkToEmbedRender.