Tag Archives: Drupal

My experiences/views on drupal development (module/theme)

Hello I am from Nepal and we have very few drupal developers (module/theme) here. I usually am focused in drupal module developement so had average knowledge of drupal theme development But recently I threw myself a challenge to work in drupal theming to test my skills. I decided to do everything i.e. from PSD to HTML template slicing to drupal theme (template) coding everything from scratch. So that I can have thorough understanding of the drupal theme engine.

I always wanted to make use of CSS framework so I chosed to use 960 grid for PSD theme and the layout I choosed to make is for newspaper.

Fully sliced drupal theme page http://www.samundra.com.np/mynepalinews/

drupal site screenshot for mynepalinews

Later on, I sliced PSD to HTML.

I would rate it intermediate.

Problems faced:
I started to change HTML to drupal compatible theme. The first problem I faced was to choose a good theme which I can use as base. I picked up with “bartik” theme that comes with drupal fresh installation. I copied “bartik” themes folder to sites/default/themes. There inside I renamed the theme name to “basic”. I renamed .info file to reflect the it so the info file now becomes “basic.info”. Inside basic.info I removed regions that I didn’t wanted to use and added my own custom regions as needed. But this theme was not detected by the drupal and always gave me an error “not compatible with drupal7”. I didn’t know what caused the error. After googling for few errors, I came to know that there are few must have regions in drupal theme.

regions[content] = Content
regions[help] = Help
regions[page_top] = Page top
regions[page_bottom] = Page bottom

I removed the page_top and page_bottom regions as they didn’t make any sense to me. As soon as I added them back to the .info file drupal recognized my theme. I added few stylesheet files and then few javascripts.

Customizing the template.php for handling the views template.

I had used many views during the site development. So I copied the appropriate view template file from sites/default/modules/views/theme to my template’s root directory and then added then made changes to the field title ‘views-view-field–title.tpl.php’ because I wanted to wrap title field with <h2></h2> tag.

I had a custom page–front.tpl.php file as this had different regions then the page.tpl.php and node.tpl.php.

Changes for slideshow
To keep it simple. I directly hardcoded the slideshow html markup in the page.tpl.php and page–front.tpl.php file and then initialized the script from the template.php by adding it to template.php

function basic_preprocess_html(&$variables) {

  if (!empty($variables['page']['footer'])) {
	$variables['classes_array'][] = 'footer';

  if (!empty($variables['page']['video_news'])) {
	$variables['classes_array'][] = 'video_news';

  if (!empty($page['video_news_boxed_left']) ||
		  !empty($page['video_news_boxed_right']) ||
		  !empty($page['video_news_boxed_list_items'])) {
	$variables['classes_array'][] = 'video_news_boxed';

  drupal_add_js(path_to_theme() . '/js/slides.min.jquery.js');
  drupal_add_js(path_to_theme() . '/js/slides-init.js');

directly added the line 17, line 18 myself. This made the slideshow functional.
I created many blocks from views. I assigned them directly to regions.

Knowledge gained drupal theme development :
So this experience of drupal theme development has sharpened my knowledge of drupal’s internal theme engine. Still there are much more to learn in drupal theme development and module development.

I came from the past experience of drupal module development and I found most of them quite related from file naming conventions to internal theme parser and module parsers. Drupal make use of the design patterns called “decorator pattern” for module and theme development, which primarily, focuses on the changing of output without altering the base codes. So, I find it very useful to study the drupal’s internal core architecture to understand drupal thoroughly.

Summary : I have skipped many things here to make this article short. If you think you need to have details on some points or You think I might have missed the important part please let me know. Thanks for your valuable time.

Currently the theme developed is not mobile friendly and has many design flaws. I have to re-work on it and optimize it, make it mobile friendly. I have skipped those for the next session. When I have more time I will play with responsive designs with this “basic” theme and have guidance from my Sr. Drupal Theme Developer colleague and have his words on my drupal theme development skills. If you have any questions please let them coming.

hook_menu path not working in Drupal (6,7)

Right From Drupal website :

This hook enables modules to register paths in order to define how URL requests are handled. Paths may be registered for URL handling only, or they can register a link to be placed in a menu (usually the Navigation menu). A path and its associated information is commonly called a “menu router item”. This hook is rarely called (for example, when modules are enabled), and its results are cached in the database.

Suppose we defined a custom a wildcard menu path as shown below

  $items['my-module/%mymodule_abc/edit'] = array(
    'page callback' => 'mymodule_abc_edit',
    'page arguments' => array(1),

When path ‘my-module/123/edit’ is requested, your load function mymodule_abc_load() will be invoked with the argument ‘123’, and should load and return an “abc” object with internal id 123:

  function mymodule_abc_load($abc_id) {
    return db_query("SELECT * FROM {mymodule_abc} WHERE abc_id = :abc_id", array(':abc_id' => $abc_id))->fetchObject();

This ‘abc’ object will then be passed into the callback functions defined for the menu item, such as the page callback function mymodule_abc_edit() to replace the integer 1 in the argument array.

For more information please visit actual source for this article
Drupal hook_menu article

[Solved] How to use hook_entity_view in drupal7 ?

I work in a team of developers and we separately develop modules in drupal. We are assigned to create entities, and there was time once I need to alter the view which was output of some other entity module. So, I am sharing here my experience how I did that.

Lets say we have two custom entity module
1. product module – creates a product entity
2. customer module – creates a customer entity

I need to alter the output of product entity module from customer module entity. In Drupal6 it was quite easy using node_api and then adding our own content in node->content, once we verified the correct content type. In Drupal7 we do it using the hook_entity_view.

To use hook_entity_view the product module should implement the single line of code highlighted in line 17. More information on hook_entity_view

function product_page_view( $product, $view_mode='full' ) {
// Remove previously built content, if exists.
  $product->content = array();

  if ($view_mode == 'teaser') {
    $product->content['title_en'] = array(
        '#markup' => filter_xss($product->title_en),
        '#weight' => -5,

// Build fields content.
  field_attach_prepare_view(PRODUCT_ENTITY, array($product->pid => $product), $view_mode);

  entity_prepare_view(PRODUCT_ENTITY, array($product->pid => $product));

  module_invoke_all('entity_view', $product, 'product', 'full', $GLOBALS['language_content']->language);

  $product->content += field_attach_view(PRODUCT_ENTITY, $product, $view_mode);

   * Specify the theme to use and set the #element. Note that the
   * key you use to pass the entity object must match the key you
   * set in in the variables in entity_theme().
  $product->content += array(
      '#theme' => 'product',
      '#element' => $product,
      '#view_mode' => 'full',
      '#language' => NULL,

  return $product->content;

After implementating that code in product entity module. There is one more step. We now finally need to use hook_entity_view hook in customer module, which is similar to using other hooks in drupal. i.e. modulename_entity_view().

Below code is roughly a random sample code, that I picked up and altered the variables to depict product and customer relationships.

we might be using the same hook to alter the output of more than one entity module so, I am checking here the $type variable and then depending on the cases, I have used my output logic.

Here I have simply added my own content to the output of product entity module. This is done by the code from line 13-18.

function customer_entity_view( $entity, $type, $view_mode, $langcode ) {
  $t = 0;
  switch ($type) {
    case 'product':
      $productStatus = entity_get_controller(PRODUCT_ENTITY)->getProductStatus($product->pid);

      if ($productStatus == 'yes') {
        $output = drupal_get_form('customer_product_page_form', $product->pid);
        $body = drupal_render($output);

	// Alter the output of product entity module
	// Add your customer output to the output of product entity module
        $entity->content['product'] = array(
            '#prefix' => '<div id="customer_product_info">', // used by javascripts to populate sliders
            '#suffix' => '</div>',
            '#markup' => $body,
            '#weight' => -10,

If there is anything that you don’t understand. Please mention it in the comments. I will try to make them clear as far as possible.

[Solved] Add module to admin/config page in Drupal7

We are going to create a following custom block in admin/config page.
Drupal admin/config block - Configure Products

To create a block in admin/config like those ‘People, Content Authoring, Media’ etc. We need to define two menu items as shown below

function products_menu() {
$items = array();
$items['admin/config/products'] = array(
      'title' => 'Configure Products',
      'description' => 'Allows administrators to configure product items',
      'weight' => -30,
      'page callback' => 'drupal_get_form',
      'page arguments' => array('products_admin_settings_form'),
      'access arguments' => array('administer products'),
      'file' => 'products.admin.inc',

  $items['admin/config/products/manage'] = array(
      'title' => 'Manage Products',
      'description' => 'Allows admins to manage products',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('products_admin_settings_form'),
      'access arguments' => array('administer products'),
      'file' => 'products.admin.inc',
      'weight' => -10,

  return $items;

admin/config/products – highlighted above line no. 3

Creates a new block with title Configure Products

admin/config/products/manage – highlighted above line no. 13

This menu item is visible under the Configure Products as clickable item. When this link “Manage Products” is clicked, it invokes the ‘products_admin_settings_form’ page callback which eventually displays a form for products configuration.

If there is any confusions please post them on comment.