Problem/Motivation

The `klaro_preprocess_field()` function causes an "Undefined array key" PHP warning by attempting to access the `#formatter` key without checking for its existence. This affects fields whose render arrays omit this key, such as those processed by the **Real name** module.

Warning: Undefined array key "#formatter" in klaro_preprocess_field()

Steps to reproduce

  1. Install and enable the **Klaro Cookie & Consent Management** module.
  2. Install and enable the **Real name** module (`drupal.org/project/realname`).
  3. Configure the **Real name** module to replace the display name for users.
  4. View a page where a user's realname field is displayed.
  5. The PHP warning will appear.

Proposed resolution

Update the `klaro_preprocess_field()` function to safely check for the existence of the `#formatter` key in the render array before attempting to access it..

Issue fork klaro-3551342

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

frouco created an issue. See original summary.

frouco’s picture

Assigned: frouco » Unassigned
Status: Active » Needs review

Ready for review

anybody’s picture

Status: Needs review » Needs work

Thanks @frouco - code-wise this totally makes sense.

Additionally I'd vote to convert all these if's to either a switch statement or at least else-if's, because they are all exclusive and simply doesn't make sense to evaluate them all once one matches.

My favourite, also as fix here, would be a switch statement, but @jankellermann should decide finally

marcelovani’s picture

Status: Needs work » Needs review

If we have a simple solution to fix the issue, why don't we just merge this and then @anybody can create a new ticket to refactor things

anybody’s picture

Thanks @marcelovani for your helpful comment. Please mind that I'm not blocking this, I just left my review and opinion and I think currently the maintainers here don't have much time to look into issues.
I just took the time and changed things as written.

Please review.

Anyway, no bad intentions from my side, we're one community! :)

jan kellermann made their first commit to this issue’s fork.

jan kellermann’s picture

Fixed some DCS. Maybe @frouco or @marcelovani could review? Thank you all for your work.

marcelovani’s picture

Looks good to me.

ps: Haven't manually tested, just reviewed the changes.

jan kellermann’s picture

Version: 3.x-dev » 3.1.x-dev
anybody’s picture

@jan kellermann what's your oppinion on #4? (to get a final statement here)

jan kellermann’s picture

Doing it in two steps would indeed have been clearer. But once it's finished and has an RTBC, it would be unnecessary work to split it up.

So please RTBC.

anybody’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, fine!

jan kellermann’s picture

Status: Reviewed & tested by the community » Fixed

Thank you for your work! The changes are merged.

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.