You are here

public function TawktoGenerator::setWidget in tawk.to Live Chat (Drupal 8) 8

File

tawk_to/src/core/TawktoGenerator.php, line 337

Class

TawktoGenerator

Namespace

Drupal\tawk_to\core

Code

public function setWidget($page, $widget) {
  $page = trim($page);
  $widget = trim($widget);
  $options = array(
    'success' => false,
  );
  if (!$page || !$widget) {
    return new JsonResponse($options);
  }
  if (preg_match('/^[0-9A-Fa-f]{24}$/', $page) !== 1 || preg_match('/^[a-z0-9]{1,50}$/i', $widget) !== 1) {
    return new JsonResponse($options);
  }

  // $config = $this->config('tawk_to.settings');
  $config = \Drupal::service('config.factory')
    ->getEditable('tawk_to.settings');
  $config
    ->set('tawk_to.page_id', $page);
  $config
    ->set('tawk_to.widget_id', $widget);
  $config
    ->set('tawk_to.user_id', \Drupal::currentUser()
    ->id());

  // $config->set('tawk_to.options', $options);
  //
  $config
    ->save();

  // \Drupal::state()->set(TAWK_TO_WIDGET_PID, $page);
  // \Drupal::state()->set(TAWK_TO_WIDGET_WID, $widget);
  $options = array(
    'success' => true,
  );
  return new JsonResponse($options);
}