theme.inc 39 KB

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