You are here

class LivechatWidgetHandler in Rocket.Chat 8

Same name and namespace in other branches
  1. 8.2 modules/livechat/src/LivechatWidgetHandler.php \Drupal\livechat\LivechatWidgetHandler

Glue class to make the widget dynamically build the javascript file.

@Class LivechatWidgetHandler.

@package Drupal\livechat

Hierarchy

Expanded class hierarchy of LivechatWidgetHandler

2 files declare their use of LivechatWidgetHandler
LiveChatBlock.php in modules/livechat/src/Plugin/Block/LiveChatBlock.php
LivechatWidgetcontroller.php in modules/livechat/src/Controller/LivechatWidgetcontroller.php
Contains \Drupal\rocket_chat\Controller\Rocket.

File

modules/livechat/src/LivechatWidgetHandler.php, line 42
Contains \Drupal\rocket_chat\WidgetHandler.

Namespace

Drupal\livechat
View source
class LivechatWidgetHandler {

  /**
   * WidgetLibraryName.
   *
   * @var mixed|string
   */
  private $widgetLibraryName;

  /**
   * WidgetLibraryRoute.
   *
   * @var mixed|string
   */
  private $widgetLibraryRoute;

  /**
   * Form.
   *
   * @var array
   */
  private $form;

  /**
   * WidgetHandler constructor.
   *
   * @param string|mixed $widgetLibraryName
   *   Library Name.
   * @param string|mixed $widgetLibraryRoute
   *   Library Route.
   */
  public function __construct($widgetLibraryName, $widgetLibraryRoute) {
    if (!empty($widgetLibraryName) && !is_null($widgetLibraryName)) {
      if (!empty($widgetLibraryRoute) && !is_null($widgetLibraryRoute)) {
        $this->widgetLibraryName = $widgetLibraryName;
        $this->widgetLibraryRoute = $widgetLibraryRoute;
        $this->form = [];
      }
    }
  }

  /**
   * Render Widget.
   *
   * @param array|null $keys
   *   Array to render.
   *
   * @return array
   *   Rendered Widget.
   */
  public function renderWidgetWithJavaScriptKeys(array $keys) {
    if (!empty($keys)) {
      $this
        ->setAssets();
      foreach ($keys as $value) {
        $this
          ->setJavascriptParams($value);
      }
      return $this
        ->widgetParams();
    }
    return [];
  }

  /**
   * Setter for widgetLibraryRoute.
   *
   * @param mixed|string $widgetLibraryRoute
   *   Widget library route.
   */
  public function setWidgetLibraryRoute($widgetLibraryRoute) {
    $this->widgetLibraryRoute = $widgetLibraryRoute;
  }

  /**
   * Setter for widgetLibraryName.
   *
   * @param mixed|string $widgetLibraryName
   *   Widget library name.
   */
  public function setWidgetLibraryName($widgetLibraryName) {
    $this->widgetLibraryName = $widgetLibraryName;
  }

  /**
   * Get widgetLibraryRoute.
   *
   * @return mixed|string
   *   widgetLibraryRoute.
   */
  public function getWidgetLibraryRoute() {
    return $this->widgetLibraryRoute;
  }

  /**
   * Get widgetLibraryName.
   *
   * @return mixed|string
   *   widgetLibraryName.
   */
  public function getWidgetLibraryName() {
    return $this->widgetLibraryName;
  }

  /**
   * Get widgetParams.
   *
   * @return array
   *   Form Array
   */
  public function widgetParams() {
    return $this->form;
  }

  /*
   * get the .js file by getting the route
   * and it's library route
   * rocket_chat.libraries.yml has rocket_chat_conf which has the app.js file
   * output: rocket_chat/rocket_chat_conf
   */

  /**
   * Extract the intended javascript based on the route and the Library route.
   */
  private function setAssets() {
    $this->form['#attached']['library'][] = $this
      ->getWidgetLibraryName() . '/' . $this
      ->getWidgetLibraryRoute();
  }

  /**
   * Setter Javascript Parameters.
   *
   * @param string $key
   *   String representing the key.
   *
   * @TODO Extend this to include Department setting and theme setting.
   */
  private function setJavascriptParams($key) {
    if (!empty($key) && !is_null($key)) {
      switch ($key) {
        case 'server':
          $this
            ->buildJavaScriptArray('server', \Drupal::config('rocket_chat.settings')
            ->get('server'));
          break;
        default:
          return;
      }
    }
  }

  /**
   * Build Javascript Settings Array.
   *
   * The values to send to the Javascript file declared in your library's route
   * drupalSettings is a javascript global object declared by the Drupal API
   * to get values within your js file, use
   * e.g. drupalSettings.library.route.key.
   *
   * @param string $key
   *   Key to set the value in.
   * @param mixed $value
   *   Value to set in the key register.
   */
  private function buildJavaScriptArray($key, $value) {
    $ds = $this->form['#attached']['drupalSettings'][$this
      ->getWidgetLibraryName()][$this
      ->getWidgetLibraryRoute()][$key] = $value;
    return $ds;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LivechatWidgetHandler::$form private property Form.
LivechatWidgetHandler::$widgetLibraryName private property WidgetLibraryName.
LivechatWidgetHandler::$widgetLibraryRoute private property WidgetLibraryRoute.
LivechatWidgetHandler::buildJavaScriptArray private function Build Javascript Settings Array.
LivechatWidgetHandler::getWidgetLibraryName public function Get widgetLibraryName.
LivechatWidgetHandler::getWidgetLibraryRoute public function Get widgetLibraryRoute.
LivechatWidgetHandler::renderWidgetWithJavaScriptKeys public function Render Widget.
LivechatWidgetHandler::setAssets private function Extract the intended javascript based on the route and the Library route.
LivechatWidgetHandler::setJavascriptParams private function Setter Javascript Parameters.
LivechatWidgetHandler::setWidgetLibraryName public function Setter for widgetLibraryName.
LivechatWidgetHandler::setWidgetLibraryRoute public function Setter for widgetLibraryRoute.
LivechatWidgetHandler::widgetParams public function Get widgetParams.
LivechatWidgetHandler::__construct public function WidgetHandler constructor.