theme.inc 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. <?php
  2. /**
  3. * @file
  4. * Preprocessors and helper functions to make theming easier.
  5. */
  6. /**
  7. * Provide a full array of possible themes to try for a given hook.
  8. *
  9. * @param $hook
  10. * The hook to use. This is the base theme/template name.
  11. * @param $view
  12. * The view being rendered.
  13. * @param $display
  14. * The display being rendered, if applicable.
  15. */
  16. function _views_theme_functions($hook, $view, $display = NULL) {
  17. $themes = array();
  18. if ($display) {
  19. $themes[] = $hook . '__' . $view->name . '__' . $display->id;
  20. $themes[] = $hook . '__' . $display->id;
  21. $themes[] = $hook . '__' . preg_replace('/[^a-z0-9]/', '_', strtolower($view->tag));
  22. // Add theme suggestions foreach single tag.
  23. foreach (drupal_explode_tags($view->tag) as $tag) {
  24. $themes[] = $hook . '__' . preg_replace('/[^a-z0-9]/', '_', strtolower($tag));
  25. }
  26. if ($display->id != $display->display_plugin) {
  27. $themes[] = $hook . '__' . $view->name . '__' . $display->display_plugin;
  28. $themes[] = $hook . '__' . $display->display_plugin;
  29. }
  30. }
  31. $themes[] = $hook . '__' . $view->name;
  32. $themes[] = $hook;
  33. return $themes;
  34. }
  35. /**
  36. * Preprocess the primary theme implementation for a view.
  37. */
  38. function template_preprocess_views_view(&$vars) {
  39. global $base_path;
  40. $view = $vars['view'];
  41. $vars['rows'] = (!empty($view->result) || $view->style_plugin->even_empty()) ? $view->style_plugin->render($view->result) : '';
  42. $vars['css_name'] = drupal_clean_css_identifier($view->name);
  43. $vars['name'] = $view->name;
  44. $vars['display_id'] = $view->current_display;
  45. // Basic classes
  46. $vars['css_class'] = '';
  47. $vars['classes_array'] = array();
  48. $vars['classes_array'][] = 'view';
  49. $vars['classes_array'][] = 'view-' . drupal_clean_css_identifier($vars['name']);
  50. $vars['classes_array'][] = 'view-id-' . $vars['name'];
  51. $vars['classes_array'][] = 'view-display-id-' . $vars['display_id'];
  52. $css_class = $view->display_handler->get_option('css_class');
  53. if (!empty($css_class)) {
  54. $vars['css_class'] = preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class);
  55. $vars['classes_array'][] = $vars['css_class'];
  56. }
  57. $empty = empty($vars['rows']);
  58. $vars['header'] = $view->display_handler->render_area('header', $empty);
  59. $vars['footer'] = $view->display_handler->render_area('footer', $empty);
  60. if ($empty) {
  61. $vars['empty'] = $view->display_handler->render_area('empty', $empty);
  62. }
  63. $vars['exposed'] = !empty($view->exposed_widgets) ? $view->exposed_widgets : '';
  64. $vars['more'] = $view->display_handler->render_more_link();
  65. $vars['feed_icon'] = !empty($view->feed_icon) ? $view->feed_icon : '';
  66. $vars['pager'] = '';
  67. // @todo: Figure out whether this belongs into views_ui_preprocess_views_view.
  68. // Render title for the admin preview.
  69. $vars['title'] = !empty($view->views_ui_context) ? filter_xss_admin($view->get_title()) : '';
  70. if ($view->display_handler->render_pager()) {
  71. $exposed_input = $view->get_exposed_input();
  72. $vars['pager'] = $view->query->render_pager($exposed_input);
  73. }
  74. $vars['attachment_before'] = !empty($view->attachment_before) ? $view->attachment_before : '';
  75. $vars['attachment_after'] = !empty($view->attachment_after) ? $view->attachment_after : '';
  76. // Add contextual links to the view. We need to attach them to the dummy
  77. // $view_array variable, since contextual_preprocess() requires that they be
  78. // attached to an array (not an object) in order to process them. For our
  79. // purposes, it doesn't matter what we attach them to, since once they are
  80. // processed by contextual_preprocess() they will appear in the $title_suffix
  81. // variable (which we will then render in views-view.tpl.php).
  82. views_add_contextual_links($vars['view_array'], 'view', $view, $view->current_display);
  83. // Attachments are always updated with the outer view, never by themselves,
  84. // so they do not have dom ids.
  85. if (empty($view->is_attachment)) {
  86. // Our JavaScript needs to have some means to find the HTML belonging to this
  87. // view.
  88. //
  89. // It is true that the DIV wrapper has classes denoting the name of the view
  90. // and its display ID, but this is not enough to unequivocally match a view
  91. // with its HTML, because one view may appear several times on the page. So
  92. // we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
  93. // each view. This identifier is written to both Drupal.settings and the DIV
  94. // wrapper.
  95. $vars['dom_id'] = $view->dom_id;
  96. $vars['classes_array'][] = 'view-dom-id-' . $vars['dom_id'];
  97. }
  98. // If using AJAX, send identifying data about this view.
  99. if ($view->use_ajax && empty($view->is_attachment) && empty($view->live_preview)) {
  100. $settings = array(
  101. 'views' => array(
  102. 'ajax_path' => url('views/ajax'),
  103. 'ajaxViews' => array(
  104. 'views_dom_id:' . $vars['dom_id'] => array(
  105. 'view_name' => $view->name,
  106. 'view_display_id' => $view->current_display,
  107. 'view_args' => check_plain(implode('/', $view->args)),
  108. 'view_path' => check_plain($_GET['q']),
  109. // Pass through URL to ensure we get e.g. language prefixes.
  110. // 'view_base_path' => isset($view->display['page']) ? substr(url($view->display['page']->display_options['path']), strlen($base_path)) : '',
  111. 'view_base_path' => $view->get_path(),
  112. 'view_dom_id' => $vars['dom_id'],
  113. // To fit multiple views on a page, the programmer may have
  114. // overridden the display's pager_element.
  115. 'pager_element' => isset($view->query->pager) ? $view->query->pager->get_pager_id() : 0,
  116. ),
  117. ),
  118. ),
  119. // Support for AJAX path validation in core 7.39.
  120. 'urlIsAjaxTrusted' => array(
  121. url('views/ajax') => TRUE,
  122. ),
  123. );
  124. drupal_add_js($settings, 'setting');
  125. views_add_js('ajax_view');
  126. }
  127. // If form fields were found in the View, reformat the View output as a form.
  128. if (views_view_has_form_elements($view)) {
  129. $output = !empty($vars['rows']) ? $vars['rows'] : $vars['empty'];
  130. $form = drupal_get_form(views_form_id($view), $view, $output);
  131. // The form is requesting that all non-essential views elements be hidden,
  132. // usually because the rendered step is not a view result.
  133. if ($form['show_view_elements']['#value'] == FALSE) {
  134. $vars['header'] = '';
  135. $vars['exposed'] = '';
  136. $vars['pager'] = '';
  137. $vars['footer'] = '';
  138. $vars['more'] = '';
  139. $vars['feed_icon'] = '';
  140. }
  141. $vars['rows'] = $form;
  142. }
  143. }
  144. /**
  145. * Process function to render certain elements into the view.
  146. */
  147. function template_process_views_view(&$vars) {
  148. if (is_array($vars['rows'])) {
  149. $vars['rows'] = drupal_render($vars['rows']);
  150. }
  151. // Flatten the classes to a string for the template file.
  152. $vars['classes'] = implode(' ', $vars['classes_array']);
  153. }
  154. /**
  155. * Preprocess theme function to print a single record from a row, with fields
  156. */
  157. function template_preprocess_views_view_fields(&$vars) {
  158. $view = $vars['view'];
  159. // Loop through the fields for this view.
  160. $previous_inline = FALSE;
  161. $vars['fields'] = array(); // ensure it's at least an empty array.
  162. foreach ($view->field as $id => $field) {
  163. // render this even if set to exclude so it can be used elsewhere.
  164. $field_output = $view->style_plugin->get_field($view->row_index, $id);
  165. $empty = $field->is_value_empty($field_output, $field->options['empty_zero']);
  166. if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($vars['options']['hide_empty'])))) {
  167. $object = new stdClass();
  168. $object->handler = &$view->field[$id];
  169. $object->inline = !empty($vars['options']['inline'][$id]);
  170. $object->element_type = $object->handler->element_type(TRUE, !$vars['options']['default_field_elements'], $object->inline);
  171. if ($object->element_type) {
  172. $class = '';
  173. if ($object->handler->options['element_default_classes']) {
  174. $class = 'field-content';
  175. }
  176. if ($classes = $object->handler->element_classes($view->row_index)) {
  177. if ($class) {
  178. $class .= ' ';
  179. }
  180. $class .= $classes;
  181. }
  182. $pre = '<' . $object->element_type;
  183. if ($class) {
  184. $pre .= ' class="' . $class . '"';
  185. }
  186. $field_output = $pre . '>' . $field_output . '</' . $object->element_type . '>';
  187. }
  188. // Protect ourself somewhat for backward compatibility. This will prevent
  189. // old templates from producing invalid HTML when no element type is selected.
  190. if (empty($object->element_type)) {
  191. $object->element_type = 'span';
  192. }
  193. $object->content = $field_output;
  194. if (isset($view->field[$id]->field_alias) && isset($vars['row']->{$view->field[$id]->field_alias})) {
  195. $object->raw = $vars['row']->{$view->field[$id]->field_alias};
  196. }
  197. else {
  198. $object->raw = NULL; // make sure it exists to reduce NOTICE
  199. }
  200. if (!empty($vars['options']['separator']) && $previous_inline && $object->inline && $object->content) {
  201. $object->separator = filter_xss_admin($vars['options']['separator']);
  202. }
  203. $object->class = drupal_clean_css_identifier($id);
  204. $previous_inline = $object->inline;
  205. $object->inline_html = $object->handler->element_wrapper_type(TRUE, TRUE);
  206. if ($object->inline_html === '' && $vars['options']['default_field_elements']) {
  207. $object->inline_html = $object->inline ? 'span' : 'div';
  208. }
  209. // Set up the wrapper HTML.
  210. $object->wrapper_prefix = '';
  211. $object->wrapper_suffix = '';
  212. if ($object->inline_html) {
  213. $class = '';
  214. if ($object->handler->options['element_default_classes']) {
  215. $class = "views-field views-field-" . $object->class;
  216. }
  217. if ($classes = $object->handler->element_wrapper_classes($view->row_index)) {
  218. if ($class) {
  219. $class .= ' ';
  220. }
  221. $class .= $classes;
  222. }
  223. $object->wrapper_prefix = '<' . $object->inline_html;
  224. if ($class) {
  225. $object->wrapper_prefix .= ' class="' . $class . '"';
  226. }
  227. $object->wrapper_prefix .= '>';
  228. $object->wrapper_suffix = '</' . $object->inline_html . '>';
  229. }
  230. // Set up the label for the value and the HTML to make it easier
  231. // on the template.
  232. $object->label = check_plain($view->field[$id]->label());
  233. $object->label_html = '';
  234. if ($object->label) {
  235. $object->label_html .= $object->label;
  236. if ($object->handler->options['element_label_colon']) {
  237. $object->label_html .= ': ';
  238. }
  239. $object->element_label_type = $object->handler->element_label_type(TRUE, !$vars['options']['default_field_elements']);
  240. if ($object->element_label_type) {
  241. $class = '';
  242. if ($object->handler->options['element_default_classes']) {
  243. $class = 'views-label views-label-' . $object->class;
  244. }
  245. $element_label_class = $object->handler->element_label_classes($view->row_index);
  246. if ($element_label_class) {
  247. if ($class) {
  248. $class .= ' ';
  249. }
  250. $class .= $element_label_class;
  251. }
  252. $pre = '<' . $object->element_label_type;
  253. if ($class) {
  254. $pre .= ' class="' . $class . '"';
  255. }
  256. $pre .= '>';
  257. $object->label_html = $pre . $object->label_html . '</' . $object->element_label_type . '>';
  258. }
  259. }
  260. $vars['fields'][$id] = $object;
  261. }
  262. }
  263. }
  264. /**
  265. * Display a single views grouping.
  266. */
  267. function theme_views_view_grouping($vars) {
  268. $view = $vars['view'];
  269. $title = $vars['title'];
  270. $content = $vars['content'];
  271. $output = '<div class="view-grouping">';
  272. $output .= '<div class="view-grouping-header">' . $title . '</div>';
  273. $output .= '<div class="view-grouping-content">' . $content . '</div>' ;
  274. $output .= '</div>';
  275. return $output;
  276. }
  277. /**
  278. * Process a single grouping within a view.
  279. */
  280. function template_preprocess_views_view_grouping(&$vars) {
  281. $vars['content'] = $vars['view']->style_plugin->render_grouping_sets($vars['rows'], $vars['grouping_level']);
  282. }
  283. /**
  284. * Display a single views field.
  285. *
  286. * Interesting bits of info:
  287. * $field->field_alias says what the raw value in $row will be. Reach it like
  288. * this: @code { $row->{$field->field_alias} @endcode
  289. */
  290. function theme_views_view_field($vars) {
  291. $view = $vars['view'];
  292. $field = $vars['field'];
  293. $row = $vars['row'];
  294. return $vars['output'];
  295. }
  296. /**
  297. * Process a single field within a view.
  298. *
  299. * This preprocess function isn't normally run, as a function is used by
  300. * default, for performance. However, by creating a template, this
  301. * preprocess should get picked up.
  302. */
  303. function template_preprocess_views_view_field(&$vars) {
  304. $vars['output'] = $vars['field']->advanced_render($vars['row']);
  305. }
  306. /**
  307. * Preprocess theme function to print a single record from a row, with fields
  308. */
  309. function template_preprocess_views_view_summary(&$vars) {
  310. $view = $vars['view'];
  311. $argument = $view->argument[$view->build_info['summary_level']];
  312. $vars['row_classes'] = array();
  313. $url_options = array();
  314. if (!empty($view->exposed_raw_input)) {
  315. $url_options['query'] = $view->exposed_raw_input;
  316. }
  317. $active_urls = drupal_map_assoc(array(
  318. url($_GET['q'], array('alias' => TRUE)), // force system path
  319. url($_GET['q']), // could be an alias
  320. ));
  321. // Collect all arguments foreach row, to be able to alter them for example by the validator.
  322. // This is not done per single argument value, because this could cause performance problems.
  323. $row_args = array();
  324. foreach ($vars['rows'] as $id => $row) {
  325. $row_args[$id] = $argument->summary_argument($row);
  326. }
  327. $argument->process_summary_arguments($row_args);
  328. foreach ($vars['rows'] as $id => $row) {
  329. $vars['row_classes'][$id] = '';
  330. $vars['rows'][$id]->link = $argument->summary_name($row);
  331. $args = $view->args;
  332. $args[$argument->position] = $row_args[$id];
  333. $base_path = NULL;
  334. if (!empty($argument->options['summary_options']['base_path'])) {
  335. $base_path = $argument->options['summary_options']['base_path'];
  336. }
  337. $vars['rows'][$id]->url = url($view->get_url($args, $base_path), $url_options);
  338. $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
  339. if (isset($active_urls[$vars['rows'][$id]->url])) {
  340. $vars['row_classes'][$id] = 'active';
  341. }
  342. }
  343. }
  344. /**
  345. * Template preprocess theme function to print summary basically
  346. * unformatted.
  347. */
  348. function template_preprocess_views_view_summary_unformatted(&$vars) {
  349. $view = $vars['view'];
  350. $argument = $view->argument[$view->build_info['summary_level']];
  351. $vars['row_classes'] = array();
  352. $url_options = array();
  353. if (!empty($view->exposed_raw_input)) {
  354. $url_options['query'] = $view->exposed_raw_input;
  355. }
  356. $count = 0;
  357. $active_urls = drupal_map_assoc(array(
  358. url($_GET['q'], array('alias' => TRUE)), // force system path
  359. url($_GET['q']), // could be an alias
  360. ));
  361. // Collect all arguments foreach row, to be able to alter them for example by the validator.
  362. // This is not done per single argument value, because this could cause performance problems.
  363. $row_args = array();
  364. foreach ($vars['rows'] as $id => $row) {
  365. $row_args[$id] = $argument->summary_argument($row);
  366. }
  367. $argument->process_summary_arguments($row_args);
  368. foreach ($vars['rows'] as $id => $row) {
  369. // only false on first time:
  370. if ($count++) {
  371. $vars['rows'][$id]->separator = filter_xss_admin($vars['options']['separator']);
  372. }
  373. $vars['rows'][$id]->link = $argument->summary_name($row);
  374. $args = $view->args;
  375. $args[$argument->position] = $row_args[$id];
  376. $base_path = NULL;
  377. if (!empty($argument->options['summary_options']['base_path'])) {
  378. $base_path = $argument->options['summary_options']['base_path'];
  379. }
  380. $vars['rows'][$id]->url = url($view->get_url($args, $base_path), $url_options);
  381. $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
  382. if (isset($active_urls[$vars['rows'][$id]->url])) {
  383. $vars['row_classes'][$id] = 'active';
  384. }
  385. }
  386. }
  387. /**
  388. * Display a view as a table style.
  389. */
  390. function template_preprocess_views_view_table(&$vars) {
  391. $view = $vars['view'];
  392. // We need the raw data for this grouping, which is passed in as $vars['rows'].
  393. // However, the template also needs to use for the rendered fields. We
  394. // therefore swap the raw data out to a new variable and reset $vars['rows']
  395. // so that it can get rebuilt.
  396. // Store rows so that they may be used by further preprocess functions.
  397. $result = $vars['result'] = $vars['rows'];
  398. $vars['rows'] = array();
  399. $vars['field_classes'] = array();
  400. $vars['header'] = array();
  401. $options = $view->style_plugin->options;
  402. $handler = $view->style_plugin;
  403. $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
  404. $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
  405. $fields = &$view->field;
  406. $columns = $handler->sanitize_columns($options['columns'], $fields);
  407. $active = !empty($handler->active) ? $handler->active : '';
  408. $order = !empty($handler->order) ? $handler->order : 'asc';
  409. $query = tablesort_get_query_parameters();
  410. if (isset($view->exposed_raw_input)) {
  411. $query += $view->exposed_raw_input;
  412. }
  413. // Fields must be rendered in order as of Views 2.3, so we will pre-render
  414. // everything.
  415. $renders = $handler->render_fields($result);
  416. foreach ($columns as $field => $column) {
  417. // Create a second variable so we can easily find what fields we have and what the
  418. // CSS classes should be.
  419. $vars['fields'][$field] = drupal_clean_css_identifier($field);
  420. if ($active == $field) {
  421. $vars['fields'][$field] .= ' active';
  422. }
  423. // render the header labels
  424. if ($field == $column && empty($fields[$field]->options['exclude'])) {
  425. $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
  426. if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
  427. $vars['header'][$field] = $label;
  428. }
  429. else {
  430. $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
  431. if ($active == $field) {
  432. $initial = ($order == 'asc') ? 'desc' : 'asc';
  433. }
  434. $title = t('sort by @s', array('@s' => $label));
  435. if ($active == $field) {
  436. $label .= theme('tablesort_indicator', array('style' => $initial));
  437. }
  438. $query['order'] = $field;
  439. $query['sort'] = $initial;
  440. $link_options = array(
  441. 'html' => TRUE,
  442. 'attributes' => array('title' => $title),
  443. 'query' => $query,
  444. );
  445. $vars['header'][$field] = l($label, $_GET['q'], $link_options);
  446. }
  447. $vars['header_classes'][$field] = '';
  448. // Set up the header label class.
  449. if ($fields[$field]->options['element_default_classes']) {
  450. $vars['header_classes'][$field] .= "views-field views-field-" . $vars['fields'][$field];
  451. }
  452. $class = $fields[$field]->element_label_classes(0);
  453. if ($class) {
  454. if ($vars['header_classes'][$field]) {
  455. $vars['header_classes'][$field] .= ' ';
  456. }
  457. $vars['header_classes'][$field] .= $class;
  458. }
  459. // Add a CSS align class to each field if one was set
  460. if (!empty($options['info'][$field]['align'])) {
  461. $vars['header_classes'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
  462. }
  463. // Add a header label wrapper if one was selected.
  464. if ($vars['header'][$field]) {
  465. $element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
  466. if ($element_label_type) {
  467. $vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
  468. }
  469. }
  470. }
  471. // Add a CSS align class to each field if one was set
  472. if (!empty($options['info'][$field]['align'])) {
  473. $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
  474. }
  475. // Render each field into its appropriate column.
  476. foreach ($result as $num => $row) {
  477. // Add field classes
  478. $vars['field_classes'][$field][$num] = '';
  479. if ($fields[$field]->options['element_default_classes']) {
  480. $vars['field_classes'][$field][$num] = "views-field views-field-" . $vars['fields'][$field];
  481. }
  482. if ($classes = $fields[$field]->element_classes($num)) {
  483. if ($vars['field_classes'][$field][$num]) {
  484. $vars['field_classes'][$field][$num] .= ' ';
  485. }
  486. $vars['field_classes'][$field][$num] .= $classes;
  487. }
  488. $vars['field_attributes'][$field][$num] = array();
  489. if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
  490. $field_output = $renders[$num][$field];
  491. $element_type = $fields[$field]->element_type(TRUE, TRUE);
  492. if ($element_type) {
  493. $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
  494. }
  495. // Don't bother with separators and stuff if the field does not show up.
  496. if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
  497. continue;
  498. }
  499. // Place the field into the column, along with an optional separator.
  500. if (!empty($vars['rows'][$num][$column])) {
  501. if (!empty($options['info'][$column]['separator'])) {
  502. $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
  503. }
  504. }
  505. else {
  506. $vars['rows'][$num][$column] = '';
  507. }
  508. $vars['rows'][$num][$column] .= $field_output;
  509. }
  510. }
  511. // Remove columns if the option is hide empty column is checked and the field is not empty.
  512. if (!empty($options['info'][$field]['empty_column'])) {
  513. $empty = TRUE;
  514. foreach ($vars['rows'] as $num => $columns) {
  515. $empty &= empty($columns[$column]);
  516. }
  517. if ($empty) {
  518. foreach ($vars['rows'] as $num => &$column_items) {
  519. unset($column_items[$column]);
  520. unset($vars['header'][$column]);
  521. }
  522. }
  523. }
  524. }
  525. // Hide table header if all labels are empty.
  526. if (!array_filter($vars['header'])) {
  527. $vars['header'] = array();
  528. }
  529. $count = 0;
  530. foreach ($vars['rows'] as $num => $row) {
  531. $vars['row_classes'][$num] = array();
  532. if ($row_class_special) {
  533. $vars['row_classes'][$num][] = ($count++ % 2 == 0) ? 'odd' : 'even';
  534. }
  535. if ($row_class = $handler->get_row_class($num)) {
  536. $vars['row_classes'][$num][] = $row_class;
  537. }
  538. }
  539. if ($row_class_special) {
  540. $vars['row_classes'][0][] = 'views-row-first';
  541. $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
  542. }
  543. $vars['classes_array'] = array('views-table');
  544. if (empty($vars['rows']) && !empty($options['empty_table'])) {
  545. $vars['rows'][0][0] = $view->display_handler->render_area('empty');
  546. // Calculate the amounts of rows with output.
  547. $vars['field_attributes'][0][0]['colspan'] = count($vars['header']);
  548. $vars['field_classes'][0][0] = 'views-empty';
  549. }
  550. if (!empty($options['sticky'])) {
  551. drupal_add_js('misc/tableheader.js');
  552. $vars['classes_array'][] = "sticky-enabled";
  553. }
  554. $vars['classes_array'][] = 'cols-'. count($vars['header']);
  555. // Add the summary to the list if set.
  556. if (!empty($handler->options['summary'])) {
  557. $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
  558. }
  559. // Add the caption to the list if set.
  560. if (!empty($handler->options['caption'])) {
  561. $vars['caption'] = filter_xss_admin($handler->options['caption']);
  562. }
  563. else {
  564. $vars['caption'] = '';
  565. }
  566. }
  567. /**
  568. * Display a view as a grid style.
  569. */
  570. function template_preprocess_views_view_grid(&$vars) {
  571. $view = $vars['view'];
  572. $result = $view->result;
  573. $options = $view->style_plugin->options;
  574. $handler = $view->style_plugin;
  575. $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
  576. $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
  577. $columns = $options['columns'];
  578. $rows = array();
  579. $row_indexes = array();
  580. if ($options['alignment'] == 'horizontal') {
  581. $row = array();
  582. $col_count = 0;
  583. $row_count = 0;
  584. $count = 0;
  585. foreach ($vars['rows'] as $row_index => $item) {
  586. $count++;
  587. $row[] = $item;
  588. $row_indexes[$row_count][$col_count] = $row_index;
  589. $col_count++;
  590. if ($count % $columns == 0) {
  591. $rows[] = $row;
  592. $row = array();
  593. $col_count = 0;
  594. $row_count++;
  595. }
  596. }
  597. if ($row) {
  598. // Fill up the last line only if it's configured, but this is default.
  599. if (!empty($handler->options['fill_single_line']) && count($rows)) {
  600. for ($i = 0; $i < ($columns - $col_count); $i++) {
  601. $row[] = '';
  602. }
  603. }
  604. $rows[] = $row;
  605. }
  606. }
  607. else {
  608. $num_rows = floor(count($vars['rows']) / $columns);
  609. // The remainders are the 'odd' columns that are slightly longer.
  610. $remainders = count($vars['rows']) % $columns;
  611. $row = 0;
  612. $col = 0;
  613. foreach ($vars['rows'] as $count => $item) {
  614. $rows[$row][$col] = $item;
  615. $row_indexes[$row][$col] = $count;
  616. $row++;
  617. if (!$remainders && $row == $num_rows) {
  618. $row = 0;
  619. $col++;
  620. }
  621. elseif ($remainders && $row == $num_rows + 1) {
  622. $row = 0;
  623. $col++;
  624. $remainders--;
  625. }
  626. }
  627. for ($i = 0; $i < count($rows[0]); $i++) {
  628. // This should be string so that's okay :)
  629. if (!isset($rows[count($rows) - 1][$i])) {
  630. $rows[count($rows) - 1][$i] = '';
  631. }
  632. }
  633. }
  634. // Apply the row classes
  635. foreach ($rows as $row_number => $row) {
  636. $row_classes = array();
  637. if ($default_row_class) {
  638. $row_classes[] = 'row-' . ($row_number + 1);
  639. }
  640. if ($row_class_special) {
  641. if ($row_number == 0) {
  642. $row_classes[] = 'row-first';
  643. }
  644. if (count($rows) == ($row_number + 1)) {
  645. $row_classes[] = 'row-last';
  646. }
  647. }
  648. $vars['row_classes'][$row_number] = implode(' ', $row_classes);
  649. foreach ($rows[$row_number] as $column_number => $item) {
  650. $column_classes = array();
  651. if ($default_row_class) {
  652. $column_classes[] = 'col-'. ($column_number + 1);
  653. }
  654. if ($row_class_special) {
  655. if ($column_number == 0) {
  656. $column_classes[] = 'col-first';
  657. }
  658. elseif (count($rows[$row_number]) == ($column_number + 1)) {
  659. $column_classes[] = 'col-last';
  660. }
  661. }
  662. if (isset($row_indexes[$row_number][$column_number]) && $column_class = $view->style_plugin->get_row_class($row_indexes[$row_number][$column_number])) {
  663. $column_classes[] = $column_class;
  664. }
  665. $vars['column_classes'][$row_number][$column_number] = implode(' ', $column_classes);
  666. }
  667. }
  668. $vars['rows'] = $rows;
  669. $vars['class'] = 'views-view-grid cols-' . $columns;
  670. // Add the summary to the list if set.
  671. if (!empty($handler->options['summary'])) {
  672. $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
  673. }
  674. // Add the caption to the list if set.
  675. if (!empty($handler->options['caption'])) {
  676. $vars['caption'] = filter_xss_admin($handler->options['caption']);
  677. }
  678. else {
  679. $vars['caption'] = '';
  680. }
  681. }
  682. /**
  683. * Display the simple view of rows one after another
  684. */
  685. function template_preprocess_views_view_unformatted(&$vars) {
  686. $view = $vars['view'];
  687. $rows = $vars['rows'];
  688. $style = $view->style_plugin;
  689. $options = $style->options;
  690. $vars['classes_array'] = array();
  691. $vars['classes'] = array();
  692. $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : FALSE;
  693. $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : FALSE;
  694. // Set up striping values.
  695. $count = 0;
  696. $max = count($rows);
  697. foreach ($rows as $id => $row) {
  698. $count++;
  699. if ($default_row_class) {
  700. $vars['classes'][$id][] = 'views-row';
  701. $vars['classes'][$id][] = 'views-row-' . $count;
  702. }
  703. if ($row_class_special) {
  704. $vars['classes'][$id][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
  705. if ($count == 1) {
  706. $vars['classes'][$id][] = 'views-row-first';
  707. }
  708. if ($count == $max) {
  709. $vars['classes'][$id][] = 'views-row-last';
  710. }
  711. }
  712. if ($row_class = $view->style_plugin->get_row_class($id)) {
  713. $vars['classes'][$id][] = $row_class;
  714. }
  715. // Flatten the classes to a string for each row for the template file.
  716. $vars['classes_array'][$id] = isset($vars['classes'][$id]) ? implode(' ', $vars['classes'][$id]) : '';
  717. }
  718. }
  719. /**
  720. * Display the view as an HTML list element
  721. */
  722. function template_preprocess_views_view_list(&$vars) {
  723. $handler = $vars['view']->style_plugin;
  724. $class = explode(' ', $handler->options['class']);
  725. $class = array_map('views_clean_css_identifier', $class);
  726. $wrapper_class = explode(' ', $handler->options['wrapper_class']);
  727. $wrapper_class = array_map('views_clean_css_identifier', $wrapper_class);
  728. $vars['class'] = implode(' ', $class);
  729. $vars['wrapper_class'] = implode(' ', $wrapper_class);
  730. $vars['wrapper_prefix'] = '';
  731. $vars['wrapper_suffix'] = '';
  732. $vars['list_type_prefix'] = '<' . $handler->options['type'] . '>';
  733. $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
  734. if ($vars['wrapper_class']) {
  735. $vars['wrapper_prefix'] = '<div class="' . $vars['wrapper_class'] . '">';
  736. $vars['wrapper_suffix'] = '</div>';
  737. }
  738. if ($vars['class']) {
  739. $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' class="' . $vars['class'] . '">';
  740. }
  741. template_preprocess_views_view_unformatted($vars);
  742. }
  743. /**
  744. * Preprocess an RSS feed
  745. */
  746. function template_preprocess_views_view_rss(&$vars) {
  747. global $base_url;
  748. global $language;
  749. $view = &$vars['view'];
  750. $options = &$vars['options'];
  751. $items = &$vars['rows'];
  752. $style = &$view->style_plugin;
  753. // The RSS 2.0 "spec" doesn't indicate HTML can be used in the description.
  754. // We strip all HTML tags, but need to prevent double encoding from properly
  755. // escaped source data (such as &amp becoming &amp;amp;).
  756. $vars['description'] = check_plain(decode_entities(strip_tags($style->get_description())));
  757. if ($view->display_handler->get_option('sitename_title')) {
  758. $title = variable_get('site_name', 'Drupal');
  759. if ($slogan = variable_get('site_slogan', '')) {
  760. $title .= ' - ' . $slogan;
  761. }
  762. }
  763. else {
  764. $title = $view->get_title();
  765. }
  766. $vars['title'] = check_plain($title);
  767. // Figure out which display which has a path we're using for this feed. If there isn't
  768. // one, use the global $base_url
  769. $link_display_id = $view->display_handler->get_link_display();
  770. if ($link_display_id && !empty($view->display[$link_display_id])) {
  771. $path = $view->display[$link_display_id]->handler->get_path();
  772. }
  773. if ($path) {
  774. $path = $view->get_url(NULL, $path);
  775. $url_options = array('absolute' => TRUE);
  776. if (!empty($view->exposed_raw_input)) {
  777. $url_options['query'] = $view->exposed_raw_input;
  778. }
  779. // Compare the link to the default home page; if it's the default home page, just use $base_url.
  780. if ($path == variable_get('site_frontpage', 'node')) {
  781. $path = '';
  782. }
  783. $vars['link'] = check_url(url($path, $url_options));
  784. }
  785. $vars['langcode'] = check_plain($language->language);
  786. $vars['namespaces'] = drupal_attributes($style->namespaces);
  787. $vars['items'] = $items;
  788. $vars['channel_elements'] = format_xml_elements($style->channel_elements);
  789. // During live preview we don't want to output the header since the contents
  790. // of the feed are being displayed inside a normal HTML page.
  791. if (empty($vars['view']->live_preview)) {
  792. drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  793. }
  794. }
  795. /**
  796. * Default theme function for all RSS rows.
  797. */
  798. function template_preprocess_views_view_row_rss(&$vars) {
  799. $view = &$vars['view'];
  800. $options = &$vars['options'];
  801. $item = &$vars['row'];
  802. $vars['title'] = check_plain($item->title);
  803. $vars['link'] = check_url($item->link);
  804. $vars['description'] = check_plain($item->description);
  805. $vars['item_elements'] = empty($item->elements) ? '' : format_xml_elements($item->elements);
  806. }
  807. /**
  808. * Default theme function for all filter forms.
  809. */
  810. function template_preprocess_views_exposed_form(&$vars) {
  811. $form = &$vars['form'];
  812. // Put all single checkboxes together in the last spot.
  813. $checkboxes = '';
  814. if (!empty($form['q'])) {
  815. $vars['q'] = drupal_render($form['q']);
  816. }
  817. $vars['widgets'] = array();
  818. foreach ($form['#info'] as $id => $info) {
  819. // Set aside checkboxes.
  820. if (isset($form[$info['value']]['#type']) && $form[$info['value']]['#type'] == 'checkbox') {
  821. $checkboxes .= drupal_render($form[$info['value']]);
  822. continue;
  823. }
  824. $widget = new stdClass;
  825. // set up defaults so that there's always something there.
  826. $widget->label = $widget->operator = $widget->widget = $widget->description = NULL;
  827. $widget->id = isset($form[$info['value']]['#id']) ? $form[$info['value']]['#id'] : '';
  828. if (!empty($info['label'])) {
  829. $widget->label = check_plain($info['label']);
  830. }
  831. if (!empty($info['operator'])) {
  832. $widget->operator = drupal_render($form[$info['operator']]);
  833. }
  834. $widget->widget = drupal_render($form[$info['value']]);
  835. if (!empty($info['description'])) {
  836. $widget->description = check_plain($info['description']);
  837. }
  838. $vars['widgets'][$id] = $widget;
  839. }
  840. // Wrap up all the checkboxes we set aside into a widget.
  841. if ($checkboxes) {
  842. $widget = new stdClass;
  843. // set up defaults so that there's always something there.
  844. $widget->label = $widget->operator = $widget->widget = NULL;
  845. $widget->id = 'checkboxes';
  846. $widget->widget = $checkboxes;
  847. $vars['widgets']['checkboxes'] = $widget;
  848. }
  849. if (isset($form['sort_by'])) {
  850. $vars['sort_by'] = drupal_render($form['sort_by']);
  851. $vars['sort_order'] = drupal_render($form['sort_order']);
  852. }
  853. if (isset($form['items_per_page'])) {
  854. $vars['items_per_page'] = drupal_render($form['items_per_page']);
  855. }
  856. if (isset($form['offset'])) {
  857. $vars['offset'] = drupal_render($form['offset']);
  858. }
  859. if (isset($form['reset'])) {
  860. $vars['reset_button'] = drupal_render($form['reset']);
  861. }
  862. // This includes the submit button.
  863. $vars['button'] = drupal_render_children($form);
  864. }
  865. /**
  866. * Theme function for a View with form elements: replace the placeholders.
  867. */
  868. function theme_views_form_views_form($variables) {
  869. $form = $variables['form'];
  870. // Placeholders and their substitutions (usually rendered form elements).
  871. $search = array();
  872. $replace = array();
  873. // Add in substitutions provided by the form.
  874. foreach ($form['#substitutions']['#value'] as $substitution) {
  875. $field_name = $substitution['field_name'];
  876. $row_id = $substitution['row_id'];
  877. $search[] = $substitution['placeholder'];
  878. $replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
  879. }
  880. // Add in substitutions from hook_views_form_substitutions().
  881. $substitutions = module_invoke_all('views_form_substitutions');
  882. foreach ($substitutions as $placeholder => $substitution) {
  883. $search[] = $placeholder;
  884. $replace[] = $substitution;
  885. }
  886. // Apply substitutions to the rendered output.
  887. $form['output']['#markup'] = str_replace($search, $replace, $form['output']['#markup']);
  888. // Render and add remaining form fields.
  889. return drupal_render_children($form);
  890. }
  891. function theme_views_mini_pager($vars) {
  892. global $pager_page_array, $pager_total;
  893. $tags = $vars['tags'];
  894. $element = $vars['element'];
  895. $parameters = $vars['parameters'];
  896. // current is the page we are currently paged to
  897. $pager_current = $pager_page_array[$element] + 1;
  898. // max is the maximum page number
  899. $pager_max = $pager_total[$element];
  900. // End of marker calculations.
  901. if ($pager_total[$element] > 1) {
  902. $li_previous = theme('pager_previous',
  903. array(
  904. 'text' => (isset($tags[1]) ? $tags[1] : t('‹‹')),
  905. 'element' => $element,
  906. 'interval' => 1,
  907. 'parameters' => $parameters,
  908. )
  909. );
  910. if (empty($li_previous)) {
  911. $li_previous = "&nbsp;";
  912. }
  913. $li_next = theme('pager_next',
  914. array(
  915. 'text' => (isset($tags[3]) ? $tags[3] : t('››')),
  916. 'element' => $element,
  917. 'interval' => 1,
  918. 'parameters' => $parameters,
  919. )
  920. );
  921. if (empty($li_next)) {
  922. $li_next = "&nbsp;";
  923. }
  924. $items[] = array(
  925. 'class' => array('pager-previous'),
  926. 'data' => $li_previous,
  927. );
  928. $items[] = array(
  929. 'class' => array('pager-current'),
  930. 'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
  931. );
  932. $items[] = array(
  933. 'class' => array('pager-next'),
  934. 'data' => $li_next,
  935. );
  936. return theme('item_list',
  937. array(
  938. 'items' => $items,
  939. 'title' => NULL,
  940. 'type' => 'ul',
  941. 'attributes' => array('class' => array('pager')),
  942. )
  943. );
  944. }
  945. }
  946. /**
  947. * Generic <div> container function.
  948. */
  949. function theme_views_container($variables) {
  950. $element = $variables['element'];
  951. return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';
  952. }
  953. /**
  954. * @defgroup views_templates Views template files
  955. * @{
  956. * All views templates can be overridden with a variety of names, using
  957. * the view, the display ID of the view, the display type of the view,
  958. * or some combination thereof.
  959. *
  960. * For each view, there will be a minimum of two templates used. The first
  961. * is used for all views: views-view.tpl.php.
  962. *
  963. * The second template is determined by the style selected for the view. Note
  964. * that certain aspects of the view can also change which style is used; for
  965. * example, arguments which provide a summary view might change the style to
  966. * one of the special summary styles.
  967. *
  968. * The default style for all views is views-view-unformatted.tpl.php
  969. *
  970. * Many styles will then farm out the actual display of each row to a row
  971. * style; the default row style is views-view-fields.tpl.php.
  972. *
  973. * Here is an example of all the templates that will be tried in the following
  974. * case:
  975. *
  976. * View, named foobar. Style: unformatted. Row style: Fields. Display: Page.
  977. *
  978. * - views-view--foobar--page.tpl.php
  979. * - views-view--page.tpl.php
  980. * - views-view--foobar.tpl.php
  981. * - views-view.tpl.php
  982. *
  983. * - views-view-unformatted--foobar--page.tpl.php
  984. * - views-view-unformatted--page.tpl.php
  985. * - views-view-unformatted--foobar.tpl.php
  986. * - views-view-unformatted.tpl.php
  987. *
  988. * - views-view-fields--foobar--page.tpl.php
  989. * - views-view-fields--page.tpl.php
  990. * - views-view-fields--foobar.tpl.php
  991. * - views-view-fields.tpl.php
  992. *
  993. * Important! When adding a new template to your theme, be sure to flush the
  994. * theme registry cache!
  995. *
  996. * @see _views_theme_functions()
  997. * @}
  998. */