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.

Additional Categories Layout Tutorial

CansiliCansili New
edited February 2016 in Vanilla 2.0 - 2.8

Is there some sort of a guide on how to add more categories layout?

I was able to alter the Homepage settings to add options for another layout (https://vanillaforums.org/discussion/31746/override-homepage-settings-views) but I'm stuck now on where to proceed to create the views for that option.

Comments

  • R_JR_J Ex-Fanboy Munich Admin

    The two existing options are "modern" and "table" and this setting is stored in the config setting Vanilla.Categories.Layout. Your custom setting should use that config setting for a custom value e.g. "groovy" :smiley:

    While there is no option at the beginning of the index method of the categories controller, there is an event "BeforeCategoriesRender" fired before the view is rendered. So basically it should be possible with a piece of code like that:

    public function categoriesController_beforeCategoriesRender_handler($sender, $args) {
        // Check for custom layout; return on standard layouts.
        if (c('Vanilla.Categories.Layout') != 'groovy') {
            return;
        }
    
        // You have to "trick" the render function to look for your own view. Look
        // at class.controller.php, method fetchView to get an idea on how to
        // achieve that.
        // Not sure if the following will work but it should be a start...
        // Try adding a file called "groovy.php" in "themes/YourThemeName/views/categories"
        $sender->View = 'groovy';
        $sender->ApplicationFolder = 'themes/YourThemeName';
    }
    
  • CansiliCansili New
    edited February 2016

    I'm sorry but I don't know much about PHP ( should have said this earlier :( ).

    I change the layout in config.php to "groovy", put the code above in class.themehooks.php and created a file groovy.php with simple "echo" codes.

    The result is that it reverted to the modern layout and got this error:

    Trying to get property of non-object in Vanilla\applications\vanilla\views\categories\all.php line 14

  • R_JR_J Ex-Fanboy Munich Admin

    Well it could have worked that way... :mrgreen: I had not taken a closer look at the source, so I have not seen that the categoriesController index method isn't used for displaying the different layouts. Here is the code that is responsible for the "routing":

    if ($CategoryIdentifier == '') {
        switch ($Layout) {
            case 'mixed':
                $this->View = 'discussions';
                $this->Discussions();
                break;
            case 'table':
                $this->table();
                break;
            default:
                $this->View = 'all';
                $this->All();
                break;
        }
    

    => If the layout is neither "mixed" nor "table" a method called "all" will be called. At the end of this method you'll find $this->render(); which normally calls a view with the same name as the method itself. So the methods name is "all" and that's why we have to override the "all.php" view.

    Create a testfile. While /themes/yourtheme/views/all.php is possible, I'd suggest /themes/yourtheme/views/categories/all.php. Just to be precise.

    If you've created that testfile and go to your forum, you should see the output of your testfile.

    Now change the content of your all.php to this:

    <?php
    if (c('Vanilla.Categories.Layout') === 'groovy') {
        include(__DIR__.DS.'groovy.php');
    } else {
        include(PATH_APPLICATIONS.DS.'vanilla'.DS.'views'.DS.'categories'.DS.'all.php');
    }
    

    That is somewhat ugly, but it will work. You need to create a groovy.php and that will be displayed when you open the categories page and the layout is set to "groovy".
    It is ugly because we put controller logic in the view, but I think it is okay in that case.

Sign In or Register to comment.