HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Module in /themes/MyCustomTheme folder

I'm just trying myself on writing a theme and I want to replace the DiscussionFilterModule with a custom one that I will call "AllDiscussionsModule". By now, that module looks like that:

<?php defined('APPLICATION') or die;
class AllDiscussionsModule extends Gdn_Module {
    public function AssetTarget() {
        return 'Panel';
    }

    public function ToString() {
        return ':-(';
    }
}

As you can see, by now it wouldn't do much more then printing a sad emoticon to the screen :(

When I put {module name="AllDiscussionsModule"} into my default master I find the following error comment in the html source: Error: AllDiscussionsModule doesn't exist, that comes from Gdn_Theme::Module when check for class_exists("AllDiscussionsModule") fails.

I've tried to save my module to
/themes/mytheme/class.alldiscussionsmodule.php
/themes/mytheme/class.alldiscussionsmodulemodule.php
/themes/mytheme/modules/class.alldiscussionsmodule.php
/themes/mytheme/modules/class.alldiscussionsmodulemodule.php
/themes/mytheme/vanilla/modules/class.alldiscussionsmodule.php
/themes/mytheme/vanilla/modules/class.alldiscussionsmodulemodule.php
... but none of that works.

Creating a plugin would be a way around that annoyance, but I would prefer to bundle it with the theme. Furthermore I'd prefer to add the module in Base_Render_Before with AddModule, but that fails when I try $AllDiscussionsModule = new AllDiscussionsModule($Sender); because "Class 'AllDiscussionsModule' not found"... :-/
Variations like $AllDiscussionsModule = new AllDiscussionsModule($Sender, '/themes/mytheme'); also fail

So what can I do to make my theme module known to the framework?

Comments

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    The way I added the customized memodule is by adding a modules folder in the views folder of the theme and add the customized me.php to that folder. Then just call the module from the theme <?php $this->AddModule('MeModule');?>

  • peregrineperegrine MVP
    edited December 2014

    you could add module to your themehooks

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

  • R_JR_J Ex-Fanboy Munich Admin

    @vrijvlinder said:
    The way I added the customized memodule is by adding a modules folder in the views folder of the theme and add the customized me.php to that folder. Then just call the module from the theme <?php $this->AddModule('MeModule');?>

    I think this works because you are overriding a view. The module itself is still the MeModule. I'm trying to create a new module.

    @peregrine said:
    you could add module to your themehooks

    AAAAAAAH! Since now I didn't even knew I could have two classes in one file! This is a great solution! Not my preferred but it only looks a little bit hackish and it works like a charm!

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    Right, but the method also works with modules you make. Just add the module php to the views and call it by adding
    the module part in the themehooks as peregrine suggests.

  • hgtonighthgtonight ∞ · New Moderator

    Just register a library map of theme context with the autoloader in your themehooks file. E.g.:

    Gdn_Autoloader::RegisterMap(Gdn_Autoloader::MAP_LIBRARY, Gdn_Autoloader::CONTEXT_THEME, PATH_THEMES . DS . 'bittermod' . DS . 'library');
    
    class BitterModThemeHooks implements Gdn_IPlugin {
    

    The map and context just determine where the map is saved in the cache folder. The path can be anything and is recursive by default.

    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.

  • R_JR_J Ex-Fanboy Munich Admin

    Not sure anymore we come from the same planet ;) but that code works like a charm!

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

  • peregrineperegrine MVP
    edited December 2014

    @R_J said:
    Not sure anymore we come from the same planet ;) but that code works like a charm!

    regarding hgtonight....
    I think it's the sub-zero temperatures, that slowed his metabolism down, so he can see things most of us can't see. Kind of looking at things in slow motions, allows him to take everything in.

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

  • R_JR_J Ex-Fanboy Munich Admin

    @hgtonight said:
    The map and context just determine where the map is saved in the cache folder. The path can be anything and is recursive by default.

    You've said recursive and I read relative, that's why I've used Gdn_Autoloader::RegisterMap(Gdn_Autoloader::MAP_LIBRARY, Gdn_Autoloader::CONTEXT_THEME, 'modules'); and it worked. Then I deactivated a (yet unfinished) plugin and my module couldn't be found again!
    It took me half an hour to see that I misread your code and tried the absolute path which worked again.

    Very strange and mysterious...

    Just wanted to report back, the theme path is needed and my short line of code only worked by luck :s

  • hgtonighthgtonight ∞ · New Moderator

    I wonder what the interplay was.

    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.

  • R_JR_J Ex-Fanboy Munich Admin

    /cache was the clue! I first tried the full path, then changed it to the shorter path, not knowing that the first info was already cached. Disabling my plugin has obviously deleted that mapping and trying to renew it with the (too) short path failed.

    Things like that happens when you do not understand what you are doing :(

  • hgtonighthgtonight ∞ · New Moderator

    @R_J said:

    Things like that happens when you do not understand what you are doing :(

    This is just another opportunity to learn something. :)

    Most of the stuff I have learned about PHP and Vanilla started as someone else asking me how to do something.

    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.

  • @hgtonight said:
    Most of the stuff I have learned about PHP and Vanilla started as someone else asking me how to do something.

    +1

    Sometimes, if someone asks an interesting question, I try to solve it as well, even if it isn't something I don't want on my local host forum. The challenge to try to implement things is a great way to learn.

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

  • R_JR_J Ex-Fanboy Munich Admin

    Most questions have been asked before and that's why I learn a lot simply by looking at similar questions that have already been answered by you ;)

    Afterwards I pretend to be clever when I simply repeat what has been said before B)

  • peregrineperegrine MVP
    edited December 2014

    @R_J said:
    Most questions have been asked before and that's why I learn a lot simply by looking at similar questions that have already been answered by you ;)

    Afterwards I pretend to be clever when I simply repeat what has been said before B)

    that's exactly what I did albeit a small mod to code to make it work in 2.1

    pretend to be clever that is :)

    Todd: http://vanillaforums.org/discussion/comment/146418/#Comment_146418

    vs.

    peregrine : http://vanillaforums.org/discussion/comment/221094/#Comment_221094

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

  • R_JR_J Ex-Fanboy Munich Admin

    It's working all the time... :D

Sign In or Register to comment.