Legacy Event Examples - HL Vanilla Community
<main> <article class="userContent"> <h2 data-id="inject-the-the-current-users-roles-into-every-page">Inject the the current user’s roles into every page</h2><p>Sometimes you may want to adjust parts of the template based on the roles the current user. This will gather the roles of the current user and inject them into the smarty template.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">public function base_render_before($sender) { if (inSection('Dashboard')) { return; } if(!val('UserRoles', $sender->Data)) { $userRoles = val('UserRoles', $sender->Data); if (!$userRoles) { $user = val('User', Gdn::controller()); if (!$user && Gdn::session()->isValid()) { $user = Gdn::session()->User; } $userID = val('UserID', $user); $userRoles = Gdn::userModel()->getRoles($userID)->resultArray(); } $sender->setData('UserRoles', $userRoles); } } </pre><h2 data-id="inject-a-conditional-based-on-roles">Inject a conditional based on roles</h2><p>Sometimes you may not need all the roles in the page. Let’s say you wanted to make the page appear differently for a user with a certain role. Instead of injection all of the roles into the template and doing the conditional there, you can do it in the themehooks and inject just the boolean value you need.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">public function base_render_before($sender) { if (inSection('Dashboard')) { return; } $userRoles = val('UserRoles', $sender->Data)); if (!$userRoles) { $user = val('User', Gdn::controller()); if (!$user && Gdn::session()->isValid()) { $user = Gdn::session()->User; } $userID = val('UserID', $user); $userRoles = Gdn::userModel()->getRoles($userID)->resultArray(); } $roleNames = array_column($userRoles, 'Name'); $isSuperSpecialRole = in_array("SuperSpecialRole", $roleNames); $sender->setData("isSuperSpecialRole", $isSuperSpecialRole); } </pre><h2 data-id="create-an-additional-settings-page">Create an additional settings page</h2><p>This example creates a custom dashboard page that can set a few configuration options. You would then need to use these set configuration values in other hooks to customize your site. The configuration module uses Gdn_Form internally and renders an nice looking form for the dashboard. Its implementation can be found <a href="https://github.com/vanilla/vanilla/blob/master/applications/dashboard/views/modules/configuration.php" rel="nofollow noreferrer ugc">here</a>. Further details can be found by looking through <a href="https://github.com/vanilla/vanilla/blob/master/library/core/class.form.php" rel="nofollow noreferrer ugc">Gdn_Form</a>. Not all form values are supported. Currently supported form values are</p><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">categorydropdown</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">labelcheckbox</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">checkbox</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">toggle</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">dropdown</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">imageupload</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">color</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">radiolist</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">checkboxlist</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">textbox</code></li></ul><p>Additional and more complex examples of its use can be found in the <code class="code codeInline" spellcheck="false" tabindex="0">SettingsController</code> and Vanilla’s bundled plugins.</p><ul><li><a href="https://github.com/vanilla/vanilla/blob/master/applications/dashboard/controllers/class.settingscontroller.php#L472-L552" rel="nofollow noreferrer ugc">Branding Page</a></li><li><a href="https://github.com/vanilla/vanilla/blob/master/applications/dashboard/controllers/class.settingscontroller.php#L864-L893" rel="nofollow noreferrer ugc">Email Styles Page</a></li><li><a href="https://github.com/vanilla/vanilla/blob/master/plugins/GooglePlus/class.googleplus.plugin.php#L512-L520" rel="nofollow noreferrer ugc">Google Plus Page</a></li></ul><pre class="code codeBlock" spellcheck="false" tabindex="0">class MySitePlugin extends Gdn_Plugin { /** * Create the `/settings/example` page to host our custom settings. * * @param SettingsController $sender */ public function settingsController_example_create($sender) { $sender->permission('Garden.Settings.Manage'); $configurationModule = new ConfigurationModule($sender); $configurationModule->initialize([ 'ExmapleSite.BackgroundColour' => ['Control' => 'TextBox', 'Options' => ['class' => 'InputBox BigInput']], 'ExmapleSite.BackgroundImage' => ['Control' => 'ImageUpload'], 'ExmapleSite.BannerImage' => ['Control' => 'ImageUpload'], ]); $sender->addSideMenu(); $sender->setData('Title', "My Site Setttings"); $configurationModule->renderAll(); } /** * Add the "My Site" menu item. * * @param Gdn_Controller $sender */ public function base_getAppSettingsMenuItems_handler($sender) { /* @var SideMenuModule */ $menu = $sender->EventArguments['SideMenu']; $menu->addLink('Appearance', t('My Site'), '/settings/example', 'Garden.Settings.Manage'); } } </pre><h2 data-id="add-a-link-to-the-mebox">Add a link to the MeBox</h2><pre class="code codeBlock" spellcheck="false" tabindex="0"> /** * Add link to drafts page to me module flyout menu. * * @param MeModule $sender The MeModule * @param array $args Potential arguments * * @return void */ public function meModule_flyoutMenu_handler($sender, $args) { if (!val('Dropdown', $args, false)) { return; } /** @var DropdownModule $dropdown */ $dropdown = $args['Dropdown']; $dropdown->addLink(t('My Drafts'), '/drafts', 'profile.drafts', '', [], ['listItemCssClasses' => ['link-drafts']]); } </pre><h2 data-id="add-a-custom-location-for-a-moderation-message">Add a custom location for a moderation message</h2><pre class="code codeBlock" spellcheck="false" tabindex="0"> /** * Add custom asset location for messages * * @param MessageController $sender The message controller * @param array $args The event arguments passed * * @return void */ public function messageController_afterGetAssetData_handler($sender, $args) { $possibleAssetLocations = val('AssetData', $args); $possibleAssetLocations['AbovePromotedContent'] = 'Above the promoted content module'; setValue('AssetData', $args, $possibleAssetLocations); } </pre><p>You would then need to place this new asset somewhere in your template</p><pre class="code codeBlock" spellcheck="false" tabindex="0">{asset name="AbovePromotedContent"} </pre><h2 data-id="add-the-views-and-comments-counts-to-a-discussion-page">Add the Views and Comments counts to a Discussion Page</h2><pre class="code codeBlock" spellcheck="false" tabindex="0"> /** * Adds Views and Comments count to Discussion Page * * @param DiscussionController $sender * @param array $args */ public function DiscussionController_AfterDiscussionTitle_handler($sender, $args) { echo '<span class="Discussion-CountViews">'; echo t("Views").": "; echo $args['Discussion']->CountViews; echo '</span>'; echo '<span class="Discussion-CountComments">'; echo t("Reply").": "; echo $args['Discussion']->CountComments; echo '</span>'; } </pre><h2 data-id="add-leaderboards-to-the-panel-on-the-categories-page">Add Leaderboards to the panel on the categories page</h2><pre class="code codeBlock" spellcheck="false" tabindex="0"> /** * Adds leaderboards to Activity page * * @param ActivityController $sender * @param array $args */ public function categoriesController_render_before($sender, $args) { if ($sender->deliveryMethod() == DELIVERY_METHOD_XHTML) { $sender->addModule('LeaderBoardModule', 'Panel'); $module = new LeaderBoardModule(); $module->SlotType = 'a'; $sender->addModule($module); } } </pre><p><br></p> </article> </main>