array_rand(): Argument #1 ($array) must be of type array, null given

Created on 25 November 2023, 7 months ago
Updated 11 January 2024, 6 months ago

Problem/Motivation

I'll add more later and add a patch later when I get a chance.

When using the openai_devel to generate some Articles, the default content type with the default install, I would get this error
array_rand(): Argument #1 ($array) must be of type array, null given and could not generate any content.

I don't have the full error message because my local isn't running, but it pointed to /openai_devel/src/Plugin/DevelGenerate/ContentGPTDevelGenerate.php Line 223 which is $node_type = array_rand($results['node_types']);.

Doing some step debugging I saw that $results['node_types'] was not always an array and array_rand() is causing the error because $results['node_types'] is a string and not an array.

Steps to reproduce

  1. Default install and enable openai_devel
  2. Enable pathauto
  3. Enter your openai key and id in config
  4. Try generating a couple of Articles (only Articles) entering body as a field, select generate path and select a user

Proposed resolution

Check that $results['node_types'] is an array before using array_rand().

    if (is_array($results['node_types'])) {
      $node_type = array_rand($results['node_types']);
    }
    else {
      $node_type = $results['node_types'];
    }

Step debugging was ery slow because the API calls to OpenAI took a long time. I suppose there could be another reason that $results['node_types'] is not always an array but sometimes a string.

Remaining tasks

User interface changes

API changes

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇺🇸United States kamkejj WI

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

Comments & Activities

Production build 0.69.0 2024