Aller au contenu principal
loading

Fonctions de pré-process pour Drupal

POSTÉ DANS Drupal 6 TAGS Drupal 6 AUTEUR herve COMMENTAIRES 5

Preprocess Fil d'Ariane

/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return a string containing the breadcrumb output.
 */
function phptemplate_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
    return '<div class="breadcrumb">'. implode(' › ', $breadcrumb) .'</div>';
  }
}

Preprocess Page

Fonction de pré-process basé sur le nom et la langue de la page

function phptemplate_preprocess_page(&$vars) {
  global $language;
  if (module_exists('path')) {
    $path_alias = drupal_get_path_alias($_GET['q']);
    $alias_parts = explode('/', $path_alias);
    $last = array_reverse($alias_parts);
    $last_part = $last[0];
    if ($last_part != "edit") {
      $templates = array();
      $template_name = "page";
      foreach ($alias_parts as $part) {
        $template_name = $template_name . '-' . $part;
        $template_name_lang = $template_name . '-' . $language->language;
        $templates[] = $template_name;
        $templates[] = $template_name_lang;
      }
      $vars['template_files'] = $templates;
    }
  }
  //$vars = createPageTemplateFile($vars,1);
}

 

Fonction de pré-process basé sur les arguments de l'URL

function createPageTemplateFile($vars,$index=1){
    $urlParams = explode('/',$_SERVER['REQUEST_URI']);
    $i = 1;
    $args = '';
    while ($i<=$index && $i<=sizeof($urlParams)){
        $args .= urlencode($urlParams[$i]);
        $vars['template_files'][] = "page-".$args;
        $args .= "-";
        $i++;
    }
    return $vars;
}

Preprocess Node

Fonction de préprocess basé sur le type du node

function phptemplate_preprocess_node(&$vars, $hook) {
  $node = $vars['node'];
  $vars['template_file'] = 'node-'. $node->type;
}

Preprocess Block

Fonction de pré-process basé sur la région, le module et le delta du block

function phptemplate_preprocess_block(&$variables) {
  static $block_counter = array();
  // All blocks get an independent counter for each region.
  if (!isset($block_counter[$variables['block']->region])) {
    $block_counter[$variables['block']->region] = 1;
  }
  // Same with zebra striping.
  $variables['block_zebra'] = ($block_counter[$variables['block']->region] % 2) ? 'odd' : 'even';
  $variables['block_id'] = $block_counter[$variables['block']->region]++;
  $variables['template_files'][] = 'block-'. $variables['block']->region;
  $variables['template_files'][] = 'block-'. $variables['block']->module;
  $variables['template_files'][] = 'block-'. $variables['block']->module .'-'. $variables['block']->delta;
}

 



5 commentaire