Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Adding a custom module to a theme.

edited August 2015 in Vanilla 2.0 - 2.8

I'm working on a theme and I'd like to add a custom module. I've found examples of how to do this with plugins but not in a theme.

Here's what I've got so far:
In my class.customthemehooks.php

<?php if (!defined('APPLICATION')) exit();

class CustomThemeHooks extends Gdn_Plugin {
    public function Base_Render_Before($Sender) {
        $CustomModule = new CustomModule($Sender);
        $Sender->AddModule($CustomModule);
    }
}

Then I've created a class.custommodule.php and I've tried placing it in /themes/custom, /themes/custom/modules, /themes/custom/views/modules.

<?php if (!defined('APPLICATION')) exit();

class CustomModule extends Gdn_Module {
public function ToString() {
echo 'This is a custom module';
}
}

Then in my default.master.tpl file for my theme I have this (along with other html of course)

{module name="CustomModule"}

However when I load the forum, its just blank. I'm assuming I've done one or many things wrong. Or perhaps you can't have custom modules in a theme and you have to do it through plugins. Any help is appreciated. Thanks!

Tagged:

Comments

  • peregrineperegrine MVP
    edited August 2015

    Gdn_Autoloader::RegisterMap(Gdn_Autoloader::MAP_LIBRARY, Gdn_Autoloader::CONTEXT_THEME, 'modules');

    add above class in theme hooks.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    You can use the plugin widgets to make any amount of modules you like.

    http://vanillaforums.org/addon/widgets-plugin

    peregrine
  • edited August 2015

    @peregrine I added that above "class CustomTheme..." and nothing changed. Still a blank screen.

  • Thanks @vrijvlinder does your plugin allow me to add modules outside of the Panel asset?

  • apparently you need the themepath as well.

    http://vanillaforums.org/discussion/comment/220751/#Comment_220751

    I didn't need the autoloader in this example

    http://vanillaforums.org/discussion/comment/233327/#Comment_233327

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited August 2015

    @yourfavorite said:
    Thanks vrijvlinder does your plugin allow me to add modules outside of the Panel asset?

    Yes you can pick where you want it. The options are Head, Content,Panel and Foot

    You can make unlimited modules just use the ones I added as a guide.

  • edited August 2015

    @peregrine I checked out those links.

    In the second one you were overwriting an exisiting module from vanilla. This has worked fine for me however in this instance I'm wanting to add a new module not just overwrite.

    For the first link though, I tried changing the line to
    Gdn_Autoloader::RegisterMap(Gdn_Autoloader::MAP_LIBRARY, Gdn_Autoloader::CONTEXT_THEME, 'themes/custom/modules');

    This resulted in the "Something went wrong" Vanilla error page.

    Then I tried removing the above mentioned line and just pasting my CustomModule class into the theme hook file. This also resulted in the Vanilla error page.

    I should add that in both cases mentioned above, it would seem that the following line is causing the error, because without it, everything runs fine. Though of course the module doesn't show up.

    $Sender->AddModule($CustomModule);
  • This resulted in the "Something went wrong"

    debug it. and find out the error.

    add debugging through config.php

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • peregrineperegrine MVP
    edited August 2015

    you said: $Sender->AddModule($CustomModule);

    I suspect you got a target asset error.

    try

    $Sender->AddModule($CustomModule,"Panel");

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Debugging didn't spit out any errors. :|

    However I figured it out! Apparently every module class requires an AssetTarget() function to be declared. So I added that and its all working. Thanks for the help guys! Worth noting that I don't actually want this module to be added to any of the existing assets so My AssetTarget function has return '';

  • peregrineperegrine MVP
    edited August 2015

    Apparently every module class requires an AssetTarget

    but not necessarily in the module itself!

    in vanilla 2.1 (and I suspect Vanilla 2.2)

    if you are talking theme with autoloader in themehooks..

    Gdn_Autoloader::RegisterMap(Gdn_Autoloader::MAP_LIBRARY, Gdn_Autoloader::CONTEXT_THEME, 'themes/bittersweet/modules');

    you don't need to use the addmodule function, or put asset target in the module.

    it works perfectly fine adding to the .tpl below the asset.

     {asset name="Panel"}
     {module name="MyModule"}
    

    /mytheme/modules/class.mymodule.php

    <?php defined('APPLICATION') or die;
    
    class MyModule extends Gdn_Module {
    public function ToString() {
    return 'This is a custom module';
      }
    }
    

    in a plugin

    you don't need asset target in the module either.

    if you use call like this with asset as 2nd parameter the target is not needed in module.

    $Sender->AddModule($CustomModule,"Panel");

    you need asset target in module if you call function without an asset target as a parameter.

    $Sender->AddModule($CustomModule);
    

    then in module

       public function AssetTarget() {
       return "Panel";  // or content or whatever.
       }
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    vrijvlinderhgtonight
  • That certainly seems simpler. Thanks @peregrine. I'll give it a shot.

Sign In or Register to comment.