Error: Call to a member function hasTranslation() on null in Drupal\chatgpt_plugin\Form\ChatGPTTranslateForm->buildForm() (line 95 of modules/contrib/chatgpt_plugin/src/Form/ChatGPTTranslateForm.php).

Created on 7 September 2023, 10 months ago

Problem/Motivation

Based on the following piece of code from lines 82 to 93 in modules/contrib/chatgpt_plugin/src/Form/ChatGPTTranslateForm.php, the Chat GPT translation UI seems to be specific to node entity types. See line 87 where $node = $this->entityTypeManager->getStorage('node')->load($node_id):

    foreach ($languages as $langcode => $language) {
      $option = array_shift($overview['#rows']);
      $lang_name = $language->getName();
      $isDefault = $language->isDefault();
      $node_id = $form_state->get('entity')->id();
      $node = $this->entityTypeManager->getStorage('node')->load($node_id);
      $label = "Translate using ChatGPT";
      $row_title = Link::createFromRoute($label, 'chatgpt_plugin.translate_content', [
        'lang_code' => $langcode,
        'lang_name' => $lang_name,
        'node_id' => $node_id,
      ])->toString();

Therefore, when translating non node entity types such as taxonomy terms, the Chat GPT translation UI attempts to run but encounters the following error because it is trying to load a node entity type when in this case, the entity id it is loading is of a taxonomy entity type.

Steps to reproduce

If you have translation enabled for taxonomy terms, go to view any term and click on the translation tab, ex url: /taxonomy/term/3/translations and you will see the following error:

Error: Call to a member function hasTranslation() on null in Drupal\chatgpt_plugin\Form\ChatGPTTranslateForm->buildForm() (line 95 of modules/contrib/chatgpt_plugin/src/Form/ChatGPTTranslateForm.php).

Proposed resolution

To resolve this issue, we must prevent the Chat GPT translation UI from loading for non node entities and add a conditional to ensure that the Chat GPT UI only loads for node entity types. There is a an override file at /chatgpt_plugin/src/Controller/ContentTranslationControllerOverride.php that overrides the translation UI and inserts the Chat GPT UI for any given content with translation enabled. If we want to ensure Chat GPT translation only runs for node entity types, then we will need to add a conditional to only allow the override to run if the entity type is node.

The following file at /chatgpt_plugin/src/Controller/ContentTranslationControllerOverride.php should be changed from:


namespace Drupal\chatgpt_plugin\Controller;

use Drupal\content_translation\Controller\ContentTranslationController;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Overridden class for entity translation controllers.
 */
class ContentTranslationControllerOverride extends ContentTranslationController {

  /**
   * {@inheritdoc}
   */
  public function overview(RouteMatchInterface $route_match, $entity_type_id = NULL) {
    $build = parent::overview($route_match, $entity_type_id);
    $build = \Drupal::formBuilder()->getForm('Drupal\chatgpt_plugin\Form\ChatGPTTranslateForm', $build);
    return $build;
  }

}

to


namespace Drupal\chatgpt_plugin\Controller;

use Drupal\content_translation\Controller\ContentTranslationController;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Overridden class for entity translation controllers.
 */
class ContentTranslationControllerOverride extends ContentTranslationController {

  /**
   * {@inheritdoc}
   */
  public function overview(RouteMatchInterface $route_match, $entity_type_id = NULL) {
    $build = parent::overview($route_match, $entity_type_id);
    // Make sure Chat GPT translation function only applies for entity type node
    if ($entity_type_id == "node") {
      $build = \Drupal::formBuilder()->getForm('Drupal\chatgpt_plugin\Form\ChatGPTTranslateForm', $build);
    }
    return $build;
  }

}

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States Bao Truong

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

Comments & Activities

Production build 0.69.0 2024