Give Default value to all $agent keys

Created on 8 January 2024, 6 months ago
Updated 22 May 2024, about 1 month ago

We are seeing several errors with regard to keys not set:

1) Warning: Undefined array key "_cvar" in Drupal\visitors\Service\TrackerService->write() (line 112 of /modules/contrib/visitors/src/Service/TrackerService.php) [ addressed in issue https://www.drupal.org/project/visitors/issues/3389685 ๐Ÿ“Œ Three different errors displayed Active ]

2) The various errors reported here: https://www.drupal.org/project/visitors/issues/3380760 ๐Ÿ› Warning: Undefined array key Fixed

In ADDITION we are seeing these new errors:

3) Warning: Undefined array key "url" in Drupal\visitors\Service\TrackerService->write() (line 129 of \modules\contrib\visitors\src\Service\TrackerService.php)
4) Warning: Undefined array key "uid" in Drupal\visitors\Service\TrackerService->write() (line 126 of \modules\contrib\visitors\src\Service\TrackerService.php)

The underlying issue, in all cases, seems to be that $agent may not always have all values set.

Therefore, I would suggest to give reasonable default values for all $agent values, just in case if one is missing.

In our case, I added this code, just to get past this, but there are other $agent values, and these should have defaults as well. I am not sure what you may use as a "reasonable default", but all values should get one.

if ( !isset($agent['uid']))
        $agent['uid'] = \Drupal::currentUser()->id();

if ( !isset($agent['url'])){
      $current_path = \Drupal::service('path.current')->getPath();
      $agent['url'] = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
}
๐Ÿ› Bug report
Status

Fixed

Version

2.16

Component

Code

Created by

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024