- Issue created by @HitchShock
- Status changed to Needs review
8 months ago 10:39am 15 March 2024 - Status changed to Needs work
4 months ago 2:43pm 28 July 2024 - π¬π§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