cancel
Showing results for 
Search instead for 
Did you mean: 

Sort order for di.xml arrays can't be influenced

Sort order for di.xml arrays can't be influenced

Feature request from Vinai, posted on GitHub Jun 17, 2015

When constructing arrays using DI (or also in layout XML) with the array type, it seems to be impossible to influence the order or the elements except via module load sequence. However, it currently is not possible to have one module to add itself to the sequence of another module, which limits this option severely. As I'm sure you are aware, the sort order of the array elements can be quite important.

In some instances, for example the RouterList, this problem is overcome by making each router in the list a sub-array with the possibility to add a sortOrder. This works fine, but if the injected array is one dimensional it isn't an option. It forces the structure onto the client receiving the array.

In other cases the injected list is simply an array of objects or strings, without the chance to influence the sort order. One example is the composite phrase renderer. It isn't possible to add a renderer before the placeholder renderer.

It would be quite useful to be able to add an optional sortOrder attribute to the item node, which - if present - is used to sort the resulting array argument. Maybe a before/after attribute (like in layout XML) would be more appropriate, but I'm sure opinions on this will vary. Personally I don't care, I would simply like to have an option to influence the sort order of dependency injected arrays built with in di.xml, without resorting to changing the module load sequence.

2 Comments
apiuser
New Member

Comment from antonkril, posted on GitHub Jun 18, 2015

Sorting wasn't implemented because it'll have impact on performance of ObjectManager. But I agree that we should support it. Thanks for the suggestion. I'll let you know when we have news about this.

clsmjohnson
M2 Certified

The concept of being able to sort the array in di.xml is something I also ran into when diagnosing issues with and extending some of the logging capabilities built into the core.
https://github.com/magento/magento2/issues/2529

In the case of the Monolog Logger arguments, the order of the items in the handlers array is important in how the logger behaves as it iterates over the array, and not having the abilitity to insert an item before or after some other item would make it much more difficult to extend or even possible to utilize.