Tuesday, August 30, 2011

Drupal 7 adding a body class that tells us whether a region is empty of blocks

Sometimes, especially in the style.css file, there is a need to understand whether a theme region is empty. A good solution to achieve this is to add a body class. In order to achieve this, you could make use of Drupal 7 template_preprocess_html(&$variables)

Just add to your template.tpl.php file of your theme the following function:

function YourThemeName_preprocess_html(&$variables) {
if (empty($variables['page']['RegionName'])) {
$variables['classes_array'][] = 'YourClassName';

Sunday, August 28, 2011

Drupal 7 possible override field templates

field.tpl.php - Default template implementation to display the value of a field. This file is not used and is here as a starting point for customization only.

Possible override templates are:
  • field.tpl.php
  • field--field-type.tpl.php
  • field--field-name.tpl.php
  • field--content-type.tpl.php
  • field--field-name--content-type.tpl.php
For example, if you want to override the 'field_image' in your 'product' content type, you should create the template field--field_image--product.tpl.php.

Futhermore, if you want to load the node in which field is attached, you should use the $element['#object'] entity.

For example, if you want to take the language of the node in which field is attached, you can use the following code <?php $node=$element['#object']; $lang = $node->language; ?>

Wednesday, August 3, 2011

$(document).ready and $(window).load

jQuery offers two powerful methods to execute code and attach event handlers: $(document).ready and $(window).load. The document ready event executes already when the HTML-Document is loaded and the DOM is ready, even if all the graphics haven’t loaded yet. If you want to hook up your events for certain elements before the window loads, then $(document).ready is the right place.

$(document).ready(function() {
// executes when HTML-Document is loaded and DOM is ready
alert("document is ready");

The window load event executes a bit later when the complete page is fully loaded, including all frames, objects and images. Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself.

$(window).load(function() {
// executes when complete page is fully loaded, including all frames, objects and images
alert("window is loaded");