template.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. require_once dirname(__FILE__) . '/includes/alpha.inc';
  3. require_once dirname(__FILE__) . '/includes/base.inc';
  4. /**
  5. * Implements hook_theme().
  6. */
  7. function alpha_theme($existing, $type, $theme, $path) {
  8. return array(
  9. 'section' => array(
  10. 'template' => 'section',
  11. 'path' => $path . '/templates',
  12. 'render element' => 'elements',
  13. 'pattern' => 'section__',
  14. 'preprocess functions' => array(
  15. 'template_preprocess',
  16. 'template_preprocess_section',
  17. 'alpha_preprocess',
  18. 'alpha_preprocess_section',
  19. ),
  20. 'process functions' => array(
  21. 'template_process',
  22. 'template_process_section',
  23. 'alpha_process',
  24. 'alpha_process_section'
  25. ),
  26. ),
  27. 'zone' => array(
  28. 'template' => 'zone',
  29. 'path' => $path . '/templates',
  30. 'render element' => 'elements',
  31. 'pattern' => 'zone__',
  32. 'preprocess functions' => array(
  33. 'template_preprocess',
  34. 'template_preprocess_zone',
  35. 'alpha_preprocess',
  36. 'alpha_preprocess_zone',
  37. ),
  38. 'process functions' => array(
  39. 'template_process',
  40. 'template_process_zone',
  41. 'alpha_process',
  42. 'alpha_process_zone'
  43. ),
  44. ),
  45. );
  46. }
  47. /**
  48. * Implements hook_preprocess().
  49. */
  50. function alpha_preprocess(&$vars, $hook) {
  51. $vars['attributes_array']['class'] = $vars['classes_array'];
  52. alpha_invoke('preprocess', $hook, $vars);
  53. }
  54. /**
  55. * Implements hook_process().
  56. */
  57. function alpha_process(&$vars, $hook) {
  58. if (!empty($vars['elements']['#grid']) || !empty($vars['elements']['#data']['wrapper_css'])) {
  59. if (!empty($vars['elements']['#grid'])) {
  60. foreach (array('prefix', 'suffix', 'push', 'pull') as $quality) {
  61. if (!empty($vars['elements']['#grid'][$quality])) {
  62. array_unshift($vars['attributes_array']['class'], $quality . '-' . $vars['elements']['#grid'][$quality]);
  63. }
  64. }
  65. array_unshift($vars['attributes_array']['class'], 'grid-' . $vars['elements']['#grid']['columns']);
  66. }
  67. if (!empty($vars['elements']['#data']['wrapper_css'])) {
  68. foreach (array_map('drupal_html_class', explode(' ', $vars['elements']['#data']['wrapper_css'])) as $class) {
  69. $vars['attributes_array']['class'][] = $class;
  70. }
  71. }
  72. $vars['attributes'] = $vars['attributes_array'] ? drupal_attributes($vars['attributes_array']) : '';
  73. }
  74. if (!empty($vars['elements']['#grid_container']) || !empty($vars['elements']['#data']['css'])) {
  75. if (!empty($vars['elements']['#data']['css'])) {
  76. foreach (array_map('drupal_html_class', explode(' ', $vars['elements']['#data']['css'])) as $class) {
  77. $vars['content_attributes_array']['class'][] = $class;
  78. }
  79. }
  80. if (!empty($vars['elements']['#grid_container'])) {
  81. $vars['content_attributes_array']['class'][] = 'container-' . $vars['elements']['#grid_container'];
  82. }
  83. $vars['content_attributes'] = $vars['content_attributes_array'] ? drupal_attributes($vars['content_attributes_array']) : '';
  84. }
  85. alpha_invoke('process', $hook, $vars);
  86. }
  87. /**
  88. * Implements hook_element_info_alter().
  89. */
  90. function alpha_element_info_alter(&$elements) {
  91. if (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')) {
  92. array_unshift($elements['styles']['#pre_render'], 'alpha_css_preprocessor');
  93. }
  94. }
  95. /**
  96. * Implements hook_css_alter().
  97. */
  98. function alpha_css_alter(&$css) {
  99. global $language;
  100. $theme = alpha_get_theme();
  101. if ($theme->settings['exclude']) {
  102. if ($exclude = array_filter($theme->settings['exclude'])) {
  103. if ($language->direction == LANGUAGE_LTR) {
  104. foreach ($exclude as $basename) {
  105. $rtl = str_replace('.css', '-rtl.css', $basename);
  106. $exclude[$rtl] = $rtl;
  107. }
  108. }
  109. foreach($css as $key => $item) {
  110. if (isset($exclude[$key])) {
  111. unset($css[$key]);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * Implements hook_page_alter().
  119. */
  120. function alpha_page_alter(&$vars) {
  121. $theme = alpha_get_theme();
  122. $theme->settings['debug']['access'] = alpha_debug_access($GLOBALS['user'], $theme->settings['debug']['roles']);
  123. // If no module has taken care of the main content, add it to the page now.
  124. // This allows the site to still be usable even if no modules that
  125. // control page regions (for example, the Block module) are enabled.
  126. if (!drupal_static('system_main_content_added', FALSE)) {
  127. $vars['content']['system_main'] = drupal_set_page_content();
  128. }
  129. if (($theme->settings['debug']['access'] || $GLOBALS['user']->uid == 1) && ($theme->settings['debug']['grid'] || $theme->settings['debug']['block'])) {
  130. drupal_add_css(drupal_get_path('theme', 'alpha') . '/css/alpha-debug.css', array('group' => CSS_THEME, 'weight' => -5));
  131. drupal_add_js(drupal_get_path('theme', 'alpha') . '/js/alpha-debug.js', array('group' => JS_THEME, 'weight' => -5));
  132. if ($theme->settings['responsive']) {
  133. $vars['page_bottom']['alpha_resize_indicator'] = array(
  134. '#type' => 'markup',
  135. '#markup' => '<div class="alpha-resize-indicator"></div>',
  136. );
  137. }
  138. if ($theme->settings['debug']['grid']) {
  139. $vars['page_bottom']['alpha_grid_toggle'] = array(
  140. '#type' => 'markup',
  141. '#markup' => '<a class="alpha-grid-toggle" href="#"></a>',
  142. );
  143. }
  144. if ($theme->settings['debug']['block']) {
  145. $vars['page_bottom']['alpha_block_toggle'] = array(
  146. '#type' => 'markup',
  147. '#markup' => '<a class="alpha-block-toggle" href="#"></a>',
  148. );
  149. foreach ($theme->regions as $region => $item) {
  150. if ($item['enabled']) {
  151. if (empty($vars[$region])) {
  152. $vars[$region]['#region'] = $region;
  153. $vars[$region]['#theme_wrappers'] = array('region');
  154. }
  155. if (isset($vars[$region]['#theme_wrappers']) && array_search('region', $vars[$region]['#theme_wrappers']) !== FALSE) {
  156. $vars[$region] = array('alpha_debug_' . $region => array(
  157. '#type' => 'markup',
  158. '#markup' => '<div class="alpha-debug-block"><h2>' . $item['name'] . '</h2><p>' . t('This is a debugging block') . '</p></div>',
  159. '#weight' => -999,
  160. )) + $vars[$region];
  161. }
  162. }
  163. }
  164. }
  165. }
  166. if (!module_implements('alpha_page_structure_alter')) {
  167. alpha_alter('alpha_page_structure', $vars, $theme->theme);
  168. }
  169. else {
  170. drupal_alter('alpha_page_structure', $vars, $theme->theme);
  171. }
  172. }
  173. /**
  174. * Implements hook_alpha_page_alter().
  175. */
  176. function alpha_alpha_page_structure_alter(&$vars) {
  177. $theme = alpha_get_theme();
  178. $temporary = array();
  179. foreach ($theme->regions as $region => $item) {
  180. if ($item['enabled'] && $theme->zones[$item['zone']]['enabled'] && ($item['force'] || !empty($vars[$region]))) {
  181. $temporary[$item['section']][$item['zone']][$region] = !empty($vars[$region]) ? $vars[$region] : array();
  182. $temporary[$item['section']][$item['zone']][$region]['#weight'] = (int) $item['weight'];
  183. $temporary[$item['section']][$item['zone']][$region]['#position'] = $item['position'];
  184. $temporary[$item['section']][$item['zone']][$region]['#data'] = $item;
  185. $temporary[$item['section']][$item['zone']][$region]['#grid'] = array(
  186. 'prefix' => $item['prefix'],
  187. 'suffix' => $item['suffix'],
  188. 'push' => $item['push'],
  189. 'pull' => $item['pull'],
  190. 'columns' => $item['columns'],
  191. );
  192. $theme->regions[$region]['grid'] = &$temporary[$item['section']][$item['zone']][$region]['#grid'];
  193. if (empty($vars[$region])) {
  194. $temporary[$item['section']][$item['zone']][$region]['#region'] = $region;
  195. $temporary[$item['section']][$item['zone']][$region]['#theme_wrappers'] = array('region');
  196. }
  197. }
  198. else if (!empty($vars[$region])) {
  199. $vars['#excluded'][$region] = !empty($vars[$region]) ? $vars[$region] : array();
  200. $vars['#excluded'][$region]['#weight'] = (int) $item['weight'];
  201. $vars['#excluded'][$region]['#data'] = $item;
  202. $vars['#excluded'][$region]['#grid'] = array(
  203. 'prefix' => $item['prefix'],
  204. 'suffix' => $item['suffix'],
  205. 'push' => $item['push'],
  206. 'pull' => $item['pull'],
  207. 'columns' => $item['columns'],
  208. );
  209. }
  210. unset($vars[$region]);
  211. }
  212. foreach ($theme->zones as $zone => $item) {
  213. if ($item['enabled'] && ($item['force'] || !empty($temporary[$item['section']][$zone]))) {
  214. if (isset($item['primary']) && isset($temporary[$item['section']][$zone][$item['primary']])) {
  215. alpha_calculate_primary($temporary[$item['section']][$zone], $item['primary'], $item['columns']);
  216. }
  217. if ($item['order']) {
  218. alpha_calculate_position($temporary[$item['section']][$zone]);
  219. }
  220. $temporary[$item['section']][$zone]['#theme_wrappers'] = array('zone');
  221. $temporary[$item['section']][$zone]['#zone'] = $zone;
  222. $temporary[$item['section']][$zone]['#weight'] = (int) $item['weight'];
  223. $temporary[$item['section']][$zone]['#data'] = $item;
  224. $temporary[$item['section']][$zone]['#grid_container'] = $item['columns'];
  225. }
  226. }
  227. foreach ($theme->sections as $section => $item) {
  228. if (isset($temporary[$section])) {
  229. $temporary[$section]['#theme_wrappers'] = array('section');
  230. $temporary[$section]['#section'] = $section;
  231. }
  232. }
  233. $vars = array_merge($vars, $temporary);
  234. }
  235. /**
  236. * Implements hook_preprocess_section().
  237. */
  238. function template_preprocess_section(&$vars) {
  239. $vars['theme_hook_suggestions'][] = 'section__' . $vars['elements']['#section'];
  240. $vars['section'] = $vars['elements']['#section'];
  241. $vars['content'] = $vars['elements']['#children'];
  242. $vars['attributes_array']['id'] = drupal_html_id('section-' . $vars['section']);
  243. $vars['classes_array'] = array('section', $vars['attributes_array']['id']);
  244. }
  245. /**
  246. * Implements hook_preprocess_zone().
  247. */
  248. function template_preprocess_zone(&$vars) {
  249. $vars['theme_hook_suggestions'] = array('zone__' . $vars['elements']['#zone']);
  250. $vars['zone'] = $vars['elements']['#zone'];
  251. $vars['content'] = $vars['elements']['#children'];
  252. $vars['wrapper'] = $vars['elements']['#data']['wrapper'];
  253. $vars['columns'] = $vars['elements']['#data']['columns'];
  254. $vars['content_attributes_array']['id'] = drupal_html_id('zone-' . $vars['zone']);
  255. $vars['content_attributes_array']['class'] = array('zone', $vars['content_attributes_array']['id'], 'clearfix');
  256. if ($vars['wrapper']) {
  257. $vars['attributes_array']['id'] = drupal_html_id($vars['content_attributes_array']['id'] . '-wrapper');
  258. $vars['classes_array'] = array('zone-wrapper', $vars['attributes_array']['id'], 'clearfix');
  259. }
  260. alpha_grid_include($vars['columns']);
  261. }
  262. /**
  263. * Implements hook_preprocess_block().
  264. */
  265. function alpha_alpha_preprocess_block(&$vars) {
  266. $vars['content_attributes_array']['class'][] = 'content';
  267. $vars['content_attributes_array']['class'][] = 'clearfix';
  268. $vars['attributes_array']['id'] = $vars['block_html_id'];
  269. $vars['attributes_array']['class'][] = drupal_html_class('block-' . $vars['block']->delta);
  270. $vars['attributes_array']['class'][] = $vars['block_html_id'];
  271. }
  272. /**
  273. * Implements hook_preprocess_html().
  274. */
  275. function alpha_alpha_preprocess_html(&$vars) {
  276. $theme = alpha_get_theme();
  277. foreach (array('two-sidebars', 'one-sidebar sidebar-first', 'one-sidebar sidebar-second', 'no-sidebars') as $exclude) {
  278. if ($index = array_search($exclude, $vars['attributes_array']['class'])) {
  279. unset($vars['attributes_array']['class'][$index]);
  280. }
  281. }
  282. // Add a CSS class based on the current page context.
  283. if (!drupal_is_front_page()) {
  284. $context = explode('/', drupal_get_path_alias());
  285. $context = reset($context);
  286. if (!empty($context)) {
  287. $vars['attributes_array']['class'][] = drupal_html_class('context-' . $context);
  288. }
  289. }
  290. if (($theme->settings['debug']['grid'] || $theme->settings['debug']['block']) && $theme->settings['debug']['access']) {
  291. if ($theme->settings['debug']['grid'] && $theme->settings['debug']['grid_active']) {
  292. $vars['attributes_array']['class'][] = 'alpha-grid-debug';
  293. }
  294. if ($theme->settings['debug']['block'] && $theme->settings['debug']['block_active']) {
  295. $vars['attributes_array']['class'][] = 'alpha-region-debug';
  296. }
  297. }
  298. if($theme->settings['responsive'] && $theme->settings['viewport']['enabled']) {
  299. $meta = array(
  300. '#tag' => 'meta',
  301. '#attributes' => array(
  302. 'name' => 'viewport',
  303. 'content' => 'width=device-width, initial-scale=' . $theme->settings['viewport']['initial'] . ', maximum-scale=' . $theme->settings['viewport']['max'] . ', minimum-scale=' . $theme->settings['viewport']['min'] . ', user-scalable=' . ($theme->settings['viewport']['user'] ? 'yes' : 'no'),
  304. ),
  305. );
  306. drupal_add_html_head($meta, 'alpha-viewport');
  307. }
  308. alpha_css_include();
  309. alpha_libraries_include();
  310. }
  311. /**
  312. * Implements hook_preprocess_page().
  313. */
  314. function alpha_alpha_preprocess_page(&$vars) {
  315. $theme = alpha_get_theme();
  316. $theme->page = &$vars;
  317. $vars['feed_icons'] = $theme->settings['toggle']['feed_icons'] ? $vars['feed_icons'] : NULL;
  318. $vars['tabs'] = $theme->settings['toggle']['tabs'] ? $vars['tabs'] : NULL;
  319. $vars['action_links'] = $theme->settings['toggle']['action_links'] ? $vars['action_links'] : NULL;
  320. $vars['show_messages'] = $theme->settings['toggle']['messages'] ? $vars['show_messages'] : FALSE;
  321. $vars['site_name_hidden'] = $theme->settings['hidden']['site_name'];
  322. $vars['site_slogan_hidden'] = $theme->settings['hidden']['site_slogan'];
  323. $vars['title_hidden'] = $theme->settings['hidden']['title'];
  324. $vars['attributes_array']['id'] = 'page';
  325. $vars['attributes_array']['class'][] = 'clearfix';
  326. }
  327. /**
  328. * Implements hook_preprocess_region().
  329. */
  330. function alpha_alpha_preprocess_region(&$vars) {
  331. $vars['attributes_array']['id'] = drupal_html_id('region-' . $vars['region']);
  332. $vars['content_attributes_array']['class'][] = 'region-inner';
  333. $vars['content_attributes_array']['class'][] = $vars['attributes_array']['id'] . '-inner';
  334. }
  335. /**
  336. * Implements hook_process_page().
  337. */
  338. function alpha_alpha_process_page(&$vars) {
  339. $theme = alpha_get_theme();
  340. $vars['title'] = $theme->settings['toggle']['page_title'] ? $vars['title'] : NULL;
  341. $vars['breadcrumb'] = $theme->settings['toggle']['breadcrumb'] ? $vars['breadcrumb'] : NULL;
  342. }