Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

C H A P T E R 1 0

■ ■ ■

Working with Blocks

Blocks are snippets of text or functionality that can be placed in the regions defined in your template. Blocks can be anything from a single node, a list of nodes, a calendar, a video, a form, an online poll, a chat window, a feed of Facebook status updates, or virtually anything else you can dream up. When I talk to clients about blocks, they often respond with “Oh, you mean a block is just a widget,” which is often the common term used outside of the sphere of Drupal for things that are represented as blocks in Drupal. In this chapter, I’ll show you how to create custom blocks and use them on your Drupal web site.

What Is a Block?

A block is essentially a stand-alone container that can be used to house virtually anything you can think of. It might be easiest to understand what a block is by examining a few examples (see table 10-1). The following list represents some of the standard blocks that ship with Drupal.

Table 10-1. Standard Blocks

Block

Description

 

 

Login form

The form displayed on a site that allows a visitor to log in,

 

register for a new account, or reset his or her password

Who’s online

A block that lists all of the users who are currently logged onto

 

the site

Who’s new

A block that displays a list of the newest users on the web site

Search form

The search form is contained within a block.

Recent comments

A list of the most recent comments posted on the site

Main menu and secondary menu

Both menus are available as a block.

Recent content

A list of the most recent nodes posted on the site

223

CHAPTER 10 WORKING WITH BLOCKS

Continued

Block

Description

 

 

Most recent poll

The last poll created on the site is displayed as a block (requires

 

that the Poll module be enabled).

Active forum topics

A list of forum topics that have recent activity (requires that the

 

Forum module be enabled)

 

 

Several contributed modules include blocks as a component of the functional solution they deliver. Ubercart, for example, provides numerous blocks that are used to display things like the visitor’s shopping cart status.

With the Block API and the block administration interface, you have the ability to create your own custom blocks for virtually any purpose you can think of (see table 10-2). Examples of custom blocks that I’ve recently created include the following:

Table 10-2. Examples of Custom Blocks

Block

Description

 

 

Recent Bloggers

A block that displays a gallery of avatars for the last bloggers who have

 

posted to the site

Slideshow of upcoming events

A block that displays the node teasers for upcoming events as a

 

slideshow

A chat form

A block that incorporates the Meebo chat widget and displays the chat

 

widget in a right or left sidebar

A donate now feature

A block that displays a button that allows a visitor to click through to

 

PayPal, allowing the visitor to make a donation

A list of new books added to a

A block that displays a mini version of a book jacket, the book’s title,

library’s collection

author, and reserve it button

A contact us form

A block that displays a simple contact request form

A list of postings on multiple

A block that displays feeds from several social networking sites as a

social networking sites

mashup in a single list

A Google map showing recent

A block that renders markers on a Google map for nodes that include

postings

geographic information in their body

 

 

Blocks are defined either through Drupal’s web interface (see figure 10-1) or programmatically through the block API (module-provided blocks). How do you know which method to use when creating a block? A one-off block such as a bit of static HTML related to the site is a good candidate for a custom

224

CHAPTER 10 WORKING WITH BLOCKS

block. Blocks that are dynamic in nature, related to a module you’ve written, or that consist of mostly PHP code are excellent candidates for using the block API and for being implemented within a module. Try to avoid storing PHP code in custom blocks, as code in the database is harder to maintain than code written in a module. A site editor can come along and accidentally delete all that hard work too easily. Rather, if it doesn’t make sense to create a block at the module level, just call a custom function from within the block and store all that PHP code elsewhere.

Figure 10-1. The block overview page

Tip A common practice for blocks and other components that are site-specific is to create a site-specific module and place the custom functionality for the site inside that module. For example, the developer of a web site for the Jones Pies and Soda Company may create a jonespiesandsoda module.

225

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]