Prerequisites
- Ensure you have a functioning Drupal installation and administrative access.
- Sign up for IBM Cloud and create an IBM Watson service instance (e.g., Watson Assistant, Watson Language Translator).
- Install the necessary modules in Drupal for integration, such as the JSON:API module for web service interaction.
Install Required Modules
- Navigate to the Drupal admin interface and go to Extend
- Search for the JSON:API module and enable it.
- Use Drush or Composer to manage modules if preferred. You can use a command like:
composer require drupal/jsonapi
Obtain IBM Watson Credentials
- In your IBM Cloud Dashboard, locate your Watson service instance and retrieve the API Key and Endpoint URL.
- Store these credentials securely, as they'll be needed for API requests.
Configure Drupal for IBM Watson Integration
- Create a custom module in Drupal if one doesn't exist. Navigate to the modules/custom directory and create a folder named ibm_watson_integration.
- Create an .info.yml file inside your module directory:
name: 'IBM Watson Integration'
type: module
description: 'Custom module to integrate IBM Watson services.'
package: Custom
core_version_requirement: ^8 || ^9
dependencies:
- drupal:jsonapi
- Create a .module file in the same directory to write custom functions and interaction logic.
Create a Service for Watson API Interaction
- Inside the src directory of your custom module, create a directory named Service and add a PHP file WatsonClient.php.
namespace Drupal\ibm_watson_integration\Service;
use GuzzleHttp\ClientInterface;
use Drupal\Component\Serialization\Json;
class WatsonClient {
protected $httpClient;
protected $apiKey;
protected $endpoint;
public function __construct(ClientInterface $http_client) {
$this->httpClient = $http_client;
$this->apiKey = 'YOUR_WATSON_API_KEY';
$this->endpoint = 'YOUR_WATSON_ENDPOINT_URL';
}
public function queryWatson($query) {
$response = $this->httpClient->request('POST', $this->endpoint, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->apiKey,
],
'body' => Json::encode(['text' => $query]),
]);
return Json::decode($response->getBody());
}
}
Register the Service
- Create a services.yml file in your module directory and register the WatsonClient service.
services:
ibm_watson_integration.watson_client:
class: 'Drupal\ibm_watson_integration\Service\WatsonClient'
arguments: ['@http_client']
Use the Service in a Custom Block
- Create a Plugin/Block directory within your module’s src directory and add a PHP file WatsonBlock.php.
namespace Drupal\ibm_watson_integration\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ibm_watson_integration\Service\WatsonClient;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
/**
* Provides a 'Watson Query Block' Block.
*
* @Block(
* id = "watson_query_block",
* admin_label = @Translation("Watson Query Block"),
* )
*/
class WatsonBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $watsonClient;
public function __construct(array $configuration, $plugin_id, $plugin_definition, WatsonClient $watson_client) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->watsonClient = $watson_client;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('ibm_watson_integration.watson_client')
);
}
public function build() {
$result = $this->watsonClient->queryWatson('Hello World!');
return [
'#markup' => $this->t('Response from IBM Watson: @response', ['@response' => $result['response']]),
];
}
}
Enable the Module and Clear Cache
- Enable your custom module via the command-line or the Drupal admin UI.
- Clear the Drupal cache to ensure all configurations are reloaded.
drush en ibm_watson_integration
drush cache:rebuild
Test the Integration
- Add the custom block (Watson Query Block) to a page through the block layout UI.
- Verify that the block displays the correct response from IBM Watson to ensure successful integration.
By following these steps, you achieve a successful integration of IBM Watson services with Drupal, allowing for enhanced AI capabilities directly within your content management system.