Tag Archives: Drupal7

Introduction to drush for drupal development

What is drush ?

Drush is very popular and useful tool for drupal developers. It provides us shell access to the drupal commands and functions which otherwise had to be accessed using the menus provided in the drupal administration. It helps us to enable/disable modules, installs  dependency modules automatically, manage users, permissions etc. For complete feature of drush browse this page.

In short drush is just like other command line tools :) Sweet.

Drush official page is http://drupal.org/project/drush

drush was initially created for linux/mac environments however it can be run on windows as well.

Drush can be installed in linux,mac and windows. But runs faster in linux, at least I felt so personally.

For Ubuntu
Type the following command

$ sudo apt-get install drush

when asked enter your computers password and hit enter

For Other Linux Distributions
Download the source from drush source extract it and then follow the readme.txt.

Actually you download the shell script for the drush and make it executable for the current user by adding the path to drush in environment variable so that it’s always available for the execution. To use Drush from the command line, you’ll need a CLI-mode capable PHP binary version 5.2 or greater.

For Windows
1. Visit http://drush.ws/drush_windows_installer
2. Download Drush 5.8 Windows Installer
3. Follow the Installation Guide

How to verify I you have installed drush successfully or not ?
– execute command ‘drush version’ at command prompt, it should show you output something like “drush verison 5.8”. It means you have successfully configured drush.

How to use drush ?
– cd into the root folder of your drupal project. For eg. my drupal project is in /media/BACKUP/htdocs/news/
– then I use “cd /media/BACKUP/htdocs/news”

Then to clear the cache I use “drush cc all” and response is

‘all’ cache was cleared in /media/BACKUP/htdocs/news#default                           [success]

You can try with other commands like “drush en module-name”

Hope this gives your short introduction on drush usage for drupal development. If you want to know more information about this then drop comments. Thanks.

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

<?php
  $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:

<?php
  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,
        );
      }
      break;
    default:break;
  }
}

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.