willowf's picture

Hello community,

I'm facing an issue when attempting to add a rule in the CiviCRM Member Roles module and would appreciate your help in resolving it.

Problem Details:

  • Context: I am using Backdrop CMS and have configured the CiviCRM Member Roles module.
  • Steps to reproduce the problem:
    1. Access the rules configuration form.
    2. Try to add a new rule.
    3. Encounter an error message related to line 454 of the file "civicrm_member_roles.module."
  • Error Message: "TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in array_keys() (line 454 of httpdocs/modules/civicrm/backdrop/modules/civicrm_member_roles/civicrm_member_roles.module)."

     

Comments

This is an issue with PHP 8.1, which produces a fatal error when trying to get the array_keys of null. This happens when you add your first rule.

You should open an issue in the CiviCRM issue queue. I've never used it, so I'm not sure where people report problems.

If you want to temporarily solve this problem you can either switch to PHP 7.4, or manually patch the function civicrm_member_roles_add_rule_form_submit() as follows (this is in file civicrm_member_roles.module):

Starting on line 451:

// If edit_flag is not set then get next highest number.
  $config = config('civicrm_member_roles.settings');
  if (empty($form_state['values']['edit_flag'])) {
    if (empty($config->get('rules'))) {
      $highest_rule_id = 0;
    }
    else {
      $highest_rule_id = max(array_keys($config->get('rules')));
    }
    $id = $highest_rule_id + 1;
  }
  else {
    $id = (int) $form_state['values']['edit_flag'];
  }

 

willowf's picture

Thank you for the help... It has worked... but I have encountered the same issue with another module, specifically the Flags module, when trying to create rules with its module. I had to create rules directly using the Rules module.

Thanks

 

Glad it worked.

Well, you should post an issue in the flag module's issue queue, then. This type of problem is not uncommon when PHP 8.1 became the default one people use nowadays. Some modules lag behind updating the code to avoid this kind of problems.

For developers, it's really very useful when users like you post issues and report problems. That's the only way the will get solved. Please take the time to report anything you find. Posting in the Forum is OK too, but ideally, you should also create issues in the queues.

[EDIT] I see that there is already a report of this type of problem in flag.

[Edited my answer to fix some typos, like the line number for the fix]

Laryn, the linked PR is similar to my suggested changes, but unfortunately it uses the null coalescing operator, which will not work for Backdrop, since we still support PHP 5.6 

I've merged the PR linked from stackexchange.