Overwrite index methods for Core Modules
-
Hi,
for one of my projects, I need to overwrite some of the index methods of the Core Modules.
I don't want to show all existing entries, but e.g. only the ones of the current user.
Is there any "planned" way to do this? Can I add some kind of a filter to the DataGrid and if yes, how?
If there is no "planned" way, would there be an idea to make something like the Models Repositories be overwritten in any way?
I could try to do a PR if this would be a way to do this.Thanks
kay899
-
Hey @kay899,
Could you explain a bit more with an example?
Currently, Core does not enforce anything on your EloquentModels. You can override anything in your module repositories.Thanks.
-
Hey @armababy,
I know that I can make in my Module whatever I want.
In this case I want to use the Page Module of the Core system. Actually the index page (DataGrid) shows all entries in the database.
For my App I need the same, but only the entries for the current user.
So my question is, what way we could find that my Module can overwrite the way this index page selects they entries?
I was thinking of overwriting the Repository somehow.Regards
kay899
-
Currently, you can not overwrite Controller methods of Core modules.
What you can do is fork your own copy of Page module, then use that in composer as a Page module. This way you can do anything you like to that module and won't get overridden by updates.
Only thing is that you have to manually keep your fork up to date with AsgardPage.Thanks for the suggestion, will look into it.
-
I don't want to use forks, as in my opinion it is too much work, when you make changes in the Core.
What I would like to do is to contribute the changes I need for this feature.
Question is, what you suggest to do?
Change the location of the Repositories?
-
You can extend page-repository to your module, so whenever the application calls PageRepository it will get instances of your module where you can freely edit all BaseReposittory methods.
Basically, you need to$this->app->extend( 'Modules\Page\Repositories\PageRepository', function () { $repository = new \Modules\PageExtender\Repositories\Eloquent\EloquentPageRepository(new \Modules\Page\Entities\Page()); if (! config('app.cache')) { return $repository; } return new \Modules\PageExtender\Repositories\Cache\CachePageDecorator($repository); } );
And possibly you need to implement PageRepository interface in your PageRepository.
So that's that, hope you can make it work.
-
Are the Core Modules loaded first and then the Custom Modules?
Or how does the system know, that my Model will overwrite that stuff?
If that would work, I can only make the customization in my "Base" Model and don't need PR or change in the Core Modules, right?
That would be perfect
-
Modules load in this order:
- Core module always first
- Then rest of the modules based on rules:
2.1) All modules by alphabet sorted byorder
attribute insidemodule.json
2.2) If order is equal you can expect ModuleA
to come before ModuleB
As a general rule, i suggest you start your module order with index
2
and work up from there. Since default ordering isASC
meaning0
/A
is always first.
-
Instead of using a fork you could also just remove the module you want to customise out of composer, so that you can change it without them being overwritten later.
This is usually how go about things like this.
-
@armababy it's working like a charm.
Thanks!