Problem/Motivation

When Comment Notify loads the comment form, it retrieves the current user’s comment_notify preference.

The module assumes that a user with stored Comment Notify settings has both entity_notify and comment_notify values. However, users can have only entity_notify stored. In that case, UserNotificationSettings::getSetting() attempts to access a missing array key and PHP emits:

Warning: Undefined array key "comment_notify" in src/UserNotificationSettings.php

This was observed while users reviewed unpublished Article content with an enabled comment field.

Steps to reproduce

  1. Install Comment Notify 8.x-1.5.
  2. Enable Comment Notify for an entity bundle with a comment field.
  3. Create a user with permission to subscribe to comments.
  4. Store only an entity_notify preference for that user, without storing comment_notify.
  5. Visit content where the enabled comment form is displayed as that user.

For example, the incomplete preference state can be created with:

$user_settings->saveSettings($uid, COMMENT_NOTIFY_ENTITY);

Proposed resolution

When a stored settings array does not contain the requested preference, merge the configured default settings before returning the requested value.

This preserves explicitly stored preferences and uses the configured default only for the missing key.

The attached patch:

  • Updates src/UserNotificationSettings.php to apply defaults for an individual missing preference.
  • Adds a functional regression test for a user with only entity_notify stored.

Remaining tasks

  • Review and test the attached patch.

User interface changes

None.

API changes

None.

Data model changes

None.

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

jwilson3 created an issue. See original summary.

jwilson3’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.35 KB

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

gnuget’s picture

I moved the fallback to UserNotificationSettings::getSettings() because comment_notify_form_user_form_alter() uses that method directly and the user profile form would otherwise receive a missing value instead of the configured default.

gnuget’s picture

StatusFileSize
new1.65 KB

Patch attached.

  • gnuget committed cd98ee31 on 8.x-1.x authored by jwilson3
    Issue #3612852 by jwilson3: Fix Undefined array key comment_notify
    
gnuget’s picture

Status: Needs review » 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.