Not send the redirect response

Created on 12 December 2023, about 1 year ago

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

🐛 Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

🇬🇧United Kingdom khaled.zaidan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024