Skip to content
Snippets Groups Projects
Commit 01a3d8d7 authored by Christophe Goffin's avatar Christophe Goffin Committed by Bram Driesen
Browse files

Issue #3373125: Missing cache invalidation when chatbot settings change

parent 76c957cd
No related branches found
Tags 4.0.0-alpha6
1 merge request!7Issue #3373125: Missing cache invalidation when chatbot settings change
......@@ -2,11 +2,11 @@
namespace Drupal\oswald\Controller;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\oswald\Entity\OswaldChatbot;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
......@@ -104,12 +104,21 @@ class OswaldChatbotController {
* The whole page array.
*/
public function attachAndInitialize(array &$page): void {
$chatbot = $this->getActiveChatbot();
$chatbot = $this->getChatbot();
// Exit early if we have not received a chatbot.
if (!$chatbot) {
return;
}
// Set necessary caching information.
$cacheable_metadata = CacheableMetadata::createFromRenderArray($page);
$cacheable_metadata->addCacheTags($chatbot->getCacheTags());
$cacheable_metadata->applyTo($page);
if (!$chatbot->get('enabled')->value) {
return;
}
// Generate the snippet to attach.
$script = 'setTimeout(function(){ var e=new XMLHttpRequest;if(e.open("GET","' . $chatbot->getUrlForEnvironment() . '/api/v1/chats/' . $chatbot->getChatbotId() . '/widget?open=' . $chatbot->getAutoOpen() . '&locale=' . $this->languageManager->getCurrentLanguage()->getId() . '",!1),e.send(null),200==e.status){var t=document.createElement("script");t.innerHTML=e.responseText,document.body.appendChild(t);};}, 500);';
$page['#attached']['html_head'][] = [
......@@ -132,15 +141,16 @@ class OswaldChatbotController {
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Executable\ExecutableException
*/
protected function getActiveChatbot(): ?OswaldChatbot {
protected function getChatbot(): ?OswaldChatbot {
if ($this->routeMatch->getRouteName() === 'entity.oswald_chatbot.test_form') {
// Only load this specific bot and return.
return OswaldChatbot::load($this->routeMatch->getParameter('oswald_chatbot'));
}
// Get all the enabled chatbots.
$chatbots = $this->entityTypeManager->getStorage('oswald_chatbot')->loadByProperties(['enabled' => TRUE]);
$chatbots = $this->entityTypeManager->getStorage('oswald_chatbot')->loadMultiple();
/** @var \Drupal\system\Plugin\Condition\RequestPath $condition */
$condition = $this->manager->createInstance('request_path');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment