TypeError and Undefined Variable in OpenAiCustomField with custom_field Module

Created on 9 July 2024, 5 months ago
Updated 14 July 2024, 4 months ago

When attempting to add a custom field using the custom_field module, I encounter a TypeError when trying to save the field prefix and label. Additionally, there is a warning about an undefined variable $form in the CustomField class.

Steps to Reproduce:

  1. Install and enable the custom_field module.
  2. Install and enable the ai_interpolator_openai module (dev version).
  3. Navigate to the field configuration for a content type.
  4. Add a new custom field.
  5. Attempt to save the field prefix and label.

Expected Behavior:

The custom field should save without errors.

Actual Behavior:

The following error message is displayed when saving the field prefix and label:

        
The website encountered an unexpected error. Try again later.

TypeError: array_merge(): Argument #1 must be of type array, null given in array_merge() (line 35 of modules/contrib/ai_interpolator_openai/src/Plugin/AiInterpolatorFieldRules/OpenAiCustomField.php).
Drupal\ai_interpolator_openai\Plugin\AiInterPolatorFieldRules\OpenAiCustomField->extraAdvancedFormFields() (Line: 367)
Drupal\ai_interpolator\FormAlter\AiInterpolatorFieldConfig->alterForm() (Line: 16)
ai_interpolator_form_field_config_edit_form_alter() (Line: 552)
Drupal\Core\Extension\ModuleHandler->alter() (Line: 834)
Drupal\Core\Form\FormBuilder->prepareForm() (Line: 285)
Drupal\Core\Form\FormBuilder->buildForm() (Line: 73)
Drupal\Core\Controller\FormController->getContentResult() (Line: 39)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 638)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 100)
Drupal\wire\StackMiddleware\RegisterWireMiddleware->handle() (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741)
Drupal\Core\DrupalKernel->handle() (Line: 19)
        

Proposed Solution:

To resolve the TypeError, ensure that both $baseForm and $form are always arrays before using array_merge().

        
public function extraAdvancedFormFields(ContentEntityInterface $entity, FieldDefinitionInterface $fieldDefinition) {
  $baseForm = $this->addCustomFormFields('openai', $entity, $fieldDefinition) ?? [];
  $form = $this->loadExtraAdvancedFormFields($entity, $fieldDefinition) ?? [];
  $form = array_merge($baseForm, $form);
  return $form;
}
        

To address the undefined variable $form warning in the addCustomFormFields method, ensure that $form is initialized properly:

        
<?php

namespace Drupal\ai_interpolator\PluginBaseClasses;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;

abstract class CustomField {
  // Other methods...

  /**
   * Adds custom form fields.
   *
   * @param string $type
   *   The type of the custom field.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition
   *   The field definition.
   *
   * @return array
   *   The form array.
   */
  public function addCustomFormFields($type, ContentEntityInterface $entity, FieldDefinitionInterface $fieldDefinition) {
    // Initialize $form as an empty array
    $form = [];

    // Your existing logic to add custom form fields...
    
    // Example logic (ensure you replace this with actual implementation)
    if ($type == 'openai') {
      $form['custom_field'] = [
        '#type' => 'textfield',
        '#title' => t('Custom Field'),
        '#default_value' => '',
      ];
    }

    return $form;
  }

  // Other methods...
}
        

Remaining Issue:

Despite the above solutions, a warning regarding the undefined variable $form persists. Further investigation is needed to ensure that $form is properly defined and used within all relevant methods and classes.

Additional Information:

  • Drupal version: 10.x
  • AI Interpolator module version: Dev version
  • Custom Field module version: Dev version

The above changes should address the primary TypeError and improve the handling of form arrays. The remaining warning about the undefined variable requires further debugging to identify all instances where $form needs initialization.

Thank you for your help in resolving these issues.

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡°πŸ‡¬Kyrgyzstan sahaj

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

Comments & Activities

  • Issue created by @sahaj
  • ananya.k β†’ made their first commit to this issue’s fork.

  • Hi @sahaj,

    I successfully resolved the issues on the local environment. Please refer to the attached patch for the details.

    Thanks!

  • Status changed to Needs review 5 months ago
  • πŸ‡°πŸ‡¬Kyrgyzstan sahaj
  • πŸ‡°πŸ‡¬Kyrgyzstan sahaj

    Thanks @ananya.k. Just noticed that I messed it up initially posting on the parent module, instead of ai_interpolator_openai.

    However, with the patch I'm getting the followint error:

    The website encountered an unexpected error. Try again later.
    
    Error: Call to undefined method Drupal\ai_interpolator\Rulehelpers\GeneralHelper::getImageStyles() in Drupal\ai_interpolator_openai\Plugin\AiInterPolatorFieldRules\OpenAiCustomField->getModelsForForm() (line 75 of modules/contrib/ai_interpolator_openai/src/OpenAiTrait.php).
    Drupal\ai_interpolator_openai\Plugin\AiInterPolatorFieldRules\OpenAiCustomField->loadExtraAdvancedFormFields() (Line: 38)
    Drupal\ai_interpolator_openai\Plugin\AiInterPolatorFieldRules\OpenAiCustomField->extraAdvancedFormFields() (Line: 367)
    Drupal\ai_interpolator\FormAlter\AiInterpolatorFieldConfig->alterForm() (Line: 16)
    ai_interpolator_form_field_config_edit_form_alter() (Line: 552)
    Drupal\Core\Extension\ModuleHandler->alter() (Line: 834)
    Drupal\Core\Form\FormBuilder->prepareForm() (Line: 285)
    Drupal\Core\Form\FormBuilder->buildForm() (Line: 48)
    Drupal\Core\Entity\EntityFormBuilder->getForm() (Line: 62)
    Drupal\field_ui\Controller\FieldConfigAddController->fieldConfigAddConfigureForm()
    call_user_func_array() (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 638)
    Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
    Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 100)
    Drupal\wire\StackMiddleware\RegisterWireMiddleware->handle() (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741)
    Drupal\Core\DrupalKernel->handle() (Line: 19)
    
Production build 0.71.5 2024