The best thing to do is usually to copy an existing block, and change the code to do what you need your new block to do.
The block called "blog_menu" is quite simple, so you can use that as an example. Make your own copy of it, and you can delete everything in the "get_content()" function (except the top line that starts with "global"), and replace it with your own code to "echo" your content into the webpage.
Let's say you want to call it "myblock", then the other changes that you need to make are:
- Remove the "require_once" line at the top
- In the "init()" function, change the "$this->title =" line to store the title of your block
- Rename the file to "block_myblock.php"
- Create a new folder in your Moodle install called "blocks/myblock", and put the PHP file there
After that, visit your admin page in Moodle (usually click "Notifications" or something similar), and you are ready to go!
Hope that helps.
-Peter
I have a new block that I want appearing in the centre, but want to be able to have my own header for it, and put what ever I want in it.
I have played around, hiding the centre blocks and attempting to set them right blocks width to 100% to see if that will make it stretch out, but this doesnt seem to work either.
Any help will be much appreciated
Kind Regards
Sam
As far as I know, I'm afraid it's not possible to put a block in the centre of a Moodle page (not under normal circumstances anyway... you could probably hack a theme to try it, but you might end up with odd results!).
Thank you very much.I am trying to create a new block as your suggestion but I am getting error: "Block link_menu: class does not have a constructor"
Here is my code:
<?php //$Id: block_blog_menu.php,v 1.16.2.2 2008/05/02 04:07:31 dongsheng Exp $
//require_once($CFG->dirroot .'/blog/lib.php');
class block_link_menu{
function init() {
$this->title = "My Link";
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2007101509;
}
function get_content() {
global $CFG, $USER, $COURSE;
echo "aaaaaaaaaaa";
}
}
?>
can you plz suggest me where is the problems in my code and how I will craete a new block.
Thanks.
Madhabendra
Hi
you are missing extends block base
class block_link_menu extends block_base {
Simon
I have created a block, but after adding this to front page I am getting an extra line break on top of the page. What may be the issue?
There is nothing special in $this->content->text
$this->content->text = "abc"
Please help.
Thanks,
jaswant
No there is no any extra DIV. I already checked with firebug. This is only happens when I set show the block when I hide it there is no extra break.
The block internal HTML is okay have checked using firebug.
Thanks,
Jaswant
And please post a screenshot.
hth
Frank
Its solved, Thanks for the help.
Cheers
I have done that in moodle 2.0, hope that help:
I have a problem. I would like to have my custom-block with background-color different from all the other blocks.
Somebody can tell me how can I do?
Add this to your block code, within the class declaration:
function html_attributes() {
$attributes = parent::html_attributes();
$attributes['style'] .= 'background-color: #0000ff';
return $attributes;
}
where #0000ff is the background colour you want- the code above will result in a blue block!
This function overrides a function in the block_base class and adds to the html attributes of the block HTML element.
OK: here's where it gets interesting. Anomaly has a more complex CSS structure than most, with inner rounded corners.
This means that you'll need to make the changes in the CSS directly.
First: You'll need to undo the changes made to your block code (or comment it out!)
In Moodle 2.0, the file that defines the look of blocks for the 'Anomaly' theme is at (moodle root)\theme\anomaly\style\browser.css
Note that there are some references to block styling:
.block {border-width:0px;background-color:white;}
.block .corner-box {border:1px solid #C8C9C7;margin-bottom:10px;padding-bottom:10px;background-color:#E3E3E3;}
.block .corner-box .rounded-corner {background-image:url([[pix:theme|corners_block]]);position:relative;}
.block .corner-box .rounded-corner.top-left {background-position:0px 0px;float:left;top:-1px;left:-1px;}
.block .corner-box .rounded-corner.top-right {background-position:11px 0px;float:right;top:-1px;right:-1px;}
.block .corner-box .rounded-corner.bottom-left {background-position:0px 11px;float:left;bottom:0px;left:-1px;}
.block .corner-box .rounded-corner.bottom-right {background-position:11px 11px;float:right;bottom:0px;right:-1px;}
These set up the general look of all blocks.
To change the CSS for your specific block, we'll need to add some entries specific to your block. Look at where you defined the block in code- it should look like
class block_your_block extends block {
...
Note the name of the block- in this case 'block_your_block'.
Under the block CSS at (moodle root)\theme\anomaly\style\browser.css, make some new entries like so:
.block_your_block.corner-box {border:1px ;margin-bottom:10px;padding-bottom:10px;background-color:#00ff00;}
.block_your_block .corner-box .rounded-corner {background-image:url([[pix:theme|corners_block]]);position:relative;}
Change the things in boldto achieve the desired result. The above CSS will result in an awful green block- modify #00ff00 to the colour you want.
You'll see funny corners in IE- to fix these you'll need to either remove the line
.block_your_block .corner-box .rounded-corner {background-image:url([[pix:theme|corners_block]]);position:relative;}
and accept that your block will have sharp corners in IE, or, make a copy of the image at (moodle root)\theme\anomaly\pix\corners_block.png- copy it to (moodle root)\theme\anomaly\pix\corners2_block.png and modify it to be the same colour as your block. Then, in browsers.css, change the line
.block_your_block .corner-box .rounded-corner {background-image:url([[pix:theme|corners_block]]);position:relative;}
to
.block_your_block .corner-box .rounded-corner {background-image:url([[pix:theme|corners2_block]]);position:relative;}
Unlike the first approach, this is theme specific- it will only be effective in the 'Anomaly' theme. You'll need to make sure these files are not overwritten when you upgrade.
Once you learn more, another approach could be to write your own theme!
I hope this helps
I have not IE, I hope to have made everything right.
Luciano