28 July 2011

How to theme Unordered (item) list with unique id or class assigned to all LI in Drupal

Drupal has many built in theme function to construct HTML elements, One of the frequently used is 'Item_list'. This theme function used to build a HTML ordered / unordered list.
<?php
$data = array('List 1','List 2', 'List 3');
$output = theme('item_list', $data);
?>
The Above use of theme function will output the <UL> and <LI> without any class or id.
For Example:
<ul >
<li > List 1 </li>
<li > List 2 </li>
<li > List 3 </li>
</ul>


If we need to have the output as below.
For Example:
<h3> Title </h3>
<ul class="my-list">
<li clase="list" id="list-1"> List 1 </li>
<li clase="list" id="list-2"> List 2 </li>
<li clase="list" id="list-3"> List 3 </li>
</ul>


Then our code will look like.

<?php
$items = array(
array('data' => 'List 1', 'Class' => 'list', 'id' => 'list-1'),
array('data' => 'List 2', 'Class' => 'list', 'id' => 'list-2'),
array('data' => 'List 3', 'Class' => 'list', 'id' => 'list-3')
);
$title = 'Title';
$output = theme('item_list', $items, $title, 'ul', array('class' => 'my-list'));
?>

In $items array for each item we defined an array with key data, class and id, this will be transformed to HTML.
Title parameter will be printed prefix to the UL tag, with header tag.
And 'ul' parameter decides the type ordered / unordered tag to be printed. defualt is UL
Last a class attribute to UL tag, you may add id or any HTML attribute to UL / OL tag, defining inside the array as key => value.
Refer this in Drupal API

27 July 2011

A journey with Drupal as a developer

Drupal - The word I heard in my office four years back(2007). From then with my company (Unimity Solutions Pvt. Ltd.), I started adopting the Drupal in my profession.

The early days when we started developing drupal 5 projects, it was very hard to understand.

For instance if we think about the form. A form in php is very simple, define the Form HTML with action attribute pointing to a PHP page, and the php page will manage all the server side functionality. But in Drupal you have to define the form in a array and you need to follow the hook system of drupal to handle the form server side validation and submission. To understand the Drupal form you must have knowledge about drupal hook system then you need to understand form api(which is a separate tutorial). First of all we are not able to relate any way the approach of PHP(or any platform) form submission and the drupal form.

Next if you talk about theme, Drupal has separated the logic and HTML. You will have a theme function or template file to put your HTML. Which is also very new to me. I use to put all HTML and PHP in the same page.

You will understand from my above views, that at early stage how pour I'm, I don't have proper knowledge about MVC also.

But as time move, I slowly started learning Drupal, at the same time I also started to understand the MVC concept, Web standards, Re-usablity, Scalability, Performance, Cache, Security, etc.As all these are properties of Drupal.

Now I'm capable to doing anything as required, by following all above said concepts, features and best practices. Drupal teach me all those concepts and features. Now I can  even build a framework. Thanks to Drupal.

If I praise Drupal, then I must not forgot the community, which is working for the development of Drupal framework. All my praise to Drupal is directly linked to the community.  Now I thought of giving back the community. For that as a first step I donated to Drupal Association and became individual member. The Second thing is now I started writing tutorials about Drupal. This Blog is the first step to the tutorial. In coming days I will write about Drupal, Web2.0, Jquery and Mobile technology.

Thanks to Drupal and its community.
My Drupal Profile