Notice: Undefined index in user_history_diff_user_history() (line 296 of /var/www/html/docroot/modules/contrib/user_history/user_history.module)

Created on 31 March 2023, over 1 year ago
Updated 14 June 2023, over 1 year ago

On user save the error happens for my config

It can be easily fixed by adding a check

...
foreach ($attached_fields as $field_name => $tracked) {
--  if ($tracked) {
++  if ($tracked && !empty($field_definitions[$field_name])) {
  /** @var \Drupal\field\Entity\FieldConfig $fieldConfig */
  $fieldConfig = $field_definitions[$field_name];
...
🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇩🇪Germany Rudi Teschner

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

Comments & Activities

  • Issue created by @Rudi Teschner
  • Status changed to Fixed over 1 year ago
  • 🇳🇿New Zealand jlscott

    @Rudi Teschner: Thanks for picking this up and for the patch. I have accepted and committed the patch.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed over 1 year ago
  • 🇩🇪Germany Rudi Teschner

    I fear the patch I suggested was just handling the error, but not dealing with the origin of the problem.

    I have fields where changes need to be tracked and they are listed in $attached_fields. So far, so good.
    But while looping through $attached_fields they are missing from $field_definitions in function user_history_create_user_history and therefore cannot be dealt with

    // Get a list of fields attached to the user_history entity.
      $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('user_history', 'user_history');

    The patch above deals with the error. But the problem is that the field_definitions should be made available at this point so that the values can be tracked.

    Has this worked for fields added via field api before?

  • 🇩🇪Germany Rudi Teschner

    My mistake. I forgot to initialise the user_history database via user_history/update :)

Production build 0.71.5 2024