- Issue created by @khaled.zaidan
- Status changed to Needs review
about 1 year ago 3:36pm 12 December 2023
It enough to return the redirect response in ChatGPTTranslateController. There's no need to call ->send() prior to the end of the function.
Calling ->send() results in the redirect kicking in too early and preventing a potential redirect or functionality from another module.
In my example, I'm building a functionality to translate into multiple languages in one go and I've leveraging the controller. This ->send() is resulting in the redirect kicking after only the first translation (I know, ideally the whole translate functionality is better moved into a service that I could call directly... and maybe that's a good thing to do down the line, but not calling ->send() is still a valid change as it is not needed).
Run the following code:
$chatgpt_translate_controller = \Drupal\chatgpt_plugin\Controller\ChatGPTTranslateController::create(\Drupal::getContainer());
$languages = \Drupal::languageManager()->getLanguages();
foreach ($languages as $language) {
$existing_translations = $entity->getTranslationLanguages();
$translation_exists = !empty($existing_translations[$language->getId()]);
if (!$translation_exists) {
$chatgpt_translate_controller->translate(
$language->getId(),
$language->getName(),
$entity->id()
);
}
}
Simply don't call ->send(). The redirect still kicks in by being returned as a page response.
Test/review
N/A
N/A
N/A
Needs review
2.0
Code