- Moodle 1.9 Extension Development
- Jonathan Moore Michael Churchward
- 683字
- 2021-08-06 17:24:06
Adding instance configuration
We will now add some real functionality to our block by adding the ability to configure what the block will display. This is done by adding another function to the block class. Moodle makes it very simple to add this functionality. With just a few lines of code we will add a full WYSIWYG editing capability for entering the text to be displayed by our block. When completed, our simple Hello World block will have most of the functionality of the built-in HTML block, plus it will have its own capability to control access to who sees the resulting information.
To begin, let's tell Moodle that our block allows each instance to be configured. We do this by adding the instance_allow_config
function, and having it return true:
function instance_allow_config() { return true; }//function
Note that all Moodle modules also support Global Configuration, which is covered inChapter 3, Creating and Modifying Filters, on Filter Modules.
Capturing your configuration content
Now we make a config_instance.html
file. Add the following text to this file. The first line references and prints a new text string that we will have to add to our language file for the block. This illustrates how simple it is to reference an arbitrary new string in our code. It is important that any code that we create uses this string rather than hardcoding the text in the code. This is necessary for our code to function properly for localization to other languages. The next line prints a textarea
input box with the contents, if any, of this instance of the block. The last line displays Moodle's integrated WYSIWYG editor, which is based on the HTMLArea JavaScript editor (this editor is scheduled to be replaced in Moodle 2.0, in order to provide better browser support):
<?php print_string('configcontent', 'block_helloworld'); ?> <?php print_textarea(true, 10, 50, 0, 0, 'text', $this->config->text); ?> <input type="submit" value="<?php print_string('savechanges') ?>" /> <?php use_html_editor(); ?>
Note that this is not a complete HTML form; Moodle pulls in this partial form information and turns it into a complete and functional form for us.
Displaying your content
Finally, we have to edit one line in our get_content
function. Our original text assignment was:
$this->content->text = 'Hello World!';
Let's change it to the following new line, which will grab the text entered into the configuration editor and display it in the block:
$this->content->text = $this->config->text;
In the following screenshot, we see how this new configuration provides not just the ability to enter arbitrary text, but also to format that text by using Moodle's WYSIWYG editor:

The end result of these new changes can be seen in the following screenshot. Notice the edit icon next to the block display, which was added as a result of adding the instance_allow_config()
function. This version of the block is found in the helloworld_final
source code folder. While we are still using roughly the same text, we have made it larger, and have formatted it to be bold and italic:

We now have a fully-functional and usable block. At this point, the title Hello World may be the wrong name for our block. With the latest changes it is actually very similar to the core HTML block, but with the addition of its own dedicated capability to control access. Modifying the HTML block to add new capability controls is actually a common project. The HTML block is very powerful because it's the Swiss army knife of blocks. It's not always the most elegant solution to a problem, but it's often possible to put together some HTML in this block in order to quickly and easily make changes to the Moodle interface. Adding a capability control that allows the block to be visible or invisible to certain groups of users adds just enough flexibility to meet a whole new class of common use cases. We can take this basic template and change the context for the capability to work in different situations. This is especially useful if you need to toggle the visibility of a course sticky block, which doesn't currently work with role overrides. This also illustrates how Moodle's API allows us to add a lot of functionality with a small amount of code.
- UG NX 12.0中文版完全自學一本通
- Autodesk Ecotect Analysis 2011綠色建筑分析應用
- Unity 2D與3D手機游戲開發(fā)實戰(zhàn)
- UG NX 9.0中文版 基礎教程 (UG工程師成才之路)
- 3ds Max & Unreal Engine 4:VR三維建模技術實例教程(附VR模型)
- Creo Parametric 5.0中文版從入門到精通
- AutoCAD 2014實用教程(第4版)
- The PEAR Installer Manifesto
- UG NX 9中文版從入門到精通
- 正則表達式必知必會(修訂版)
- CAXA電子圖板2016基礎與實例教程
- 邊做邊學:Illustrator CS6平面設計案例教程(微課版)
- Practical Data Analysis and Reporting with BIRT
- 玩轉微信5.0
- Inkscape Starter (Microcontent)