Problem/Motivation

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).

Steps to reproduce

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()
        );
      }
    }

Proposed resolution

Simply don't call ->send(). The redirect still kicks in by being returned as a page response.

Remaining tasks

Test/review

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Comments

khaled.zaidan created an issue. See original summary.

khaled.zaidan’s picture

StatusFileSize
new553 bytes

Patch for the fix suggested.

khaled.zaidan’s picture

Status: Active » Needs review