class LivechatWidgetHandler in Rocket.Chat 8
Same name and namespace in other branches
- 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
- class \Drupal\livechat\LivechatWidgetHandler
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\livechatView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LivechatWidgetHandler:: |
private | property | Form. | |
LivechatWidgetHandler:: |
private | property | WidgetLibraryName. | |
LivechatWidgetHandler:: |
private | property | WidgetLibraryRoute. | |
LivechatWidgetHandler:: |
private | function | Build Javascript Settings Array. | |
LivechatWidgetHandler:: |
public | function | Get widgetLibraryName. | |
LivechatWidgetHandler:: |
public | function | Get widgetLibraryRoute. | |
LivechatWidgetHandler:: |
public | function | Render Widget. | |
LivechatWidgetHandler:: |
private | function | Extract the intended javascript based on the route and the Library route. | |
LivechatWidgetHandler:: |
private | function | Setter Javascript Parameters. | |
LivechatWidgetHandler:: |
public | function | Setter for widgetLibraryName. | |
LivechatWidgetHandler:: |
public | function | Setter for widgetLibraryRoute. | |
LivechatWidgetHandler:: |
public | function | Get widgetParams. | |
LivechatWidgetHandler:: |
public | function | WidgetHandler constructor. |