Zastąp html.tpl.php według typu węzła


17

W moim pliku template.php dla mojej kompozycji próbowałem:

function media_preprocess_page(&$vars, $hook) {
  if (isset($vars['node'])) 
  {
      // If the node type is "blog" the template suggestion will be "html--blog.tpl.php".
       $vars['theme_hook_suggestions'][] = 'html__'.$vars['node']->type;

      // If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
       $vars['theme_hook_suggestions'][] = 'page__'.$vars['node']->type;

      // If the node id is "33" the template suggestion will be "page--33.tpl.php".
       $vars['theme_hook_suggestions'][] = 'page__'.$vars['node']->nid;    
  }

    //Create page suggestion for first part of url-alias
    $url_alias = drupal_get_path_alias($_GET['q']);
    $parts = explode('/', $url_alias);

    $vars['theme_hook_suggestions'][] = 'page__'.$parts[0].'__alias';  
}

Działa to dla strony - nodetype.tpl.php, ale nie dla html - nodetype.tpl.php

Być może pytasz, dlaczego musisz zastąpić szablon html.tpl.php dla każdego typu węzła. Jest tak, ponieważ istnieją znaczniki, których nie chcę uwzględniać dla tego konkretnego węzła.

Odpowiedzi:


28

Nazwa funkcji wstępnego przetwarzania zależy od przetwarzanego motywu / szablonu. Aby wstępnie przetworzyć plik html.tpl.php, musisz użyć hook_preprocess_html():

function media_preprocess_html(&$vars) {
  $node = menu_get_object();

  if ($node && $node->nid) {
    $vars['theme_hook_suggestions'][] = 'html__' . $node->type;
  }
}

3

Podejście @Clive jest bardzo inteligentne.

Pamiętaj również, że w pliku html.tpl.php możesz przeczytać typ zawartości, z którą masz do czynienia $variables['classes'], co da ci coś w rodzajuhtml not-front not-logged-in no-sidebars page-node page-node- page-node-5638 node-type-CONTENT-TYPE-NAME

Dzięki temu możesz zmienić sposób, w jaki zachowuje się plik html.tpl.php:

if (strpos($variables['classes'],'node-type-YOUR-CONTENT-TYPE') == true ) {
  echo 'Do something special  for YOUR-CONTENT-TYPE ';
}
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.