You are here

public function TawkToWidgetController::setWidget in Tawk.to - Live chat application (Drupal 8) 8.2

Same name and namespace in other branches
  1. 8 src/Controller/TawkToWidgetController.php \Drupal\tawk_to\Controller\TawkToWidgetController::setWidget()

Callback for settting widget with ajax in tawk.to iframe.

Return value

\Symfony\Component\HttpFoundation\JsonResponse JSON object for JS code.

1 string reference to 'TawkToWidgetController::setWidget'
tawk_to.routing.yml in ./tawk_to.routing.yml
tawk_to.routing.yml

File

src/Controller/TawkToWidgetController.php, line 112

Class

TawkToWidgetController
Controller routine that manages tawk.to widget settings.

Namespace

Drupal\tawk_to\Controller

Code

public function setWidget() {
  $pageId = $this->request
    ->getCurrentRequest()
    ->get('pageId');
  $widgetId = $this->request
    ->getCurrentRequest()
    ->get('widgetId');
  if (!$pageId || !$widgetId) {
    return new JsonResponse([
      'success' => FALSE,
    ]);
  }
  if (preg_match('/^[0-9A-Fa-f]{24}$/', $pageId) !== 1 || preg_match('/^[a-z0-9]{1,50}$/i', $widgetId) !== 1) {
    return new JsonResponse([
      'success' => FALSE,
    ]);
  }
  $this->config
    ->set('tawk_to_widget_page_id', $pageId)
    ->save();
  $this->config
    ->set('tawk_to_widget_id', $widgetId)
    ->save();
  return new JsonResponse([
    'success' => TRUE,
  ]);
}