Problem/Motivation

This ticket is basically https://www.drupal.org/project/drupal/issues/3174022 but it's another place in the code making problems.

I just checked my Drupal instance's compatibility with PHP 8.1. it works quite well so far. Only one thing doesn't work, yet. I did some debugging and found the issue in the core:

web/core/lib/Drupal/Core/Extension/ModuleHandler.php

Here we have calls of call_user_func_array but the args are not processed with array_values:

 /**
   * {@inheritdoc}
   */
  public function invoke($module, $hook, array $args = []) {
    if (!$this->hasImplementations($hook, $module)) {
      return;
    }
    $hookInvoker = \Closure::fromCallable($module . '_' . $hook);
    return call_user_func_array($hookInvoker, $args);
  }

  /**
   * {@inheritdoc}
   */
  public function invokeAll($hook, array $args = []) {
    $return = [];
    $this->invokeAllWith($hook, function (callable $hook, string $module) use ($args, &$return) {
      $result = call_user_func_array($hook, $args);
      if (isset($result) && is_array($result)) {
        $return = NestedArray::mergeDeep($return, $result);
      }
      elseif (isset($result)) {
        $return[] = $result;
      }
    });
    return $return;
  }

Hence I get:

Error: Unknown named parameter $message in call_user_func_array() (line 427 of core/lib/Drupal/Core/Extension/ModuleHandler.php).

Proposed resolution

Use array_values so that in php 8.x the $args's keys aren't used for named properties:

 /**
   * {@inheritdoc}
   */
  public function invoke($module, $hook, array $args = []) {
    if (!$this->hasImplementations($hook, $module)) {
      return;
    }
    $hookInvoker = \Closure::fromCallable($module . '_' . $hook);
    return call_user_func_array($hookInvoker, array_values($args));
  }

  /**
   * {@inheritdoc}
   */
  public function invokeAll($hook, array $args = []) {
    $return = [];
    $this->invokeAllWith($hook, function (callable $hook, string $module) use ($args, &$return) {
      $result = call_user_func_array($hook, array_values($args));
      if (isset($result) && is_array($result)) {
        $return = NestedArray::mergeDeep($return, $result);
      }
      elseif (isset($result)) {
        $return[] = $result;
      }
    });
    return $return;
  }

Remaining tasks

Test case
Code review
Testing

CommentFileSizeAuthor
#9 3310431-9.patch1.8 KBprem suthar

Issue fork drupal-3310431

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

Defcon0 created an issue. See original summary.

wim leers’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +PHP 8.1

Thanks for trying this, and for the bug report!

Two questions:

  1. Which exact version of Drupal 9.4 are you using?
  2. Can you also reproduce this with PHP 8.0?

Thanks! 🤓😊

longwave’s picture

Which hook was being invoked, and was it core, contrib or custom code that invoked it? Can you post an extract of the caller?

defcon0’s picture

Sorry guys for the delay, I‘m on DrupalCon in Prague right now. Will answer on friday :-)

defcon0’s picture

OK, now the promised feedback ;-)

  • The exact version of drupal core is 9.4.6.
  • The issue is also reproducable with php 8.0 (composer dependencies updated with this version).
  • The hook being called is "swiftmailer_attach".
  • The caller of the hook is custom code as shown below:
/**
 * Implements hook_swiftmailer_attach().
 */
function acme_partner_swiftmailer_attach($key) {
  if ($key == 'register_pending_approval') {
    $file = UrlBuilderService::buildFileUrlFromConfig(
      'acme_partner.settings',
      'registration_attachment_',
      \Drupal::languageManager()->getCurrentLanguage()->getId()
    );
    if (isset($file)) {
      $attachment = new stdClass();
      $attachment->uri = $file->getFileUri();
      $attachment->filename = $file->getFilename();
      $attachment->filemime = $file->getMimeType();

      return $attachment;
    }
  }
}
defcon0’s picture

Since the ticket is now in state "Postponed (maintainer needs more info)": what extra info is needed exactly?

defcon0’s picture

Status: Postponed (maintainer needs more info) » Needs review

Changing the status because no answer which additional info is needed has been provided. In my opinion all necessary info is given and the issue is not minor for php8 systems.

longwave’s picture

Status: Needs review » Active

Given that over in #3174022: call_user_func_array() and named arguments in PHP 8 we decided that named arguments were not supported in hook invocations, I think that it is probably OK to make the same change in ModuleHandler.

Setting to "Active" as there is no patch to review. If you want to make the suggested change and upload it, please then set the status to "Needs review".

prem suthar’s picture

StatusFileSize
new1.8 KB

Upload the Patch.

prem suthar’s picture

Status: Active » Needs review

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Priority: Critical » Major
Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Lowering as critical seems to be everyones sites are broken.

As a bug this will need a test case to show the issue.

Updated IS.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sahana _n made their first commit to this issue’s fork.

sahana _n’s picture

Status: Needs work » Needs review

Hi,
I tested the MR patch in Drupal 10.4 Please review.
If I missed anything, please let me know. I would greatly appreciate any guidance.
Thank you!!

smustgrave’s picture

Status: Needs review » Needs work

MRs should be against 11.x

Also mentioned needing tests to show this is an issue.

nicxvan’s picture

Component: base system » extension system

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

nicxvan’s picture

Status: Needs work » Postponed (maintainer needs more info)

Do we still need to do this?