Annotated Plugin - How to write a Plugin for - how to set guest to view title only and not content?
1. Create the shell and assign the basics of the plugin.
- all of this goes into default.php
- the plugin name is GuestRestriction.
create a mixed case folder with the name of you plugin under plugins directory
/vanilla/plugins/GuestRestriction.create a default.php
- fill in the plugin array with appropriate descriptive info
default.php
<?php if (!defined('APPLICATION')) exit(); // Define the plugin: // the 'GuestRestriction' is the name of the Plugin folder $PluginInfo['GuestRestriction'] = array( // just the name to display of the plugin below 'Name' => 'Guest Restriction', // a brief description 'Description' => 'This plugin prevents guests from viewing discussion, onlty title is viewable.', // each time you update plugin update version so you can upload it. 'Version' => '1.0', 'Author' => "Peregrine" // the author );
2. add the basic setup
// extend the garden Plugin class and create GuestRestrictionPlugin
// class name is created by adding name of plugin folder "GuestRestriction" + "Plugin"
class GuestRestrictionPlugin extends Gdn_Plugin { // where you put your code logic // this allows you to enable and disable plugin public function Setup() { } }
3. Add the logic
- we want to do things on the individual discussion page for each comment. - How did I find where to put this - I searched for FireEvent in helper_ functions.php in - the /vanilla/applications/vanilla/views/discussion folder. // this function executes on the discussion page public function DiscussionController_BeforeCommentMeta_Handler($Sender) { }
4. Add more logic. get the session info and test if user is logged in
public function DiscussionController_BeforeCommentMeta_Handler($Sender) { // get current session info $Session = Gdn::Session(); // this tests to see is user is logged in if (!($Session->IsValid())) { } } }
5. Add more logic. now we add the .css file that will hide the comments.
public function DiscussionController_BeforeCommentMeta_Handler($Sender) { // get current session info $Session = Gdn::Session(); // this tests to see is user is logged in if (!($Session->IsValid())) { // if the user is NOT logged in - add this css file which resides // in the /plugins/GuestRestriction design folder $Sender->AddCssFile('guestrestriction.css', 'plugins/GuestRestriction'); } }
6. create the css file. it should reside in
/vanilla/plugins/GuestRestriction/design we name it 'guestrestriction.css,
/vanilla/plugins/GuestRestriction/design/guestrestriction.css contains
.Comment .Message { display:none; }
this isolates any classes of Message that are descendants of class Comment and makes them invisible by setting display to none.
I found this by looking in firebug or chrome developer tools.
7. now we ensure this works when a user is viewing individual categories.
// this function essentially produces the same results
// of Discussion View when viewing Category
public function CategoriesController_BeforeDiscussionMeta_Handler(&$Sender) { // the call to discussioncontroller at the BeforeDiscussionMeta event // How did I find where to put this - I searched for FireEvent //in helper functions in // the /vanilla/applications/vanilla/views/discussion folder. $this->DiscussionController_BeforeCommentMeta_Handler($Sender); }
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
Best Answer
-
aery ✭✭✭
Thanks @peregrine
Now I will try to make some plugins based on your skelton
There was an error rendering this rich post.
1
Answers
If you "like" this, download the complete example plugin here.
http://vanillaforums.org/addon/guestrestriction-plugin
and now one of "You" can add it to the wiki, if you found it helpful.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
Thanks @peregrine
Now I will try to make some plugins based on your skelton
There was an error rendering this rich post.
looking forward to your plugins!
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
i think i am starting to understand how it works. oh it will be awesome as soon as i totally get it!
thanks so much @peregrine
edit: i just wish i had more time on my hands, damn
fh111 - Once you get the time looking forward to some new plugins!. I'm happy if this added any more insight in to writing some basic plugins. Once I learned a bit about the naming and syntax of the plugin, it really did become a lot clearer and grepping for Fire to find events gave me a clue as to what events are possible.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
Part 2: Modifying an Existing Plugin.
Lets say instead of blanking out the message for guests (users who are not logged in), you want to replace the message in the body with a link that directs the user to log-in.
Steps required to modify the plugin.
1) In this case i know that the message is added in helper_ functions.php in
Go back into default.php
remove or comment out the two functions
2) create the new function
3) add the logic to replace the message.
4) resynch with the category controller
5) Now users who are not logged in will see the link "This message can be viewed if you log in" that points to the signin process.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
Nice tutorial @peregrine! I was going to say... hiding restricted content with CSS is not really hiding it. But looks like you changed this function to server-side.
I just made my first plugin and would love some eyes on it: http://vanillaforums.org/discussion/20373/tag-cloud-plugin
Next step is to delve into event hooks and whatnot...
@leafmonster
It was more of an exploratory to try to explain the ins and outs of a simple plugin rather than the virtues of the plugin itself.
One more plugin developer added to the bunch!
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
whenever i click the redirection link, the page cannot be displayed.
do i have to change the link to my domain?
http://localhost/vanilla/entry/signin?Target=discussions
btw! thanks for making my request possible
Yes - unless you have everybody signin at your house or on a keyboard at your physical server location.
on line 6 in step 3 above.
// note: put whatever url you want below.
revision of comment, should say
// note: put whatever url you want below. and obviously you must point it to a valid url**
just for reference.....
to point to your own domain for signup. highlight the sigin link on your site and right click and copy the link into this position.
this annotation is for how-to write a plugin - not how to write php, or css, or how to make a link. It does demand some knowledge of php, css, and a bit of how to write some code logic.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
How to put a setting option in your plugin. (there are numerous ways to do this - this technique will add a settings option aside the enable button in the dashboard.
1 - add the SettingsUrl to the array in default.php
'SettingsUrl' => '/dashboard/plugin/guestrestiction',
like so:
2 - add a views directory to your plugin
vanilla/plugins/GuestRestriction/views
create a file gr-settings.php
vanilla/plugins/GuestRestriction/views/gr-settings.php
3 - modify the default.php to reflect and retrieve the config settings you just created in the view above
we modify this function so it now retrieves configurations settings form config.php
4. Add a function to default.php that directs you the view you created above in step 2. Once again make sure the two config option names match.
public function PluginController_GuestRestriction_Create(&$Sender, $Args = array()) {
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
Pt. 3 - add sidemenu to the dashboard for the plugin with a slight change to arguments.
change this
public function PluginController_GuestRestriction_Create(&$Sender, $Args = array()) {
to this (lose the ampersand and the arguments array).
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
It may not be perfect - but it might be a start.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.