PHP Notice: Undefined offset in _civicrm_member_roles_sync()
Overview
This is an issue that I have noticed in several Drupal 7 / CiviCRM websites. When the CiviCRM Member Roles Synch activity takes place, my recent log messages get inundated with PHP Notices:
Notice: Undefined offset: x in _civicrm_member_roles_sync() (line 607 of /civicrm/drupal/modules/civicrm_member_roles/civicrm_member_roles.module).
Reproduction steps
- Enable civicrm member roles sync.
- Add a rule to synch some member type to a role.
- Configure to run at Drupal Cron
- Run Cron and observe as Reports -> Recent Log Messages get filled with notices.
Steps to Resolve
line 607 of /civicrm/drupal/modules/civicrm_member_roles/civicrm_member_roles.module contains the following statement:
if (is_array($memberroles[$membership['membership_type_id']])) {
If the array is not set then the notice will be raised so if we check it first as follows, the notice goes away:
if (isset($memberroles[$membership['membership_type_id']]) && is_array($memberroles[$membership['membership_type_id']])) {
Environment information
- CiviCRM: 5.54.0
- PHP: 7.4/8.1_
- CMS: Drupal 7.92
- Database: MySQL 5.7.4
- Web Server: Apache 2.x