Value passed as the first argument to the method_exists function is null

Created on 20 August 2024, 4 months ago
Updated 27 August 2024, 4 months ago

Problem/Motivation

In some circumstances within the ui_patterns_field_group_field_group_field_overview_submit function in the ui_patterns_field_group.module file, an error occurs indicating that the value passed as the first argument to the method_exists function is null. The method_exists function requires an object or a string (the class name), but in your code, it seems that $plugin_definition['class'] may not be correctly defined or could be null.

Proposed resolution

To resolve this issue, I added a check to ensure that $plugin_definition['class'] is not null before calling method_exists. Here is how the updated code might look:

function ui_patterns_field_group_field_group_field_overview_submit(array $form, FormStateInterface $form_state) {
  $field_group_form_state = $form_state->get('field_group');

  if (!empty($field_group_form_state)) {
    foreach ($form['#fieldgroups'] as $group_name) {
      // Only save updated groups.
      if (!isset($field_group_form_state[$group_name])) {
        continue;
      }

      if (isset($field_group_form_state[$group_name]->format_settings)) {
        // Retrieve the plugin definition.
        $plugin_definition = \Drupal::service('plugin.manager.field_group.formatters')->getDefinition($field_group_form_state[$group_name]->format_type, FALSE);

        // Ensure that $plugin_definition is not null and that the class exists.
        if ($plugin_definition && isset($plugin_definition['class']) && method_exists($plugin_definition['class'], 'processFormStateValues')) {
          call_user_func_array([
            $plugin_definition['class'],
            'processFormStateValues',
          ], [&$field_group_form_state[$group_name]->format_settings]);
        }
      }
    }

    // Set the form_state so that the submit hook of field_groups can work.
    $form_state->set('field_group', $field_group_form_state);
  }
}
🐛 Bug report
Status

Active

Version

1.8

Component

UI Patterns Field Group [1.x only]

Created by

🇮🇹Italy braintec Perugia, Umbria

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

Comments & Activities

Production build 0.71.5 2024