- Moodle 1.9 Extension Development
- Jonathan Moore Michael Churchward
- 283字
- 2021-08-06 17:24:05
Creating a block from scratch
We will start our exploration of block plugins by revisiting a programming classic, Hello World.
Our first block will be about as simple a block as possible, simply displaying the text Hello World
.
Because our block will be called Hello World, let's start by creating a folder in the block
folder of our development site called helloworld
. Inside of this folder, create an empty PHP file called block_helloworld.php
. Note that the pattern is module-type_module-name.php
.
We start by creating a new class block_helloworld
that extends the block_base
class. Then we create a new init
function for this class. We set the two required values: the block title and the block version, as follows:
<?php class block_helloworld extends block_base { function init() { $this->title = get_string('helloworld', 'block_helloworld'); $this->version = 2009050700; }//function init
Assigning content to our block
Once we have created the init()
function, all that is left in order to have a functioning block is to assign some content to our block for display in the get_content()
function. Block content is stored in $this->content:
function get_content() { if ($this->content !== NULL) { return $this->content; }//if $this->content = new stdClass; $this->content->text = 'Hello World!'; return $this->content; }//function get_content }//class block_helloworld ?>
Now try out the results of our new code, by logging into Moodle with an administrator account. Click on the Notifications link in the Site Administration block. Moodle then notifies us that it has installed the [[helloworld]]
block.
Now let's turn on editing, and from the drop-down list of blocks we will see our new block [[helloworld]]
at the bottom of the list. We then click on it to add an instance of the block to our current page. The results will look similar to the following screenshot:

- TensorFlow與自然語(yǔ)言處理應(yīng)用
- 中文版After Effects CC 2020完全自學(xué)一本通
- Talend Open Studio Cookbook
- Microsoft SharePoint 2010 Power User Cookbook: SharePoint Applied
- 剪映視頻后期剪輯零基礎(chǔ)入門到精通
- AutoCAD 2020從入門到精通
- Photoshop電商設(shè)計(jì)與產(chǎn)品精修實(shí)戰(zhàn)(微視頻版)
- 數(shù)碼攝影后期密碼Photoshop CC調(diào)色秘籍(第2版)
- 新編 中文版Photoshop平面設(shè)計(jì)入門與提高
- PKPM 2010結(jié)構(gòu)分析從入門到精通
- 寫(xiě)給大家看的PPT設(shè)計(jì)書(shū)(第2版)
- Joomla! E/Commerce with VirtueMart
- Microsoft SQL Server 2008 High Availability
- Service Oriented Architecture with Java
- AutoCAD 2022中文版實(shí)用教程