Passing null to strpos() in message_subscribe_ui_form_flag_form_alter().

Created on 23 July 2024, 5 months ago
Updated 17 September 2024, 3 months ago

Problem/Motivation

On /admin/structure/flags/add/entity%3Anode I get this message:

Deprecated function: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in message_subscribe_ui_form_flag_form_alter() (line 19 of modules/contrib/message_subscribe/message_subscribe_ui/message_subscribe_ui.module).

Looking into the code:

function message_subscribe_ui_form_flag_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $prefix = \Drupal::config('message_subscribe.settings')->get('flag_prefix');
  /** @var \Drupal\flag\FlagInterface $flag */
  $flag = $form_state->getFormObject()->getEntity();
  if (strpos($flag->id(), $prefix) === 0) {

If this is a flag create form, the $flag->id() will be null, but strpos() expects a string.

Steps to reproduce

PHP 8.1+
message_subscribe 8.x-1.3
Errors displayed to the screen.

Visit /admin/structure/flags/add/entity%3Anode.
Or /admin/structure/flags/add/entity:node

Expected: No error.
Actual: Error as above.

Proposed resolution

We need a well-defined behavior in case the $flag->id() is null.
Currently this would be cast to empty string, so strpos('', $prefix) === 0 will fail, unless $prefix is also empty.

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Needs review

Version

1.3

Component

Code

Created by

🇩🇪Germany donquixote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024