欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

史平忠的 模板建议hook_preprocess_page 例子 预处理函数

shiping1 的头像

下面是drupal6的例子
//-- Delete this line if you want to use this function
function shipingzhong_preprocess_page(&$variables, $hook) {
  //$vars['sample_variable'] = t('Lorem ipsum.');

  // To remove a class from $classes_array, use array_diff().
  //$vars['classes_array'] = array_diff($vars['classes_array'], array('class-to-remove'));
   //var_dump(array_keys($vars));
   if(arg(0)=='taxonomy' && arg(1)=='term')
   {
         if(is_numeric(arg(2)))
         {
             
               $termObj = _getTermObject(arg(2));
//       var_dump($termObj);exit;
               //var_dump($termObj);exit;
               //这些是分类单页面  termflag为2
                 //if (in_array(arg(2),array(23,24,25,19,20,21,22,31,7,6)))
//                 var_dump($termObj);exit;

                 if($termObj->termflag=='2')
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     $variables['template_files'][] = 'page-taxonomy-article';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                 }
                 //这些是分类的图片列表页和视频的列表面 termflag为3或4
                 if($termObj->termflag=='3' || $termObj->termflag=='4' )
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     $variables['template_files'][] = 'page-taxonomy-shiping';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                 }             
                 //父栏目 下面有子栏目的栏目
                 if($termObj->termflag=='5')
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     $variables['template_files'][] = 'page-taxonomy-shiping';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                 }
                 //普通文章列表 跟 图片 视频一样 使用同一个模板
                 if($termObj->termflag=='1')
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     $variables['template_files'][] = 'page-taxonomy-list-artilce';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                 }           
                 else //否则的话 使用默认
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的                   
                     //$variables['template_files'][] = 'page-taxonomy-list-artilce';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';                   
                 }         
         }    
   }
   if(arg(0)=='node' && is_numeric(arg(1)))
   {
      $nodeObj =  node_load(arg(1));
      //普通文章
       if($nodeObj->type=='page'){
                $variables['template_files'][] = 'page-node-article';
       }
       else if ($nodeObj->type=='tuping'){
               $variables['template_files'][] = 'page-node-tuping';
           
       }
       else if ($nodeObj->type=='shiping'){
               $variables['template_files'][] = 'page-node-shiping';
           
       }      
   }
}























//-- Delete this line if you want to use this function
function shipingzhong_preprocess_page(&$variables, $hook) {
  //$vars['sample_variable'] = t('Lorem ipsum.');

  // To remove a class from $classes_array, use array_diff().
  //$vars['classes_array'] = array_diff($vars['classes_array'], array('class-to-remove'));
   //var_dump(array_keys($vars));
   if(arg(0)=='taxonomy' && arg(1)=='term')
   {
         if(is_numeric(arg(2)))
         {
             
               $termObj = _getTermObject(arg(2));
//       var_dump($termObj);exit;
               //var_dump($termObj);exit;
               //这些是分类单页面  termflag为2
                 //if (in_array(arg(2),array(23,24,25,19,20,21,22,31,7,6)))
//                 var_dump($termObj);exit;

                 if($termObj->termflag=='2')
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     $variables['template_files'][] = 'page-taxonomy-article';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                 }
                 //这些是分类的图片列表页和视频的列表面 termflag为3或4
                 if($termObj->termflag=='3' || $termObj->termflag=='4' )
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     $variables['template_files'][] = 'page-taxonomy-shiping';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                 }
                 
                 //父栏目 下面有子栏目的栏目
                 if($termObj->termflag=='5')
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     $variables['template_files'][] = 'page-taxonomy-shiping';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                 }
                 //普通文章列表 跟 图片 视频一样 使用同一个模板
                 if($termObj->termflag=='1')
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     $variables['template_files'][] = 'page-taxonomy-list-artilce';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                 }
                 
                 else //否则的话 使用默认
                 {
                     //d7这里的名字是是中短行  文件名 page_taxonomy_article.tpl.php
                     //d6这里是一模一样的
                     
                     //$variables['template_files'][] = 'page-taxonomy-list-artilce';
                     //$vars['theme_hook_suggestions'][] = 'page__taxonomy__term__country';
                     
                 }
                 
                 
                 
                 
                 
         }    
   }
   
   
   if(arg(0)=='node' && is_numeric(arg(1)))
   {
      $nodeObj =  node_load(arg(1));
      //普通文章
       if($nodeObj->type=='page'){
                $variables['template_files'][] = 'page-node-article';
       }
       else if ($nodeObj->type=='tuping'){
               $variables['template_files'][] = 'page-node-tuping';
           
       }
       else if ($nodeObj->type=='shiping'){
               $variables['template_files'][] = 'page-node-shiping';
           
       }       
       
       
   }

}

 

 

 

 

//修改模板文件
//默认的 再加上  page-nodetype.tpl.php 模板文件
function mygarland_preprocess_page(&$vars) {
    //复制它以前的开始
    // Add favicon
    if (theme_get_setting('toggle_favicon')) {
        drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />');
    }

    global $theme;
    // Populate all block regions.
    $regions = system_region_list($theme);
    // Load all region content assigned via blocks.
    foreach (array_keys($regions) as $region) {
        // Prevent left and right regions from rendering blocks when 'show_blocks' == FALSE.
        if (!(!$variables['show_blocks'] && ($region == 'left' || $region == 'right'))) {
            $blocks = theme('blocks', $region);
        }
        else {
            $blocks = '';
        }
        // Assign region to a region variable.
        isset($variables[$region]) ? $variables[$region] .= $blocks : $variables[$region] = $blocks;
    }

    // Set up layout variable.
    $variables['layout'] = 'none';
    if (!empty($variables['left'])) {
        $variables['layout'] = 'left';
    }
    if (!empty($variables['right'])) {
        $variables['layout'] = ($variables['layout'] == 'left') ? 'both' : 'right';
    }

    // Set mission when viewing the frontpage.
    if (drupal_is_front_page()) {
        $mission = filter_xss_admin(theme_get_setting('mission'));
    }

    // Construct page title
    if (drupal_get_title()) {
        $head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'Drupal'));
    }
    else {
        $head_title = array(variable_get('site_name', 'Drupal'));
        if (variable_get('site_slogan', '')) {
            $head_title[] = variable_get('site_slogan', '');
        }
    }
    $variables['head_title']        = implode(' | ', $head_title);
    $variables['base_path']         = base_path();
    $variables['front_page']        = url();
    $variables['breadcrumb']        = theme('breadcrumb', drupal_get_breadcrumb());
    $variables['feed_icons']        = drupal_get_feeds();
    $variables['footer_message']    = filter_xss_admin(variable_get('site_footer', FALSE));
    $variables['head']              = drupal_get_html_head();
    $variables['help']              = theme('help');
    $variables['language']          = $GLOBALS['language'];
    $variables['language']->dir     = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
    $variables['logo']              = theme_get_setting('logo');
    $variables['messages']          = $variables['show_messages'] ? theme('status_messages') : '';
    $variables['mission']           = isset($mission) ? $mission : '';
    $variables['primary_links']     = theme_get_setting('toggle_primary_links') ? menu_primary_links() : array();
    $variables['secondary_links']   = theme_get_setting('toggle_secondary_links') ? menu_secondary_links() : array();
    $variables['search_box']        = (theme_get_setting('toggle_search') ? drupal_get_form('search_theme_form') : '');
    $variables['site_name']         = (theme_get_setting('toggle_name') ? filter_xss_admin(variable_get('site_name', 'Drupal')) : '');
    $variables['site_slogan']       = (theme_get_setting('toggle_slogan') ? filter_xss_admin(variable_get('site_slogan', '')) : '');
    $variables['css']               = drupal_add_css();
    $variables['styles']            = drupal_get_css();
    $variables['scripts']           = drupal_get_js();
    $variables['tabs']              = theme('menu_local_tasks');
    $variables['title']             = drupal_get_title();
    // Closure should be filled last.
    $variables['closure']           = theme('closure');

    if ($node = menu_get_object()) {
        $variables['node'] = $node;
    }

    // Compile a list of classes that are going to be applied to the body element.
    // This allows advanced theming based on context (home page, node of certain type, etc.).
    $body_classes = array();
    // Add a class that tells us whether we're on the front page or not.
    $body_classes[] = $variables['is_front'] ? 'front' : 'not-front';
    // Add a class that tells us whether the page is viewed by an authenticated user or not.
    $body_classes[] = $variables['logged_in'] ? 'logged-in' : 'not-logged-in';
    // Add arg(0) to make it possible to theme the page depending on the current page
    // type (e.g. node, admin, user, etc.). To avoid illegal characters in the class,
    // we're removing everything disallowed. We are not using 'a-z' as that might leave
    // in certain international characters (e.g. German umlauts).
    $body_classes[] = preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'. form_clean_id(drupal_strtolower(arg(0))));
    // If on an individual node page, add the node type.
    if (isset($variables['node']) && $variables['node']->type) {
        $body_classes[] = 'node-type-'. form_clean_id($variables['node']->type);
    }
    // Add information about the number of sidebars.
    if ($variables['layout'] == 'both') {
        $body_classes[] = 'two-sidebars';
    }
    elseif ($variables['layout'] == 'none') {
        $body_classes[] = 'no-sidebars';
    }
    else {
        $body_classes[] = 'one-sidebar sidebar-'. $variables['layout'];
    }
    // Implode with spaces.
    $variables['body_classes'] = implode(' ', $body_classes);
    //复制它以前的结束


    if($node = menu_get_object()) {
        $vars['node'] = $node;
        $suggestions = array();
        $template_filename = 'page';
        $template_filename = $template_filename . '-' . $vars['node']->type;
        $i = 0;
        $suggestion = 'page';
        while ($arg = arg($i++)) {
            $arg = str_replace(array("/", "\\", "\0"), '', $arg);
            $suggestions[] = $suggestion .'-'. $arg;
            if (!is_numeric($arg)) {
                $suggestion .= '-'. $arg;
            }
        }
        //这里的模板文件 page-nodetype.tpl.php 模板文件
        //优先级比  page-node-num.tpl.php 高
        $suggestions[] = $template_filename;
        if (drupal_is_front_page()) {
            $suggestions[] = 'page-front';
        }
        if ($suggestions) {
            $vars['template_files'] = $suggestions;
        }
    }
}
 

 

 

 

//修改模板文件
//默认的 再加上  page-nodetype.tpl.php 模板文件
function mygarland_preprocess_page(&$vars) {
    //复制它以前的开始
    // Add favicon
    if (theme_get_setting('toggle_favicon')) {
        drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />');
    }

    global $theme;
    // Populate all block regions.
    $regions = system_region_list($theme);
    // Load all region content assigned via blocks.
    foreach (array_keys($regions) as $region) {
        // Prevent left and right regions from rendering blocks when 'show_blocks' == FALSE.
        if (!(!$variables['show_blocks'] && ($region == 'left' || $region == 'right'))) {
            $blocks = theme('blocks', $region);
        }
        else {
            $blocks = '';
        }
        // Assign region to a region variable.
        isset($variables[$region]) ? $variables[$region] .= $blocks : $variables[$region] = $blocks;
    }

    // Set up layout variable.
    $variables['layout'] = 'none';
    if (!empty($variables['left'])) {
        $variables['layout'] = 'left';
    }
    if (!empty($variables['right'])) {
        $variables['layout'] = ($variables['layout'] == 'left') ? 'both' : 'right';
    }

    // Set mission when viewing the frontpage.
    if (drupal_is_front_page()) {
        $mission = filter_xss_admin(theme_get_setting('mission'));
    }

    // Construct page title
    if (drupal_get_title()) {
        $head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'Drupal'));
    }
    else {
        $head_title = array(variable_get('site_name', 'Drupal'));
        if (variable_get('site_slogan', '')) {
            $head_title[] = variable_get('site_slogan', '');
        }
    }
    $variables['head_title']        = implode(' | ', $head_title);
    $variables['base_path']         = base_path();
    $variables['front_page']        = url();
    $variables['breadcrumb']        = theme('breadcrumb', drupal_get_breadcrumb());
    $variables['feed_icons']        = drupal_get_feeds();
    $variables['footer_message']    = filter_xss_admin(variable_get('site_footer', FALSE));
    $variables['head']              = drupal_get_html_head();
    $variables['help']              = theme('help');
    $variables['language']          = $GLOBALS['language'];
    $variables['language']->dir     = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
    $variables['logo']              = theme_get_setting('logo');
    $variables['messages']          = $variables['show_messages'] ? theme('status_messages') : '';
    $variables['mission']           = isset($mission) ? $mission : '';
    $variables['primary_links']     = theme_get_setting('toggle_primary_links') ? menu_primary_links() : array();
    $variables['secondary_links']   = theme_get_setting('toggle_secondary_links') ? menu_secondary_links() : array();
    $variables['search_box']        = (theme_get_setting('toggle_search') ? drupal_get_form('search_theme_form') : '');
    $variables['site_name']         = (theme_get_setting('toggle_name') ? filter_xss_admin(variable_get('site_name', 'Drupal')) : '');
    $variables['site_slogan']       = (theme_get_setting('toggle_slogan') ? filter_xss_admin(variable_get('site_slogan', '')) : '');
    $variables['css']               = drupal_add_css();
    $variables['styles']            = drupal_get_css();
    $variables['scripts']           = drupal_get_js();
    $variables['tabs']              = theme('menu_local_tasks');
    $variables['title']             = drupal_get_title();
    // Closure should be filled last.
    $variables['closure']           = theme('closure');

    if ($node = menu_get_object()) {
        $variables['node'] = $node;
    }

    // Compile a list of classes that are going to be applied to the body element.
    // This allows advanced theming based on context (home page, node of certain type, etc.).
    $body_classes = array();
    // Add a class that tells us whether we're on the front page or not.
    $body_classes[] = $variables['is_front'] ? 'front' : 'not-front';
    // Add a class that tells us whether the page is viewed by an authenticated user or not.
    $body_classes[] = $variables['logged_in'] ? 'logged-in' : 'not-logged-in';
    // Add arg(0) to make it possible to theme the page depending on the current page
    // type (e.g. node, admin, user, etc.). To avoid illegal characters in the class,
    // we're removing everything disallowed. We are not using 'a-z' as that might leave
    // in certain international characters (e.g. German umlauts).
    $body_classes[] = preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'. form_clean_id(drupal_strtolower(arg(0))));
    // If on an individual node page, add the node type.
    if (isset($variables['node']) && $variables['node']->type) {
        $body_classes[] = 'node-type-'. form_clean_id($variables['node']->type);
    }
    // Add information about the number of sidebars.
    if ($variables['layout'] == 'both') {
        $body_classes[] = 'two-sidebars';
    }
    elseif ($variables['layout'] == 'none') {
        $body_classes[] = 'no-sidebars';
    }
    else {
        $body_classes[] = 'one-sidebar sidebar-'. $variables['layout'];
    }
    // Implode with spaces.
    $variables['body_classes'] = implode(' ', $body_classes);
    //复制它以前的结束


    if($node = menu_get_object()) {
        $vars['node'] = $node;
        $suggestions = array();
        $template_filename = 'page';
        $template_filename = $template_filename . '-' . $vars['node']->type;
        $i = 0;
        $suggestion = 'page';
        while ($arg = arg($i++)) {
            $arg = str_replace(array("/", "\\", "\0"), '', $arg);
            $suggestions[] = $suggestion .'-'. $arg;
            if (!is_numeric($arg)) {
                $suggestion .= '-'. $arg;
            }
        }
        //这里的模板文件 page-nodetype.tpl.php 模板文件
        //优先级比  page-node-num.tpl.php 高
        $suggestions[] = $template_filename;
        if (drupal_is_front_page()) {
            $suggestions[] = 'page-front';
        }
        if ($suggestions) {
            $vars['template_files'] = $suggestions;
        }
    }
}
 

普通分类: