Create a theme for Drupal. Part 2

    imagePart 1

    Preprocesses


    Preprocesses are needed to determine which variables will be available in the templates. We will add preprocesses to sites / all / themes / mytheme / preprocess . Create preprocess-page.inc files for the page, preprocess-node.inc for content, preprocess-header.inc for the header, preprocess-footer.inc for the footer, preprocess-region.inc for regions, preprocess-block .inc - for blocks, preprocess-comment.inc - for comments. We can also create files of type preprocess-node-story.inc to process material like story or preprocess-block-user.incfor the user block. This allows you to process separately different types of site content. Basically, in the preprocesses, I defined classes for different regions of the site.

    preprocess-page.inc


    Copy Source | Copy HTML
    1. if ($index = array_search(preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'. drupal_strtolower(arg( 0))), $vars['classes_array'])) {
    2.     unset($vars['classes_array'][$index]);
    3. }
    4.  
    5. // Определяем шаблон и переменные для шапки и подвала
    6. $vars['header'] = theme('header', array(
    7.     // Имя переменной => значение
    8.     'front_page' => $vars['front_page'],
    9.     'logo' => $vars['logo'],
    10.     'search_box' => $vars['search_box'],
    11.     'header_line' => $vars['header_line'],
    12. ));
    13. $vars['footer'] = theme('footer', array(
    14.     'front_page' => $vars['front_page'],
    15.     'footer_line' => $vars['footer_line'],
    16.     'page_bottom' => $vars['page_bottom'],
    17. ));
    18.  
    19. // Определяем дополнительные классы для не_главной страницы
    20. if (!$vars['is_front']) {
    21.     // Добавляем уникальные классы для разных страниц
    22.     $path = drupal_get_path_alias($_GET['q']);
    23.     $vars['classes_array'][] = drupal_html_class('page-' . $path);
    24.     // И для разных разделов сайта
    25.     list($section, ) = explode('/', $path, 2);
    26.     if (arg( 0) == 'node') {
    27.         if (arg(1) == 'add') {
    28.             $section = 'node-add';
    29.         }
    30.         elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
    31.             $section = 'node-' . arg(2);
    32.         }
    33.     }
    34.     $vars['classes_array'][] = drupal_html_class('section-' . $section);
    35. }


    preprocess-node.inc


    Copy Source | Copy HTML
    1. // Создаем переменную $build_mode в зависимости от типа отображения ноды
    2. switch ($vars['node']->build_mode) {
    3.     case NODE_BUILD_NORMAL:
    4.         $vars['build_mode'] = $vars['teaser'] ? 'teaser' : 'full';
    5.         break;
    6.     case NODE_BUILD_PREVIEW:
    7.         $vars['build_mode'] = 'preview';
    8.         break;
    9.     case NODE_BUILD_SEARCH_INDEX:
    10.         $vars['build_mode'] = 'search_index';
    11.         break;
    12.     case NODE_BUILD_SEARCH_RESULT:
    13.         $vars['build_mode'] = 'search_result';
    14.         break;
    15.     case NODE_BUILD_RSS:
    16.         $vars['build_mode'] = 'rss';
    17.         break;
    18.     case NODE_BUILD_PRINT:
    19.         $vars['build_mode'] = 'print';
    20.         break;
    21. }
    22.  
    23. // Создаем переменную $user_picture
    24. $vars['user_picture'] = $vars['picture'];
    25.  
    26. // Специальные классы для материалов, зависящие от типа
    27. // Например: "node-type-page", "node-type-story", "node-type-my-custom-type"
    28. $vars['classes_array'][] = drupal_html_class('node-type-' . $vars['type']);
    29. if ($vars['promote']) {
    30.     $vars['classes_array'][] = 'node-promoted';
    31. }
    32. if ($vars['sticky']) {
    33.     $vars['classes_array'][] = 'node-sticky';
    34. }
    35. if (!$vars['status']) {
    36.     $vars['classes_array'][] = 'node-unpublished';
    37.     $vars['unpublished'] = TRUE;
    38. }
    39. else {
    40.     $vars['unpublished'] = FALSE;
    41. }
    42. if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid) {
    43.     $vars['classes_array'][] = 'node-by-viewer'; // Нода текущего пользователя.
    44. }
    45. if ($vars['teaser']) {
    46.     $vars['classes_array'][] = 'node-teaser'; // Тизер.
    47. }
    48. if (isset($vars['preview'])) {
    49.     $vars['classes_array'][] = 'node-preview';
    50. }


    preprocess-header.inc, preprocess-footer.inc


    Copy Source | Copy HTML
    1. if ($vars['elements']) {
    2.     // Разбираем массив, пришедший из функции theme() и добавляем параметры в переменные
    3.     foreach ($vars['elements'] as $ind => $val) {
    4.         $vars[$ind] = $val;
    5.     }
    6.     unset ($vars['elements']);
    7. }


    preprocess-region.inc


    Copy Source | Copy HTML
    1. // Создаем переменные $content и $region
    2. $vars['content'] = $vars['elements']['#children'];
    3. $vars['region'] = $vars['elements']['#region'];
    4.  
    5. // Задаем классы
    6. $region = 'region-' . str_replace('_', '-', $vars['region']);
    7. $vars['classes_array'] = array('region', $region);
    8.  
    9. // И шаблон
    10. $vars['template_files'][] = $region;


    preprocess-block.inc


    Copy Source | Copy HTML
    1. $block = $vars['block'];
    2.  
    3. // Задаем переменные $title и $content
    4. $vars['content'] = $block->content;
    5. $vars['title'] = $block->subject;
    6.  
    7. // Задаем классы
    8. $vars['classes_array'][] = 'block-' . $block->module;
    9. $vars['classes_array'][] = 'region-' . $vars['block_zebra'];
    10. $vars['classes_array'][] = $vars['zebra'];
    11. $vars['classes_array'][] = 'region-count-' . $vars['block_id'];
    12. $vars['classes_array'][] = 'count-' . $vars['id'];
    13.  
    14. $vars['edit_links_array'] = array();


    preprocess-comment.inc


    Copy Source | Copy HTML
    1. // Создаем переменную $created
    2. $vars['created'] = $vars['date'];
    3.  
    4. // Убираем тему коммента если она откллючена
    5. if (variable_get('comment_subject_field_' . $vars['node']->type, 1) ==  0) {
    6.     $vars['title'] = '';
    7. }
    8.  
    9. // Стили
    10. if ($vars['comment']->new) {
    11.     $vars['classes_array'][] = 'comment-new';
    12. }
    13. if ($vars['comment']->status == COMMENT_NOT_PUBLISHED) {
    14.     $vars['unpublished'] = TRUE;
    15. }
    16. else {
    17.     $vars['unpublished'] = FALSE;
    18.     $vars['classes_array'][] = $vars['status'];
    19. }
    20. // Зебра
    21. if ($vars['id'] == 1) {
    22.     $vars['classes_array'][] = 'first';
    23. }
    24. if ($vars['id'] == $vars['node']->comment_count) {
    25.     $vars['classes_array'][] = 'last';
    26. }
    27. $vars['classes_array'][] = $vars['zebra'];
    28. if ($vars['comment']->uid ==  0) {
    29.     // Коммент анонимного пользователя
    30.     $vars['classes_array'][] = 'comment-by-anonymous';
    31. }
    32. else {
    33.     if ($vars['comment']->uid == $vars['node']->uid) {
    34.         // Коммент автора ноды
    35.         $vars['classes_array'][] = 'comment-by-node-author';
    36.     }
    37.     if ($vars['comment']->uid == $GLOBALS['user']->uid) {
    38.         // Коммент текущего пользователя
    39.         $vars['classes_array'][] = 'comment-by-viewer';
    40.     }
    41. }


    Part 3

    Also popular now: