how can I populate an edit area of a block config?

how can I populate an edit area of a block config?

by Daniele Cordella -
Number of replies: 0
Picture of Core developers Picture of Plugin developers

I am trying to get familiarity with moodle 2 developing a very silly block. The block looks empty but by providing a title and welcome text in the configuration page (using the configuration icon just below the block title bar) the block looks different: with a title amd a welcome text in its content. As you can understand, this block is really similar to the HTML block. This is the reason why I went to read the code of that core block. What I found is that... to populate the 'config_text' editor area, the code used in moodle20/blocks/html/edit_form.php is

    function set_data($defaults) {
        if (!empty($this->block->config) && is_object($this->block->config)) {
            $text = $this->block->config->text;
            $draftid_editor = file_get_submitted_draft_itemid('config_text');
            if (empty($text)) {
                $currenttext = '';
            } else {
                $currenttext = $text;
            }
            $defaults->config_text['text'] = file_prepare_draft_area($draftid_editor, $this->block->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $currenttext);
            $defaults->config_text['itemid'] = $draftid_editor;
            $defaults->config_text['format'] = $this->block->config->format;
        } else {
            $text = '';
        }
        // have to delete text here, otherwise parent::set_data will empty content
        // of editor
        unset($this->block->config->text);
        parent::set_data($defaults);
        // restore $text
        $this->block->config->text = $text;
    }

What I use, instead, is:

    function set_data($defaults) {
        $defaults->config_welcometext['itemid'] = file_get_submitted_draft_itemid('config_welcometext');
        if (!empty($this->block->config) && is_object($this->block->config)) {
            $defaults->config_welcometext['text'] = $this->block->config->welcometext['text'];
            $defaults->config_welcometext['format'] = $this->block->config->welcometext['format'];
        } else {
            $defaults->config_welcometext['text'] = get_string('welcometext', 'block_contacts');
            $defaults->config_welcometext['format'] = FORMAT_HTML;
        }
        // have to delete text here, otherwise parent::set_data will empty content
        // of editor
        unset($this->block->config->text);
        parent::set_data($defaults);
        // restore $text
    }

My question is: Am I correct? What am I missing using this shorter code?

Thanks in advance.

Average of ratings: -