Front End Drupal: Designing, Theming, Scripting
May 12, 2009
|
Learn more about Drupal as we dig in to front end of this exciting open source CMS.
|
Blocks
With your regions established, you can now fill them with
blocks. Blocks may be generated by Drupal core modules,
contributed modules, or custom PHP snippets, including lists
of content created by the Views module.
- Commonly used blocks include the following:
- Navigation menus (created in Administer, Site building, Menus)
- Lists of content (Views module; see Chapter 2)
- Login forms (Drupal core; turned on by default)
- Site categories (Drupal’s Taxonomy module)
- Recent comments (Drupal’s Comment module)
- Search (Drupal’s search module)
- Author information (Drupal’s profile module)
- Five-star ratings (http://drupal.org/project/fivestar)
- Facebook, Digg, and social bookmarking links (http://drupal.org/project/service_links)
- Similar entries (http://drupal.org/project/similar)
You can also create custom blocks with text, images, and
even your own snippets of PHP code. Sample PHP snippets are
available from the Drupal Web site at
http://drupal.org/node/21867. To create a custom block,
follow these steps:
Navigate to to Administer, Site Building, Modules and
enable the PHP Filter module. You may also need to adjust
the permissions for this input format by navigating to
Administer, Site configuration, Input formats and clicking
on the "configure" link next to PHP filter.
- Navigate to Administer, Site building, Blocks.
- Select the tab "Add block."
- Add a "Block description." This description specifies how the block will be identified in the administration area and is a required field.
- Add a "Block title" if you would like a title to appear at the top of the displayed block. This field is optional.
- Put your text, images, and PHP snippet into the "Block body." You could also use plain text or HTML markup here if it was appropriate for your block.
- Update the "Input format" to PHP.
- Adjust the visibility settings for the "User," "Role," and "Page" roles.
- Scroll to the bottom of the Web page and click "Save."
PHP snippets in blocks - Blocks with custom PHP snippets
could break the display of your site if they contain errors.
Be sure to carefully test your snippets before placing them
into a block. Place your PHP snippet into the body of a
private page to confirm that it will not break your site
before deploying the snippet as a block.
Sites will sometimes have more screen real estate
dedicated to blocks than to the main content on each page,
especially when the blocks provide additional information
for the node that is displayed on the page, such as author
profile information or related content. Don’t be shy! Enable
the most appropriate blocks for each part of your Web site.
Blocks are included in Drupal’s caching system and will not
harm the overall performance of your site. To enable caching
for blocks, navigate to Administer, Site configuration,
Performance. Under the section "Block cache," choose
"Enabled." Scroll to the bottom of the Web page and click
"Save configuration."
Customizing the Markup of Blocks
You may change the markup of the blocks displayed in your
page template by creating a new template file,
block.tpl.php. Drupal’s default for this template contains
only a few wrapper HTML elements:
<div id="block-<?php print $block->module .'-'. $block->delta; ?>"
class="block block-<?php print $block->module ?>">
<?php if ($block->subject) { ?>
<h2><?php print $block->subject; ?></h2>
<?php } ?>
<div class="content">
<?php print $block->content ?>
</div>
For blocks provided by Drupal core, the variable $block-
>delta represents the order in which this block was created.
For example, the first block has a delta value of 1, the
second has a delta value of 2, and so on. In rendered HTML,
the first line would look like this:
<div id="block-user-1" class="block block-user">
As you can see, the output is not nearly as complicated
as the variables would suggest! Check the output to see what
your module is using for its delta value. Some modules
provide a text delta instead of a numeric delta.
A full list of block template variables is available from
the default block template. This file can be found in your
Drupal system files:
modules/system/block.tpl.php. A full list of
the variables is also available online at http://api.drupal.org/api/file/modules/system/
block.tpl.php.
Regions
Drupal Front End
Search
|