Problem/Motivation

The module generates an error in some cases.
Warning: Attempt to read property "value" on array in Drupal\do_username\DOComputedFields->getValue() (line 37 of /app/web/modules/contrib/do_username/src/DOComputedFields.php).

Steps to reproduce

Set the field value to a user where the bio is not set.

Proposed resolution

Check if the `field_bio` exists and contains properties before attempting to read them.

Remaining tasks

Fix the error. We can use the null-safe operator here (making it a single-character fix).

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hussainweb created an issue. See original summary.

shreya_th’s picture

Assigned: Unassigned » shreya_th

jagraj_singh_gill made their first commit to this issue’s fork.

sarwan_verma’s picture

StatusFileSize
new3.31 KB

Hi @hussainweb,
I have fixed this issue " Attempt to read property "value" on array in Drupal\do_username\DOComputedFields->getValue()" and also attached patch ,
please review and verify.

jagraj_singh_gill’s picture

StatusFileSize
new529 bytes

Hello @hussainweb,
I have fixed the issue and also attached the patch. I feel there was only one change required and we can use null-coalescing operator there which will fix the issue for us.
Please review and verify.

Thank You !!

shreya_th’s picture

Assigned: shreya_th » Unassigned
Status: Active » Needs review

@sarwan_verma i have applied your patch on my local but it doesn't resolved my error.

shreya_th’s picture

Hi @hussainweb,
I have fixed this issue and it is also working fine on my local . Also created MR for this issue . Kindly review the changes.

Thank you!

hussainweb’s picture

Thanks for the fixes.

@Shreya_th, I see your patch and MR are different. The patch has accidentally changed the indentation.

+++ b/src/DOComputedFields.php
@@ -33,42 +33,59 @@ class DOComputedFields extends TypedData {
+    return is_array($userInformation->field_bio) && isset($userInformation->field_bio[0]['value']) ? $userInformation->field_bio[0]['value'] : 'N/A';

I see you are treating the `$userInformation` as a Drupal field value. It is not. It is a simple PHP class and the `field_bio` (magic) property is not an array (unless it is empty). So the above code will always return "N/A". Yes, it won't throw an error.

In your MR, on the other hand, you are using a function called `get` but that function doesn't exist on the `User` class at all (nor on the base class).

hussainweb’s picture

Status: Needs review » Needs work

@jagraj_singh_gill, your change is good in theory but we should return an empty string rather than `NULL`. This value gets used within Drupal and I am worried this NULL will throw other errors. We only hit this case when the bio is empty on Drupal.org. In that case, empty string is good enough. Please make this change and I can commit.

shreya_th’s picture

Hi @hussainweb,
Sorry for the mistake, I have made changes as per your requirement . kindly review the changes.

hussainweb’s picture

Thanks, @Shreya_th. I am afraid this doesn't work either. You are reading the expression into a variable and the warning is generated at that point. By the time you check for `!empty`, the warning is already generated.

The fix is to use the null-coalescing operator.

jagraj_singh_gill’s picture

StatusFileSize
new527 bytes

Hi @hussainweb,
Thank you for your feedback. I have revised my patch and also attached it. Please review and verify. Thanks again !!

hussainweb’s picture

Status: Needs work » Fixed

Committed and pushed. Thanks!

Status: Fixed » Closed (fixed)

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