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

Conditional Module view

Is there a way to use conditional views in a module, I have multiple module views (moduleview1.php, moduleview2.php, moduleview3.php etc..)


class MyPluginModule extends Gdn_Module  {
   public function __construct($Sender = '', $ApplicationFolder = '') {
      $ApplicationFolder = 'plugins/MyPlugin';
      parent::__construct($Sender, $ApplicationFolder);
   }

   public function AssetTarget() {
      return 'Panel';
   }

    public function ToString() {
       
       if(somecondition1){
          $moduleview = 'moduleview1';
       }
       else if(somecondition2){
          $moduleview = 'moduleview2';
       }
      else (someconditionetc){
          $moduleview = 'etc';
      }

        return $this->FetchView($moduleview); // is this possible or it always have no parameter?

   }
}

I always get this error Could not find a `[viewname]` view for the `MyPluginModule` module ....

Thanks

Comments

  • Modules - those mighty beasts...

    Many things can happen magically inside modules, that's what makes them hard to understand.

    1. The application folder

    You are using the fetchView method which only accepts a view name as a parameter. That method calls the fetchViewLocation which would also require to get an $applicationFolder parameter in order to look in which folder to look at.

    Since the $applicationFolder cannot be passed to the fetchView method, you have to set it before you call that method. If you look at the __construct method, you will find it as a parameter there.

    Thus, you have to do the following when instantiating your module class inside of your plugin:

    $myPluginModule = new MyPluginModule($sender, '/plugins/my-plugin');
    

    Now the fetchView method knows the "root" of your views


    2. The folder structure

    Modules views are always saved in the folder "./views/modules" and that's what the modules fetchView expects. Therefore you need to save your module in the folder "/plugins/my-plugin/views/modules"


    3. Setting a view

    The toString method is called automatically and if it is not defined in your plugin, the Gdn_Modules toString method simply renders the protected property Gdn_Module->view. Therefore I'd say it is quite elegant if you simply set the view to render with the modules setView method: $myPluginModule->setView('anything');


    4. The conditional view

    I wouldn't put that in the toString method. To my understanding that method is the view part where such a decision shouldn't happen. I would add some other method to the module like e.g. getConditionalView and you can use it like that (again from inside of the plugin):

    $myPluginModule = new MyPluginModule($sender, '/plugins/my-plugin');
    $viewName = $myPluginModule->getConditionalView($dataRequiredForMakingDecision);
    $myPluginModule->setView($viewName);
    $sender->addModule($myPluginModule);
    


    And that's it. Your module will not need a toString method then. You have set the view and the rest happens automatically.

  • Thank you

Sign In or Register to comment.