How to set field labels in "Configuring a block" - plugin development

How to set field labels in "Configuring a block" - plugin development

by Lawrence Lagerlof -
Number of replies: 2

How to set labels in custom fields in Configuring a Block? (I named it simplehtml). It's from the official Moodle Block tutorial that is for Moodle 2.0+. I am using the 3.4 version.

In my output below I have two labels blockstring and blocktitle, that I suppose to be placeholders to be set in /simplehtml/lang/en/block_simplehtml.php, but I could not figure out how to do it. I need some help.



And here is the code that matters:


edit_form.php

<?php
class block_simplehtml_edit_form extends block_edit_form {
protected function specific_definition($mform) {
$mform->addElement('header', 'config_header', get_string('blocksettings', 'block'));

$mform->addElement('text', 'config_text', get_string('blockstring', 'block_simplehtml'));
$mform->setDefault('config_text', 'default value');
$mform->setType('config_text', PARAM_RAW);

$mform->addElement('text', 'config_title', get_string('blocktitle', 'block_simplehtml'));
$mform->setDefault('config_title', 'default title value');
$mform->setType('config_title', PARAM_RAW);
}
}



block_simplehtml.php

<?php
class block_simplehtml extends block_base {
public function init() {
$this->title = get_string('simplehtml', 'block_simplehtml');
}

public function get_content() {
if ($this->content !== null) {
return $this->content;
}

$this->content = new stdClass;

if (! empty($this->config->text)) {
$this->content->text = $this->config->text;
}
$this->content->footer = 'Footer here...';

return $this->content;
}

public function specialization() {
if (isset($this->config)) {
//$this->title = get_string('defaulttitle', 'block_simplehtml');
} else {
$this->title = $this->config->title;
}

if (empty($this->config->text)) {
$this->config->text = get_string('defaulttext', 'block_simplehtml');
}
}
}


lang/en/block_simplehtml.php

<?php
$string['pluginname'] = 'Simple HTML block';
$string['simplehtml'] = 'Simple HTML';
$string['simplehtml:addinstance'] = 'Add a new simple HTML block';
$string['simplehtml:myaddinstance'] = 'Add a new simple HTML block to My Moodle page';




Average of ratings: -
In reply to Lawrence Lagerlof

Re: How to set field labels in "Configuring a block" - plugin development

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Just add the following to lang/en/block_simplehtml.php (and purge caches):


$string['blocktitle'] = 'Title';
$string['blockstring'] = 'String';
Then go and turn Debugging on, as the warning messages would have told you exactly what you needed to do.
Average of ratings: Useful (1)
In reply to Davo Smith

Re: How to set field labels in "Configuring a block" - plugin development

by Lawrence Lagerlof -

I already tried this and didn't worked, but I didn't purge the cache before testing.

I purged it now, and everything is working flawlessly.

Thank you.