Tuesday, September 27, 2011

Preprocess in Drupal with Example

Process type : moduleName_preprocess 

Description:
Do not confuse this with the preprocessor before it. This allows modules that did not originally implement the hook to influence the variables. Applies to all hooks.

Note:  After you code completion, You have to do the clear cache from admin panel. Then only the dynamic variables get scope.

Examples:


  /* Example #1: Just creating a dynamic variable for theming. */
  $vars['preprocess_example1'] = "working mano.. working!";

Displaying this variable in template file:
echo $preprocess_example1;

  /* Example #2: Loading a Block content and assigning into a dynamic variable for theming. */
  $blocks = module_invoke('block', 'block', 'view', 1);
  $blockContent = "";
  if ($blocks['content'] != "n/a"){
    $blockContent = $blocks['content'];
  }
  $vars['preprocess_example2'] = $blockContent;

Displaying this variable in template file:
echo $preprocess_example2;

Process type : moduleName_preprocess_page


Note:  This dynamic variable only displayed in page.tpl.php, page-front.tpl.php, page-custom-menu.tpl.php pages only.  This variable will not shown in our AJAX related separate template files.  Becaz that file only points to that template file.


function common_preprocess_page(&$vars) {
  /* Example #3: Just creating a dynamic variable for theming. */
  $vars['preprocess_example3'] = "working in page templates.. working!";
}

Displaying this variable in template file:
echo $preprocess_example3;

Process type : phptemplate_preprocess

function phptemplate_preprocess(&$vars) {
 /* Example #4: Just creating a dynamic variable for theming. */
  $vars['phptemplate_example4'] = "testing content global for php template example!";
}


Displaying this variable in template file:
echo $phptemplate_example4;

Process type : phptemplate_preprocess_page

function phptemplate_preprocess_page(&$vars) {
 /* Example #5: Just creating a dynamic variable for theming. */
  $vars['phptemplate_example5'] = "testing content for specific php template example!";
}


Displaying this variable in template file:
echo $phptemplate_example5;

Process type : themename_preprocess

function danland_preprocess(&$vars) {
 /* Example #6: Just creating a dynamic variable for theming. */
  $vars['themetemplate_example6'] = "testing content for global php theme template !";
}


Displaying this variable in template file:
echo $themetemplate_example6;

Process type : themename_preprocess_page

function danland_preprocess_page(&$vars) {
 /* Example #7: Just creating a dynamic variable for theming. */
  $vars['themetemplate_example7'] = "testing content for specific php theme template !";
}


Displaying this variable in template file:
echo $themetemplate_example7;

No comments:

Post a Comment

Followers