Talk:Blocks

Jump to: navigation, search

Moodle 3.0

Update, 4th April 2016

Was working my way through some of this. I think there are a couple of new requirements for Moodle 3.0. I believe the blockname is needed and the content_type must be specified (but I'm not sure as I'm learning too).

This worked for me (took the code from admin_bookmarks and re-edited):

<?php
  
  class block_simplehtml extends block_base {
    public $blockname = null;
    protected $contentgenerated = false;
    // protected $docked = null;
    
    public function init() {
      $this->blockname = get_class($this);
      $this->title = get_string('simplehtml', 'block_simplehtml');
      $this->content_type = BLOCK_TYPE_TEXT;
    }

    public function get_content() {
    // First check if we have already generated, don't waste cycles
        if ($this->contentgenerated === true) {
            return $this->content;
        }
    $this->content         =  new stdClass;
    $this->content->text   = 'The content of our SimpleHTML block!';
    $this->content->footer = 'Footer here...';
    $this->contentgenerated = true;  // I'm not sure if this is needed
    return $this->content;
    }
  }

Make sure you purge or switch off php caching before looking at the effects of code editing!

Moodle 2.6

Update, 11th July 2014

I'm a new developer working through this tutorial and had a few questions/concerns. I suspect that some things in the tutorial are outdated, but beyond that, some of it is just a little confusing to me.

Some thoughts:

--If a block is initialized and has no instance configuration (which is the case for the block in this tutorial when you first add it), $this->config is NULL and a "Strict Standards: Creating default object from empty value" error message occurs in the specialization() function.

--If I'm not mistaken, instance_config_save() strips out all the tags anyway, so the Allow_HTML variable doesn't change anything about the way the text is handled. instance_config_save() also breaks unless you pass in $nolongerused as a second argument.

--I think the tutorial might just benefit from source code files at the end as well since occasionally it's hard to distinguish where code snippets are supposed to go (also whether a code snippet is intended to go anywhere at all or if it's simply an example, as with the global variable $allowHTML.

Moodle 2.0


Update, 28th December 2011

I have removed the instance_allow_config() method from the tutorial. As noted by Mike Churchward, this method is no longer used in Moodle 2.0. After double-checking in the code base, it turns out that basic instance config is automatic, and the edit_form.php file is all that is required to extend this. The appendix has also been updated as required.

This one got past me because while instance_config_print() is marked as deprecated, instance_allow_config() is not. Thanks to Mike for his sharp eyes.

Update, 23rd December 2011

So I tried implementing a tree block to prove my code, and there's a slight issue. The block_tree code that was submitted by Alan references a tree_item abstract class and descendants, and the renderer code expects this class in order to function correctly. Problem is, none of these classes are anywhere to be found in the Moodle 2.x code base, and believe me, I looked. Turns out this is already on the tracker as #28289. I am noting this in the tutorial, and if this issue is resolved, I will update the documentation accordingly.

PHP closing tags have been removed from the tutorial as discussed previously.