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).
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | do_username-3394985-13.patch | 527 bytes | jagraj_singh_gill |
| #5 | do_username-3394985-1.patch | 529 bytes | jagraj_singh_gill |
| #4 | do_username-3394985.patch | 3.31 KB | sarwan_verma |
Issue fork do_username-3394985
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:
- 3394985-attempt-to-read
changes, plain diff MR !5
Comments
Comment #2
shreya_th commentedComment #4
sarwan_verma commentedHi @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.
Comment #5
jagraj_singh_gill commentedHello @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 !!
Comment #7
shreya_th commented@sarwan_verma i have applied your patch on my local but it doesn't resolved my error.
Comment #8
shreya_th commentedHi @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!
Comment #9
hussainwebThanks for the fixes.
@Shreya_th, I see your patch and MR are different. The patch has accidentally changed the indentation.
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).
Comment #10
hussainweb@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.
Comment #11
shreya_th commentedHi @hussainweb,
Sorry for the mistake, I have made changes as per your requirement . kindly review the changes.
Comment #12
hussainwebThanks, @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.
Comment #13
jagraj_singh_gill commentedHi @hussainweb,
Thank you for your feedback. I have revised my patch and also attached it. Please review and verify. Thanks again !!
Comment #15
hussainwebCommitted and pushed. Thanks!