Custom index / homepage - overwriting views

I am writing a custom index / homepage plugin based on the tutorial here. I'm running into two problems that I do not understand.

1) When the homepage plugin is not the last plugin to run, the homepage is overwritten by the other plugin that is the theme for the site. When loading the homepage, you'll see the content of the homepage plugin for a few seconds and then it is overwritten by the theme plugin. The theme plugin is the last plugin to load.

My homepage plugin registers for the index, system plugin hook, so I don't understand why the view gets overwritten by the theme plugin when the theme plugin does *not* register for the index, system plugin hook.

2) When the homepage plugin is the last plugin to load, the homepage runs as expected and it is not overwritten. However, if I go to any other page (i.e. site/login) I get a white screen. I would think the theme plugin (which now loads second to last) would display its view for any page other than the homepage.

I would appreciate any clarification you could offer.

Here is my start.php code for the homepage plugin:

elgg_register_event_handler('init', 'system', 'homepage_custom_init');


function homepage_custom_init(){        
        elgg_register_plugin_hook_handler('index', 'system', 'homepage_custom');
 }
    
 function homepage_custom(){
        if (!include_once(dirname(dirname(__FILE__)) . "/HOMEPAGE-PLUGIN/pages/index.php")){
            return true;
        }else{
            return false;
        }
    }

  • I'm only a beginner, too, but there are some things missing in your start.php.

    Of course, your plugin should be the last plugin to load. All other plugins after your plugin could overwrite/change your settings. To be sure that the index-page is "yours", you should unregister the page-handler for your index-page and set it up again with your custom one.

    In addition, you don't have any page-handler in your start.php. So elgg don't know where your pages are and how to call the content. When you call a core-site (like site/login) I would guess that your plugin is called "again" and your function homepage_custom() is false (because your index.php is called/included the second time).

    I can recommend the official elgg-book which is advertised on elgg.org. There you can find a chapter "how to build your first plugin" with some example-code. It helped me to understand the structure of elgg and what is needed to call your own pages etc.

  • @Anthony Bartoli Someone wrote about How to create a landing page

    I think that it can help to you with your issue

  • Thank you @Viktor and @RvR. I do have the Elgg book so I'll take a closer look at it and review the landing page tutorial as well. Will report back later.

  • A pagehandler is in this case not necessary because you already define which page to load in the plugin hook handler callback function with the include_once statement.

    You might want to try it with this start.php:

    <?php

    elgg_register_event_handler('init', 'system', 'homepage_custom_init');


    function homepage_custom_init(){        
        elgg_register_plugin_hook_handler('index', 'system', 'homepage_custom', 1);
    }
            
    function homepage_custom($hook, $type, $returnvalue, $params){
        if ($returnvalue == true) {
            // another hook has already replaced the front page
            return $returnvalue;
        }

        if (!include_once(dirname(__FILE__) . "/pages/index.php")) {
            return false;
        }

        // return true to signify that we have handled the front page
        return true;
    }

  • Maybe some explanation:

    • elgg_register_plugin_hook_handler has another parameter (the 1) which gives the homepage_custom callback function the highest priority (i.e. it will be executed first). A good written callback function checks the returnvalue. If this is already true it might be better to not do anything. As your callback function gets executed first due to the priority and should return true any callbacks to index, system afterwards should be ignored if written correctly.
    • It seems to me you mixed up the return values true and false in your function. The function should return true when the include_once was successful and false if not. In your case it is just the other way round. This might explain the behaviour you had depending on the position of the plugin. If it wasn't in last position the theme overrides your index page. If your theme is in last position there's no other plugin overriding the index page, so it gets displayed. But nonetheless the callback function reports "false" and therefore Elgg gets confused (white screen).
  • Thanks iionly. Fixing the obvious return value mistake and listing the theme plugin last fixed both of my issues.

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking