Plugin Lazy loading can cause usort warning

Created on 2 April 2016, over 8 years ago
Updated 26 January 2023, almost 2 years ago

Problem/Motivation

Sporadically we get the following warning:
Warning: uasort(): Array was modified by the user comparison function in Drupal\Core\Plugin\DefaultLazyPluginCollection->sort() (line 100 of core/lib/Drupal/Core/Plugin/DefaultLazyPluginCollection.php).

A 100% repo is to use the "Entity Browser Module" when using a file field (in our case inside a field collection) with the File Browser widget.

Proposed resolution

According to this link:
http://shout.setfive.com/2013/06/14/symfony2-usort-array-was-modified-by...

The problem lies in the lazy loading of the plugin collection. So the fix is simple. In the sort() function (DefaultLazyLoadingPluginCollection) iterate through the array like this:

  public function sort() {
    // iterate to populatae the array
    foreach($this->instanceIDs as $id){
      $pluginId = $this->get($id)->getPluginId();
    }
    // original code
    uasort($this->instanceIDs, array($this, 'sortHelper'));
    return $this;
  }

Remaining tasks

reviews needed, tests to be written and run

User interface changes

none

API changes

none

Data model changes

none

Original report by [username]

none

πŸ› Bug report
Status

Closed: outdated

Version

10.1 ✨

Component
PluginΒ  β†’

Last updated 3 days ago

Created by

πŸ‡©πŸ‡ͺGermany Denis Danielyan

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024