Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Talk:Blocks: Difference between revisions

From MoodleDocs
mNo edit summary
mNo edit summary
 
(34 intermediate revisions by 11 users not shown)
Line 1: Line 1:
== "Notifications" ==
{{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).


At this part: "At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it and after seeing it in action come back to continue our tutorial."
This worked for me (took the code from admin_bookmarks and re-edited):
<pre>
<?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;
    }


This is not clear since Moodle no longer has a single administration page.
    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;
    }
  }
</pre>


More to the point would say to go to the /admin/index.php page for your Moodle install. On my latest version of Moodle 1.8 the link in the Administration block is labeled "Notifications".
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.


== Splitting up ==
Some thoughts:


I split the page up into sub pages because I got an error message that the page was over 32 KB. The three appendixes are now separate pages. --[[User:Frank Ralf|Frank Ralf]] 12:16, 26 January 2009 (CST)
--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.


== Increasing readability ==
--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.
* Colored the code
* Inserted some line breaks
* TODO: correcting the old links (Blocks_howto) --[[User:Frank Ralf|Frank Ralf]] 15:55, 26 January 2009 (CST)


== New new settings.php method ==
--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.
There is a new settings.php method to do this, I don't know that much about it, so could someone who does change this page please?--[[User:Mike Worth|Mike Worth]] 05:01, 28 January 2009 (CST)
 
{{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 [http://tracker.moodle.org/browse/MDL-28289 #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.

Latest revision as of 22:21, 7 April 2016

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.