Looking for a more complete example: overloading block_base html_attributes function

Looking for a more complete example: overloading block_base html_attributes function

by Carvell Fenton -
Number of replies: 7

Hello all,

This is a Moodle development question, and I searched as best I could here and don't see any answers. I am sure this is a beginner question, I am a beginner when it comes to Moodle development, so hopefully I am not missing something completely obvious. Feel free to point me to another post if there is one on the topic.

I was working my way through the blocks tutorial (http://docs.moodle.org/en/Development:Blocks) and was trying the Eye Candy section. I can get the example code working, adding the alert, but I am not having any luck with other CSS changes. Several things:

  1. Can anyone provide an example of an overloaded html_attributes function that modifies or uses a few more attributes? For example, suppose I wanted to use background-color:yellow. How would that be included in the associative array?
  2. A note about the example code in the tutorial: the html_attributes example does not include a call to parent::html_attributes(), but the description of html_attributes() in Appendix A does. Is one way "more correct" than the other?
  3. Lastly, is setting up the look of my block as simple as getting the array initialized correctly in the html_attributes function, or are there other changes I need to make (e.g. weblib.php)? I searched for other block files that use html_attributes but it didn't show up anywhere except my file and the block_base class. Perhaps people don't customize?

Thanks in advance,

Carvell

Average of ratings: -
In reply to Carvell Fenton

Re: Looking for a more complete example: overloading block_base html_attributes function

by Carvell Fenton -

Hi all,

No one is chiming in on this one, so I thought I would check; is this the wrong forum location for these types of questions?

Carvell

In reply to Carvell Fenton

Re: Looking for a more complete example: overloading block_base html_attributes function

by Frank Ralf -
Hi Carvell,

I'm not quite sure if I understand what you want to achieve, but most presentation related questions might be better posted to the Themes forum (http://moodle.org/mod/forum/view.php?id=46).

In general you should strive for modifying the look of your site using CSS instead of changing the HTML output. Please see Themes FAQ and CSS FAQ for more information.

hth
Frank
In reply to Frank Ralf

Re: Looking for a more complete example: overloading block_base html_attributes function

by Carvell Fenton -

Thanks for the response Frank,

I was under the impression from the Moodle docs Development:Blocks tutorial that changing the html_attributes function would allow me to change the look of a new block. I guess maybe that is not what that function in the block_base class is for. This was the entry that made me think that: http://docs.moodle.org/en/Development:Blocks/Appendix_A#html_attributes.28.29

I will have a look around the links you have provided.

Thanks again,

Carvell

In reply to Carvell Fenton

Re: Looking for a more complete example: overloading block_base html_attributes function

by Nadav Kavalerchik -
Picture of Core developers Picture of Plugin developers Picture of Testers Picture of Translators
i had some unsuccessful experience with that function too (html_attributes)

here is what i used:

function html_attributes() {
return array(
'class' => 'sideblock block_'. $this->name()
//,'onmouseover' => "alert('Mouseover on our block!');"
);
}

and it was "hanging" the UI.
no "delete" and "hide" icons on the commands list.
and i am unable to move the block on an ajax enabled course UI.

unremarking the mouseover works fine smile
but you can see it is only for testing.

removing the entire function makes it functional again.

i really needs those classes to work since i use some JS code that depend on them
it is not for visual manipulations.

here is a link to this quick-course-settings block
(that suppose to change course settings quickly)
it is a work in progress, so expect this link to change from time to time.
i hope this issue is solved by the time you click it smile
In reply to Nadav Kavalerchik

Re: Looking for a more complete example: overloading block_base html_attributes function

by Nadav Kavalerchik -
Picture of Core developers Picture of Plugin developers Picture of Testers Picture of Translators
i was (luck) quick smile
issue was solved.

here is the new working html_attributes function

function html_attributes() {
$attrs = parent::html_attributes();
// Add your own attributes here, e.g.
// $attrs['width'] = '50%';
$attrs['class'] = 'sideblock block_'. $this->name() ;
return $attrs;
}

i really needed to add some attributes to the original ones without over-ridding them completely.

here is a more complete help document:
http://astro-varna.com/eduportal/blocks/HOWTO.html
see the html_attributes section smile
In reply to Nadav Kavalerchik

Re: Looking for a more complete example: overloading block_base html_attributes function

by Carvell Fenton -

Hi there Nadav,

Thanks for the reply. I ended up not spending much more time on using the html_attributes function, but I will look at it again based on your comments. There may be some things there I can do now based on what you have shown.

Take care,
Carvell

PS. We look a little bit alike, perhaps we're related wink Just a little avatar joke!

In reply to Nadav Kavalerchik

Re: Looking for a more complete example: overloading block_base html_attributes function

by Carvell Fenton -

Hi there Nadav,

Thanks for the reply. I ended up not spending much more time on using the html_attributes function, but I will look at it again based on your comments. There may be some things there I can do now based on what you have shown.

Take care,
Carvell

PS. We look a little bit alike, perhaps we're related wink Just a little avatar joke!