Creating a new view on an existing controller with a plugin.
Is it possible to create a new view for an existing controller from withing a plugin?
That is, say I want to create a new way of looking at categories. I could overwrite the default view with a custom one using a theme. But I want to give my users a choice of different views. How would I go about adding a custom view to the categories controller?
I have gotten as far as creating a plugin with the following function:public function CategoriesController_Render_Before($Sender) { //todo: implement view choice logic $Sender->View = "anewwaytolookatthings"; }
This just gives me a view not found error. Even if I put a file called "anewwaytolookatthings.php" in my plugins folder.
Thoughts?
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Best Answer
-
x00 MVP
Don't override. avoid it at all costs, usually I handle that with redirects, but only when strictly necessary. I take it you are manly concerned with a different display format.
you can use
CategoriesController_Render_Before
put your view in a folder called
views
inside your plugin folder reference it like so$Sender->View = $this->GetView('myalternativeview.php');
Note this only work in plugins. GetView is a method of Gdn_Plugin
grep is your friend.
5
Answers
I have made it a few steps farther. I can override a function on an existing controller like so:
public function CategoriesController_Index_Create($Sender) {
Now how can I access data thrown to the original function?
public function Index($CategoryIdentifier = '', $Page = '0') {
I figured out I can use:
public function CategoriesController_Index_Create($Sender, $CategoryIdentifier = '', $Page = '0') {
Everything seems to be working well, other than having to replace $this with $Sender if I want to access the overridden controller's members.
Is there anything else I am missing?
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Sorry, I got a bit confused, what is your current progress? have you managed to do what you were looking for, or do you still get errors?
My shop | About Me
I still haven't found an answer to my original question, but I figured out how to override the default Categories Controller function. Hopefully this will lead somewhere profitable.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Ok. You could try using
FetchViewLocation()
to get the view and give it to the Sender.$Sender->View = $Sender->FetchViewLocation('myalternativeview', '', 'plugins/MyPlugin');
Please note I haven't tested the above, this is just am 11:30 PM idea.
My shop | About Me
That gave me the error "View not found" which is the same issue I had initially.
Thanks for the help anyway @businessdad
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Don't override. avoid it at all costs, usually I handle that with redirects, but only when strictly necessary. I take it you are manly concerned with a different display format.
you can use
CategoriesController_Render_Before
put your view in a folder called
views
inside your plugin folder reference it like so$Sender->View = $this->GetView('myalternativeview.php');
Note this only work in plugins. GetView is a method of Gdn_Plugin
grep is your friend.
Sad that I was had the solution, but it didn't work because I didn't have the extension. Slapping myself for not thinking about it.
Thanks @x00 and @businessdad for your help!
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.