Skip to content
Snippets Groups Projects

#3512736 Fix for null check.

Open Anjali Prasannan requested to merge issue/ai-3512736:3512736-php-error-null into 1.1.x
1 unresolved thread
@@ -12,6 +12,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\ai_chatbot\Form\ChatForm;
use Drupal\ai_assistant_api\Entity\AiAssistant as AiAssistant;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@@ -262,8 +263,27 @@ class ChatFormBlock extends BlockBase implements ContainerFactoryPluginInterface
* {@inheritdoc}
*/
public function build() {
/** @var \Drupal\ai_assistant_api\Entity\AiAssistant */
$assistant = $this->entityTypeManager->getStorage('ai_assistant')->load($this->configuration['ai_assistant']);
// Load the AI Assistant entity.
$assistant_id = $this->configuration['ai_assistant'] ?? NULL;
// If no assistant ID is provided or it's invalid, handle it gracefully.
if (!is_string($assistant_id) || $assistant_id === "") {
return [
'#markup' => $this->t('No valid AI Assistant configured for this block.'),
];
}
/** @var \Drupal\ai_assistant_api\Entity\AiAssistant|null */
$assistant = $this->entityTypeManager->getStorage('ai_assistant')->load($assistant_id);
// If the assistant still couldn't be loaded, handle the failure.
if (!$assistant instanceof AiAssistant) {
return [
'#markup' => $this->t('The specified AI Assistant could not be loaded.'),
];
}
// Proceed with setting the assistant and building the block.
$this->aiAssistantRunner->setAssistant($assistant);
$this->aiAssistantRunner->streamedOutput($this->configuration['stream']);
$block = [];
Loading