Adding Modules To Discussions
Thank you all for the help in the past few days. Thanks to your help I've been able to figure out how to edit some of the modules and plugins to do what I wanted. I've also learned to make a lot of changes using custom.css.
The one thing I am struggling with and need your help with is how to edit some of these plugins to display in my discussions rather than my profile.
I've tried changing the code for the Karma bank and Friendship add-on where it said ProfileController to make it say DiscussionsController, but that brought me some weird results.
So now I am asking here: how can I add the Karma Bank amount that's displayed in Profile under Discussions, or even add it to the menu header? How can I add the Friendship box that's displays in Profile to display on Discussions panel?
The above is my current Discussions page.
I want to display the Karma Bank amount in menu header, and in the panel to the left where it says My Drafts.
How can I display the friendships box under the All Time Leaders table?
The red rectangle is what I am trying to move from Profile page to Discussions.
Comments
First of all: editing files is nearly never the right way! Looking at the source code is always helpful but you should never edit anything.
So let's start with the Friendship plugin.
When speaking about a three year old plugin, things might be a little different. In fact it would be the cleanest way to create a copy. The copy uses a few files and in order to use a customized copy, you would have to change several things: file names, class names, plugin description, references to those classes...
Maybe in this case it would be okay to simply change that plugin sigh
There is another way, though! Your suggestion could be considered as an enhancement and I could imagine that a pull request will be acccepted. So let's talk about how the code could be changed and then make a pull request!
A module is attached to the panel with the
addModule()
call. Friendship attaches the module viabase_render_before()
so it is accessible on every page. No need to do anything here.Modules print out html via their
toString()
method. When looking at the code, we see that the toString method starts with checking if the module is attached to a profile page. If not, nothing happens. Well, that's bad. So let's change it! This is the code which we have to change:So when we are looking at a profile, we need to get the profile user we are looking at. If we are at a discussion page, we want to see the Friendship module as iff we are looking at our own profile.
We do not need that controller check. But we need a ProfileOwnerID and if we are not looking at a profile, we would need the session users id.
Basically that's all that's needed. You have to take care for one of the last brackets, through. The first if condition is now closed at the top of the file so the former closing bracket in line 137 (of the original file) must be deleted.
But there is another problem: what should guests see when they look at the page? That requires some more changes. I've mixed up that function in order change it's behavior and created a pull request. If you like to, you can take a look at what I've changed exactly: https://github.com/R-J/Friendships
@R_J Thank you so much for explaining that so well. The way you taught me here will certainly help me modify modules and understand the Garden framework. I could not even imagine a week ago that I'd be able to edit plugins/modules, but with the help of the community it's so much less frustrating than going solo. Thanks a ton as always! Would you suggest I try this same approach on the Karma bank plugin to get the Karma amount to show up in all the pages and not just profile?
I'd like to try the Karma bank plugin edit myself, but a general direction would be nice.
@R_J Another question: How do I make the friendship box panel go above another plugin's panel? Simply want to change the order in which the modules appear in that panel.
Since the Box is a Module, you would want to Sort the modules. You can try the plugin ModuleSort or you can sort it manually via the config.php .
Simply add your module in the order you want it to appear on the list of modules.
❌ ✊ ♥. ¸. ••. ¸♥¸. ••. ¸♥ ✊ ❌
This is the code where the line is created:
The first two lines check if the plugin is enabled. If you want to check if another plugin is enabled, you can write
If your plugin requires another plugin, you could (and should) specify this already in the plugin info array like that
'RequiredPlugins' => ['KarmaBank' => '>= 0.9.7.3b']
. If your plugin contains this in the info array, you wouldn't be able to enable it if not KarmaBank has been enabled beforehand!The next line creates an instant of the KarmaBank model and initiates it with the profile user. You do not have a profile user, but you are interested in the session used. Si it would read like that:
$karmaBank = new KarmaBankModel(Gdn::session()->UserID);
The next line stays as it is, but let's split the last line.
$Sender->AddProfileTab()
is a profileController specific action. We will not use that. Let's focus on building a nice link. Let me reformat what we have above, taking into account what the parameters of the AddProfileTab method arepublic function addProfileTab($TabName, $TabUrl = '', $CssClass = '', $TabHtml = '') {
Now it should be easy. But some things have changed what the html markup is about and we would use some handy functions. Browse through the file
functions.render.php
to find some. We will usefunction anchor($Text, $Destination = '', $CssClass = '', $Attributes = array(), $ForceAnchor = false)
andfunction userUrl($User, $Px = '', $Method = '', $Get = null)
The code we have right now will add a link to the karmabank of the currently logged in user and you would be able to use that link anywhere!
Try this:
I have not checked the code, but I hope it works. If not, I'm sure you can find the errors I made by yourself until now!
Now you want to add it somewhere else. You should inspect the target where you like to see the link to appear: this is a div.BoxDiscussionFilter. Do a fulltext search for "BoxDiscussionFilter". You will find a file
discussionfilter.php
which you should take a look at. I've told you that you can add content when you find "fireEvent" anywhere. discussionfilter.php has "AfterDiscussionFilters". So you can createpublic function base_afterDiscussionFilters_handler($sender) {...}
and add the code there.Now your link will be shown anywhere, where this menu is shown! WOOHOOO!
But the html markup is wrong... Look at the html source or the discussions.php file:
We echo our link after a li element and before the closing ul element. So change your code that it echos '
'.$karmaLink.'' and the markup should be fine.Just want to add that when you friend someone and then that user gets deleted, the deleted friend remains in your list and gives a page not found when you click on it.
@vrijvlinder your solution about the config was the best.....I don't like that Module Sorter plugin....it somehow reset a lot of my forum's theme settings and disabled some plugins all on its own and it didn't even display all the modules...Thanks for the solution.
There is nothing in the Module Sort plugin that would reset any theme settings in the downloads area, nor would it disable other plugins. It is best not to blame other plugins for your coding problems that you insert.
Suggest you restart with a clean vanilla installation without any of your code changes, that are causing your own problems. Until you remove your broken code from plugins, core and themes and start with fresh new copies, you will continue to have problems and may place undue blame on other plugins for your coding mistakes.
Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.