Tag Archives: Drupal Development

Drupal – Add Javascript in Theme

As discussed before, we can add Javascript/JQuery in Drupal modules.
Drupal – Create Javascript for Module

So if the Javascript/JQuery is related to the layout and independent of the module, we should add it in the theme instead. For example, i want to have a alert box whenever a page is loaded, i should then add it to my current Drupal theme. The steps are listed below.

1. Open you current theme folder and add the following line in the theme.info.

  • scripts[] = js/theme.js

Continue reading Drupal – Add Javascript in Theme

Drupal – Add Javascript in Module

In Drupal, you can add your Javascript to module or theme. If the javascript is related to the module logic and independent of the theme, we should add the .js file in the module. Moreover, Drupal already includes the JQuery library. So we can use the JQuery functions in the .js file.

Let’s recall the custom module we have created in previously.
Drupal – Create a Block

i want to show an Javascript alert box whenever the block is shown. Let’s create a custom.js in <drupal_root>/sites/all/custom.

jQuery(document).ready(function() {
	alert("Hello World");
});

Continue reading Drupal – Add Javascript in Module

Drupal – Customize the Views Query

I want to create a taxonomy view which shows the term name and the term image. For those terms without term image, i want to filter out them. Unfortunately there is no term image filter in the view setting. One work around is overriding the view query by implementing the hook_views_pre_execute(&$view) in a custom module. Before you construct the new SQL, you should set all your desire settings in the view editing page first. Then you can create a new SQL base on that view query.

So in the custom module, add the following function. Continue reading Drupal – Customize the Views Query

Drupal – Ubercart Localization

Localization with Ubercart is somehow a bit difficult. Assume we have 2 languages, English and Traditional Chinese, installed. For a particular product, there will be one node representing the English one and the other node representing the Traditional Chinese one. If you add the English node into the cart, it will not be translated in the cart and checkout page. In other words, they are treated as 2 different products.

The following example can localize the Product content type even in the cart and checkout page. and it is based on the work by Stewart Adam. Please refer to for the post – Translating or internationalizing an Ubercart store: Common problems & solutions more information.

1. Create a custom module and add the following hook implementation in the .module file. Continue reading Drupal – Ubercart Localization

Drupal – Add Configurable Values to Block

In previous posts, we created a custom module implement the hook_block() function.

 

We haven’t implemented the block configurable values. Let’s do it now.

We want the block to store a number and a string. so we add these 2 fields in the block configuration page. Here is the new custom.module. Continue reading Drupal – Add Configurable Values to Block

Drupal – Show Database Data in Block by SQL

Previous related posts:

 

The above posts show you how to create and customized the content of a block. Sometimes, we may need to show some database data in the block. So we need to write a SQL. For example, i want to show a list of node titles and the corresponding url links of the Song content type.

1. Modify the custom.module Continue reading Drupal – Show Database Data in Block by SQL

Drupal – Customize Block Template File

In the Drupal template architecture, you can customized a specific block just by creating a .tpl.php file in the theme folder. For example, the block.tpl.php determine all the block layout in your Drupal theme.

We can also override the design layout of the block in our customized module which we created yesterday(Drupal – Create a Block). First we have to create the corresponding .tpl.php for our block. In this case, we need a block-[module-name].tpl.php.

So create the block-custom.tpl.php

<strong>Hello World</strong>
<!-- get the block details from the $block variable -->
<p>Subject:<?php print $block->subject ?></p>
<p>Content:<?php print $block->content ?></p>

Continue reading Drupal – Customize Block Template File

Drupal – Create a Block

You can create your own block and customized its content. Drupal provides the hook_block() function for block creation. We can implement this function in our customized module.

1. Create a customized module by creating the following files.
<drupal_root>/sites/all/modules/custom/custom.info

name = Custom module
description = A custom module which create a block
core = 6.x

Continue reading Drupal – Create a Block