TL;DR
How can I make a Module’s classes (e.g. Blocks, Helpers) reference different CollectionFactories based on which child Module calls it.
Without hardcoding these into the parent. (parent module doesn’t know about children) The information has to be injected to the parent from outside.
For more context
This is the situation:
We have Module_A
and Module_B
which has Module_A
as an dependency.
Module A
contains options for the customer to configure a vehicle (brand, model, etc.)
Module B
contains Model, ResourceModel and Collection for a product_relation
Table which maps different combinations of options from Module_A
to a sku that matches the selection.
For now
Module_A
also contains a Router, Block, Template, Helper etc.
Module_B
contains route, Controller and layout
Here’s how a request looks like for now:
- Customer calls a URL which looks like this: https://page.url/keyword/brand/model/year
(This URL can be constructed by a configurator or just come from a sitemap and be listed by google) Router
forwards the request toController
ofModule_B
based on the keyword with parameters brand, model and yearModule_B
layout calls template fromModule_A
which displays the products based on the match that theproduct_relation
table provides for that combination of brand + model + year
Problem here is now:
To deliver a match, Module_A
has to use the correct product_relation
Table.
For now I have the ProductRelationCollectionFactory
of Module_B
added to the Constructor in the Helper of Module_A
and the Helper offers methods that the Block uses to get the matching products and other stuff.
I don’t know if that’s the correct place for this from a MVC point of view, but I did it like that for now to have everything in one place while It is still work in progress. I guess the problem would be the same if I used the CollectionFactory in the Block instead of the Helper though (or wherever).
The whole point here is that there might be more Child Modules (Module_C, Module_D etc.) that provide different kinds of matches for different kinds of selections from Module_A
Module_A
doesn’t know about these children.
So while for now it’s working to reference Module_B
‘s CollectionFactory in Module_A
this is not the intended solution.
Module_A
has to use a different CollectionFactory
based on which Child Module calls it.
I guess I could just create separate Blocks, Helpers or whatever in every Child Module, but since the code would be like 99% redundant, the goal would be to keep as much of it as possible in the parent.
What would be the correct way to do this?