Roundcube Community Forum

 

I don't understand the template_container plugin hook, can someone please help

Started by mabj, February 14, 2019, 07:34:27 AM

Previous topic - Next topic

mabj

I'm trying to create a plugin and use the `template_container` hook to fill a container like this:

skins/larry/templates/login.html:

    <roundcube:container name="testcontainer" id="testcontainer" />


plugins/myplugin/myplugin.php

    class myplugin extends rcube_plugin
    {
        function init()
        {
            $this->rcmail = rcmail::get_instance();
            $this->add_hook('template_container', array($this, 'test_hook'));
        }

        function test_hook($attr) {
            if ($attr["name"] === "testcontainer") {
                $content = $attr["content"];
                $content .= html::tag('div', null, "I am testing this");
            }

            return array("content" => $attr["content"]);
        }
    }


I have added the plugin to config.inc.php like

    $config['plugins'] = array('xskin', 'managesieve', 'password', 'myplugin');` so that is not the problem.


But when I load the login screen, the div does not show up. Does anyone know what I am doing wrong?

I can also say that it works when I use the "startup" hook and $this->api->add_content(html::tag(...), "testcontainer"); so I know that my plugin works in general.

SKaero

Your adding a hook "add_hook" while trying to fill a container which is incorrect you should be using "register_handler"

mabj

Quote from: SKaero on February 14, 2019, 11:34:30 AM
Your adding a hook "add_hook" while trying to fill a container which is incorrect you should be using "register_handler"

So what is stated on https://github.com/roundcube/roundcubemail/wiki/Plugin-Hooks#template-hooks is incorrect?

Also, I don't understand this part from https://github.com/roundcube/roundcubemail/wiki/Plugin-API:

Finally, to instruct roundcube to show your template:

        $rcmail = rcmail::get_instance();
        $rcmail->output->set_pagetitle('my_title');
        $rcmail->output->send('my_plugin.mytemplate');


Where am I supposed to put that part (which file/method)?

SKaero


mabj

Quote from: SKaero on February 14, 2019, 11:27:23 PM
The documentation isn't great, I believe the section your looking for is: https://github.com/roundcube/roundcubemail/wiki/Plugin-API#templates

I finally got it to work, I think. Now to the next question. I would like to use the same method (test_hook) to have several objects with almost the same content. Do I need to do register_handler for each object?

Also, can I sent some attributes to test_hook to distinguish which object to process?

SKaero

If you set attributes in the "roundcube:container" they will be in the $attr array.