Skip to content
Snippets Groups Projects

Issue #3479540:

Merged Baris Tosun requested to merge issue/sidekick-3479540:3479540-alt-text-generation into 2.0.x
1 file
+ 25
9
Compare changes
  • Side-by-side
  • Inline
+ 25
9
@@ -15,6 +15,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\file\Entity\File;
use Drupal\Core\Url;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\RequestOptions;
use Drupal\sidekick\ImageWidget;
@@ -859,20 +860,35 @@ function sidekick_image_ajax_callback(
[
"identifier" => "url",
"value" => $image_url ?? '',
// todo handle api response errors as validation.
],
],
]);
$request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/json';
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/json';
$request_options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $sidekick_api_key;
$response = $client->request('POST', $api_url, $request_options);
$data = Json::decode((string) $response->getBody());
$data_from_sidekick = $data['data']['message']['message'] ?? '';
$response = new AjaxResponse();
$response->addCommand(
new InvokeCommand($element, 'val', [$data_from_sidekick])
);
return $response;
try {
$response = $client->request('POST', $api_url, $request_options);
$data = Json::decode((string) $response->getBody());
$data_from_sidekick = $data['data']['message']['message'] ?? '';
$response = new AjaxResponse();
$response->addCommand(
new InvokeCommand($element, 'val', [$data_from_sidekick])
);
return $response;
}
catch (ClientException $e) {
$response = new AjaxResponse();
return $response->addCommand(
new MessageCommand(
json_decode($e->getResponse()
->getBody()
->getContents())->message,
'.sidekick-message',
['type' => 'error']
)
);
}
}
return [];
Loading