Error message on Drupal 10.2

Created on 15 March 2024, 8 months ago
Updated 28 July 2024, 4 months ago

Problem/Motivation

We receive error messages on the /admin/structure/../fields/add-field pages
Errors:
Warning: Undefined array key "#options" in computed_field_plugin_form_alter() (line 61 of modules/contrib/computed_field_plugin/computed_field_plugin.module).
Warning: foreach() argument must be of type array|object, null given in computed_field_plugin_form_alter() (line 61 of modules/contrib/computed_field_plugin/computed_field_plugin.module).

This happens cuz of ✨ Make field selection less overwhelming by introducing groups Fixed

Steps to reproduce

  1. Install the module
  2. Go to the node Add field page. E.g. /admin/structure/types/manage/page/fields/add-field
  3. Actual: we see errors on the page
  4. Expected: there aren't errors on the page

Proposed resolution

Implement a solution according to the new behaviour and keep the old one for the older Drupal versions.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine HitchShock Ukraine

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

Merge Requests

Comments & Activities

  • Issue created by @HitchShock
  • Merge request !5[#3428049] Fixed D10.2 error β†’ (Open) created by HitchShock
  • Status changed to Needs review 8 months ago
  • πŸ‡ΊπŸ‡¦Ukraine HitchShock Ukraine
  • Status changed to Needs work 4 months ago
  • πŸ‡ΊπŸ‡¦Ukraine vlad.dancer Kyiv
  • πŸ‡¬πŸ‡§United Kingdom altcom_neil

    Hi,

    Just to confirm this patch is causing a fatal error in D10.3.5 / PHP8.2+ due to trying to look for an array key in a NULL.

    I would suggest that the whole function is replaced with one that uses hook_form_FORM_ID_alter as there is no reason for it to run for any form but the one that is being checked for. Then it checks for the array existing before trying to check for the key.

    diff --git a/computed_field_plugin.module b/computed_field_plugin.module
    index 5a86592..8b14ee4 100644
    --- a/computed_field_plugin.module
    +++ b/computed_field_plugin.module
    @@ -47,39 +47,38 @@ function computed_field_plugin_entity_bundle_field_info(EntityTypeInterface $ent
     }
     
     /**
    - * Implements hook_form_alter().
    + * Implements hook_form_FORM_ID_alter() for field_ui_field_storage_add_form.
      */
    -function computed_field_plugin_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    -  /**
    +function computed_field_plugin_form_field_ui_field_storage_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    +  /*
        * Remove Computed render array field type from available options.
        *
        * @todo: Remove this alteration when related core issue is fixed.
        * @link: https://www.drupal.org/project/drupal/issues/2932273
        */
    -  if ($form_id === 'field_ui_field_storage_add_form') {
    -    if (version_compare(\Drupal::VERSION, '10.1.999', '<')) {
    -      // Remove Computed render array field type from "Add a new field".
    -      foreach ($form['add']['new_storage_type']['#options'] as $category => $fields) {
    -        foreach ($fields as $key => $value) {
    -          if ($key === 'computed_render_array') {
    -            unset($form['add']['new_storage_type']['#options'][$category][$key]);
    -          }
    +  if (version_compare(\Drupal::VERSION, '10.1.999', '<')) {
    +    // Remove Computed render array field type from "Add a new field".
    +    foreach ($form['add']['new_storage_type']['#options'] as $category => $fields) {
    +      foreach ($fields as $key => $value) {
    +        if ($key === 'computed_render_array') {
    +          unset($form['add']['new_storage_type']['#options'][$category][$key]);
             }
           }
    +    }
     
    -      // Remove Computed render array field type from "Re-use an existing field".
    -      if (isset($form['add']['existing_storage_name']) && isset($form['add']['existing_storage_name']['#options'])) {
    -        foreach ($form['add']['existing_storage_name']['#options'] as $key => $value) {
    -          $arguments = $value->getArguments();
    -          if ($arguments['@type']->getUntranslatedString() === 'Computed render array') {
    -            unset($form['add']['existing_storage_name']['#options'][$key]);
    -          }
    +    // Remove Computed render array field type from "Re-use an existing field".
    +    if (isset($form['add']['existing_storage_name']) && isset($form['add']['existing_storage_name']['#options'])) {
    +      foreach ($form['add']['existing_storage_name']['#options'] as $key => $value) {
    +        $arguments = $value->getArguments();
    +        if ($arguments['@type']->getUntranslatedString() === 'Computed render array') {
    +          unset($form['add']['existing_storage_name']['#options'][$key]);
             }
           }
         }
    -    elseif (array_key_exists('computed_render_array', $form['add']['new_storage_type'])) {
    -      // Remove Computed render array field type from "Add a new field".
    -      unset($form['add']['new_storage_type']['computed_render_array']);
    -    }
    +  }
    +  elseif (isset($form['add']['new_storage_type']) && is_array($form['add']['new_storage_type'])
    +    && array_key_exists('computed_render_array', $form['add']['new_storage_type'])) {
    +    // Remove Computed render array field type from "Add a new field".
    +    unset($form['add']['new_storage_type']['computed_render_array']);
       }
     }
    

    Attached is the patch for 8.x-1.4.

    Cheers, Neil

Production build 0.71.5 2024