Hello , i'm a little annoyed when adding a user to my Drupal site for a few versions. Here is the error message:

The website encountered an unexpected error. Try again later.

TypeError: array_diff(): Argument #1 ($array) must be of type array, int given in array_diff() (line 349 of modules/contrib/permissions_by_term/permissions_by_term.module).

Thanks

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

themisterprof created an issue. See original summary.

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

abhishek_gupta1’s picture

Status: Active » Needs review

@themisterprof , I have fixed the issue and created MR, please revie wit once

mbayeux’s picture

The website encountered an unexpected error. Try again later.

TypeError: Unsupported operand types: array + int in permissions_by_term_user_form_submit() (line 354 of modules/contrib/permissions_by_term/permissions_by_term.module).

mbayeux’s picture

Status: Needs review » Needs work
vitaliyb98’s picture

Provided fix isn't working

vitaliyb98 changed the visibility of the branch 3529738-issue-with-array-diff to hidden.

vitaliyb98 changed the visibility of the branch 3529738-issue-with-array-diff to active.

vitaliyb98’s picture

Status: Needs work » Needs review
StatusFileSize
new705 bytes

Something is wrong with GitLab during pushing to the fork (returns 500 error). Added a patch that fixed that error with array_diff.

baluertl’s picture

Title: Argument #1 ($array) must be of type array, int given in array_diff() » Better preparation of 'permissions_by_term_user_form_submit()' hook to values received
baluertl’s picture

StatusFileSize
new1.63 KB

I've encountered multiple PHP warnings in this module while working on a Drupal 10.4 site with PHP 8.3. After investigating the various reports in this issue, I've prepared a combined patch that addresses all the related type errors and also includes a minor cleanup.

Issues fixed in this patch

  1. foreach() type error (line 336/339) - Warning when creating new users with no taxonomy terms selected
  2. array_diff() type error (line 341/345) - Warning when #default_value is not an array
  3. Code cleanup - Removed duplicate empty @file docblock (lines 3-5)

Technical Analysis

The root cause is that when multi-select form fields have no selections, Drupal returns an empty string instead of an empty array. This causes type errors in two places:

Aspect Fix #1:
#default_value validation (line 344) by @vitaliyb98
Fix #2:
$tids validation (line 336)
Target variable #default_value from form structure $tids from $form_state->;getValue('terms')
Problem addressed Form structure integrity issue Runtime data type issue from user input
Error prevented array_diff() argument type error foreach() argument type warning
Trigger scenario Editing users OR corrupted form state Creating NEW users (no terms selected)
Solution applied isset() && is_array() double validation $tids = is_array($tids) ? $tids : [];

Why both fixes are needed

  • Defense in depth - Multiple layers of type validation
  • Different data sources - User input vs. form structure
  • Complementary protection - Neither fix alone would prevent both errors
  • PHP 8.x strict typing - Makes these validations essential

Additional improvements

  • Removed the redundant $newTerms variable – now uses $tids directly
  • Simplified logic by moving type check earlier in the flow
  • Removed duplicate empty @file docblock at the top of the file

This patch has been tested on:

  • Drupal 10.4.7
  • PHP 8.3.27
  • permissions_by_term 3.1.37

Scenarios tested:

  • ✅ Creating new users with no taxonomy terms selected
  • ✅ Creating new users with taxonomy terms selected
  • ✅ Editing existing users
  • ✅ Changing taxonomy term selections

All PHP warnings mentions by the other commenters above have been eliminated in these scenarios. I hope this helps others encountering the same issues.

vitaliyb98’s picture

Hi @baluertl, thanks for contributing! The code looks good, but it would be great to add tests for it.

vitaliyb98’s picture

Status: Needs review » Needs work

vitaliyb98’s picture

Status: Needs work » Reviewed & tested by the community

Added changes from the patch into the MR, tested

LGTM

  • 66c02085 committed on 3.1.x-dev
    feat: #3529738 Better preparation of '...
vitaliyb98’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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