theme.inc 39 KB

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