06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Every button is passed through this theme function. Together, all of the buttons<br />

are h<strong>and</strong>ed over to the <strong>Drupal</strong>.theme.prototype.buttonBar() function for<br />

additional theming.<br />

[ 285 ]<br />

Chapter 8<br />

The buttonBar() theme function<br />

The <strong>Drupal</strong>.theme.prototype.buttonBar() theme is also very straightforward.<br />

It's job is to take an array of buttons <strong>and</strong> <strong>com</strong>bine them into a single button bar.<br />

Since more than one button bar may exist on a page, each button bar needs to<br />

have its own ID.<br />

Here's the theme:<br />

<strong>Drupal</strong>.theme.prototype.buttonBar = function (buttons, id) {<br />

var buttonBar = $('')<br />

.attr('id', id);<br />

<strong>jQuery</strong>.each(buttons, function (i, item) {<br />

buttonBar.append(item);<br />

});<br />

return buttonBar.parent().html();<br />

}<br />

First, the new button bar <strong>jQuery</strong> object is created <strong>and</strong> the ID is appended.<br />

Next, we loop through each of the items in the buttons array <strong>and</strong> append each one<br />

to the button bar.<br />

Finally, the button bar is converted to an HTML string <strong>and</strong> returned. It is then<br />

inserted by our main behavior, <strong>Drupal</strong>.behaviors.editor(), into the appropriate<br />

place in the DOM.<br />

We have made it through our largest project. Although we began with an existing<br />

project, there has been a substantial amount of information to cover. However, we<br />

have created a second module. A module tied closely to server-generated content.<br />

A last question<br />

There is a question that might arise in regard to this last project. Why involve the<br />

server in the process the way that we did? Why not simply create the buttons in<br />

<strong>JavaScript</strong> to begin with?<br />

The point of this project was to exhibit how data could be passed from the server.<br />

However, if we wanted to delve into a little more PHP, we could derive even more<br />

from server integration. We could, for example, use the Forms API (FAPI) to create<br />

an administration form, which would allow system administrators to select the<br />

buttons that should appear on a form, or even define their own buttons.<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!