Forums | Mahara Community

Developers /
How to pass a variable with a value from instance_config_form to render_instance?


anonymous profile picture
Account deleted
Posts: 9

05 July 2012, 7:16

Hi there

I'm a newbie in Mahara/PHP.

I'm trying to right a custom plugin (blocktype) in Mahara. I've gone through the tutorial of how to build a blocktype.

What I want to achieve is: storing the username/id of the current logged in user into a variable with end user entering any sort of information apart from clicking Save button.

I have a JS that uses this id to refer some resources in drupal. Also, the end users can share this page with other users.

In short: End users will import their own drupal resources (kind of forum entries) into Mahara pages and share with other users.

Using global $USER, I can get the current logged in userid. I want to save this into a variable in instance_config_form and pass it to render_instance. And I dont want any input fields in the form.

Can some one give me any ideas on how to do this?

Gregor Anželj's profile picture
Posts: 349

05 July 2012, 14:58

Haven't tested it, but that should do it...

    public static function instance_config_form($instance) {
        global $USER;
        $configdata = $instance->get('configdata');
        $userid = (isset($configdata['userid']) && !empty($configdata['userid'] ? $configdata['userid'] : $USER->get('id'));
       
        return array(
            'userid' => array(
                'type'  => 'hidden',
                'value' => $userid,
            )
        );
    }

    public static function render_instance(BlockInstance $instance, $editing=false) {
        $configdata = $instance->get('configdata');
        return $configdata['userid'];
    }

A little explanation:

In function instance_config_form (3rd line: $userid = ...) set user id form value that was written/stored in DB, if it wasn't written yet, than use $USER->get('id')

In function render_instance get value of user id written/stored in the DB and than return that value, that should print it...

HTH,
Gregor

anonymous profile picture
Account deleted
Posts: 9

05 July 2012, 19:59

Thanks very much Greg. Appreciated.

It worked for me.

Regards,

Sandeep Kankatala

A post by Account deleted was deleted

4 results