theme.inc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. url($_GET['q'], $url_options + array('alias' => TRUE)),
  326. // Could be an alias.
  327. url($_GET['q']),
  328. url($_GET['q'], $url_options),
  329. ));
  330. // Collect all arguments foreach row, to be able to alter them for example by
  331. // the validator. This is not done per single argument value, because this
  332. // could cause performance problems.
  333. $row_args = array();
  334. foreach ($vars['rows'] as $id => $row) {
  335. $row_args[$id] = $argument->summary_argument($row);
  336. }
  337. $argument->process_summary_arguments($row_args);
  338. foreach ($vars['rows'] as $id => $row) {
  339. $vars['row_classes'][$id] = '';
  340. $vars['rows'][$id]->link = $argument->summary_name($row);
  341. $args = $view->args;
  342. $args[$argument->position] = $row_args[$id];
  343. $base_path = NULL;
  344. if (!empty($argument->options['summary_options']['base_path'])) {
  345. $base_path = $argument->options['summary_options']['base_path'];
  346. }
  347. $vars['rows'][$id]->url = url($view->get_url($args, $base_path), $url_options);
  348. $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
  349. if (isset($active_urls[$vars['rows'][$id]->url])) {
  350. $vars['row_classes'][$id] = 'active';
  351. }
  352. }
  353. }
  354. /**
  355. * Template preprocess theme function to print summary basically unformatted.
  356. */
  357. function template_preprocess_views_view_summary_unformatted(&$vars) {
  358. $view = $vars['view'];
  359. $argument = $view->argument[$view->build_info['summary_level']];
  360. $vars['row_classes'] = array();
  361. $url_options = array();
  362. if (!empty($view->exposed_raw_input)) {
  363. $url_options['query'] = $view->exposed_raw_input;
  364. }
  365. $count = 0;
  366. $active_urls = drupal_map_assoc(array(
  367. // Force system path.
  368. url($_GET['q'], array('alias' => TRUE)),
  369. url($_GET['q'], $url_options + array('alias' => TRUE)),
  370. // Could be an alias.
  371. url($_GET['q']),
  372. url($_GET['q'], $url_options),
  373. ));
  374. // Collect all arguments foreach row, to be able to alter them for example by
  375. // the validator. This is not done per single argument value, because this
  376. // could cause performance problems.
  377. $row_args = array();
  378. foreach ($vars['rows'] as $id => $row) {
  379. $row_args[$id] = $argument->summary_argument($row);
  380. }
  381. $argument->process_summary_arguments($row_args);
  382. foreach ($vars['rows'] as $id => $row) {
  383. // Only false on first time.
  384. if ($count++) {
  385. $vars['rows'][$id]->separator = filter_xss_admin($vars['options']['separator']);
  386. }
  387. $vars['rows'][$id]->link = $argument->summary_name($row);
  388. $args = $view->args;
  389. $args[$argument->position] = $row_args[$id];
  390. $base_path = NULL;
  391. if (!empty($argument->options['summary_options']['base_path'])) {
  392. $base_path = $argument->options['summary_options']['base_path'];
  393. }
  394. $vars['rows'][$id]->url = url($view->get_url($args, $base_path), $url_options);
  395. $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
  396. if (isset($active_urls[$vars['rows'][$id]->url])) {
  397. $vars['row_classes'][$id] = 'active';
  398. }
  399. }
  400. }
  401. /**
  402. * Display a view as a table style.
  403. */
  404. function template_preprocess_views_view_table(&$vars) {
  405. $view = $vars['view'];
  406. // We need the raw data for this grouping, which is passed in as
  407. // $vars['rows']. However, the template also needs to use for the rendered
  408. // fields. We therefore swap the raw data out to a new variable and reset
  409. // $vars['rows'] so that it can get rebuilt. Store rows so that they may be
  410. // used by further preprocess functions.
  411. $result = $vars['result'] = $vars['rows'];
  412. $vars['rows'] = array();
  413. $vars['field_classes'] = array();
  414. $vars['header'] = array();
  415. $vars['classes_array'] = array();
  416. $options = $view->style_plugin->options;
  417. $handler = $view->style_plugin;
  418. if (!empty($handler->options['class'])) {
  419. $classes = explode(' ', $handler->options['class']);
  420. $classes = array_map('views_clean_css_identifier', $classes);
  421. if (!empty($classes)) {
  422. // Trim empty class entries.
  423. foreach ($classes as $key => $class) {
  424. if (!empty($class)) {
  425. $vars['classes_array'][] = $class;
  426. }
  427. }
  428. }
  429. }
  430. $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
  431. $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
  432. $fields = &$view->field;
  433. $columns = $handler->sanitize_columns($options['columns'], $fields);
  434. $active = !empty($handler->active) ? $handler->active : '';
  435. $order = !empty($handler->order) ? $handler->order : 'asc';
  436. $query = tablesort_get_query_parameters();
  437. if (isset($view->exposed_raw_input)) {
  438. $query += $view->exposed_raw_input;
  439. }
  440. // Fields must be rendered in order as of Views 2.3, so we will pre-render
  441. // everything.
  442. $renders = $handler->render_fields($result);
  443. foreach ($columns as $field => $column) {
  444. // Create a second variable so we can easily find what fields we have and
  445. // what the CSS classes should be.
  446. $vars['fields'][$field] = drupal_clean_css_identifier($field);
  447. if ($active == $field) {
  448. $vars['fields'][$field] .= ' active';
  449. }
  450. // Render the header labels.
  451. if ($field == $column && empty($fields[$field]->options['exclude'])) {
  452. $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
  453. if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
  454. $vars['header'][$field] = $label;
  455. }
  456. else {
  457. $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
  458. if ($active == $field) {
  459. $initial = ($order == 'asc') ? 'desc' : 'asc';
  460. }
  461. $title = t('sort by @s', array('@s' => $label));
  462. if ($active == $field) {
  463. $label .= theme('tablesort_indicator', array('style' => $initial));
  464. }
  465. $query['order'] = $field;
  466. $query['sort'] = $initial;
  467. $link_options = array(
  468. 'html' => TRUE,
  469. 'attributes' => array('title' => $title),
  470. 'query' => $query,
  471. );
  472. $vars['header'][$field] = l($label, $_GET['q'], $link_options);
  473. }
  474. $vars['header_classes'][$field] = '';
  475. // Set up the header label class.
  476. if ($fields[$field]->options['element_default_classes']) {
  477. $vars['header_classes'][$field] .= "views-field views-field-" . $vars['fields'][$field];
  478. }
  479. $class = $fields[$field]->element_label_classes(0);
  480. if ($class) {
  481. if ($vars['header_classes'][$field]) {
  482. $vars['header_classes'][$field] .= ' ';
  483. }
  484. $vars['header_classes'][$field] .= $class;
  485. }
  486. // Add a CSS align class to each field if one was set.
  487. if (!empty($options['info'][$field]['align'])) {
  488. $vars['header_classes'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
  489. }
  490. // Add a header label wrapper if one was selected.
  491. if ($vars['header'][$field]) {
  492. $element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
  493. if ($element_label_type) {
  494. $vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
  495. }
  496. }
  497. }
  498. // Add a CSS align class to each field if one was set.
  499. if (!empty($options['info'][$field]['align'])) {
  500. $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
  501. }
  502. // Render each field into its appropriate column.
  503. foreach ($result as $num => $row) {
  504. // Add field classes.
  505. $vars['field_classes'][$field][$num] = '';
  506. if ($fields[$field]->options['element_default_classes']) {
  507. $vars['field_classes'][$field][$num] = "views-field views-field-" . $vars['fields'][$field];
  508. }
  509. if ($classes = $fields[$field]->element_classes($num)) {
  510. if ($vars['field_classes'][$field][$num]) {
  511. $vars['field_classes'][$field][$num] .= ' ';
  512. }
  513. $vars['field_classes'][$field][$num] .= $classes;
  514. }
  515. $vars['field_attributes'][$field][$num] = array();
  516. if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
  517. $field_output = $renders[$num][$field];
  518. $element_type = $fields[$field]->element_type(TRUE, TRUE);
  519. if ($element_type) {
  520. $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
  521. }
  522. // Don't bother with separators and stuff if the field does not show up.
  523. if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
  524. continue;
  525. }
  526. // Place the field into the column, along with an optional separator.
  527. if (!empty($vars['rows'][$num][$column])) {
  528. if (!empty($options['info'][$column]['separator'])) {
  529. $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
  530. }
  531. }
  532. else {
  533. $vars['rows'][$num][$column] = '';
  534. }
  535. $vars['rows'][$num][$column] .= $field_output;
  536. }
  537. }
  538. // Remove columns if the option is hide empty column is checked and the
  539. // field is not empty.
  540. if (!empty($options['info'][$field]['empty_column'])) {
  541. $empty = TRUE;
  542. foreach ($vars['rows'] as $num => $columns) {
  543. $empty &= empty($columns[$column]);
  544. }
  545. if ($empty) {
  546. foreach ($vars['rows'] as $num => &$column_items) {
  547. unset($column_items[$column]);
  548. unset($vars['header'][$column]);
  549. }
  550. }
  551. }
  552. }
  553. // Hide table header if all labels are empty.
  554. if (!array_filter($vars['header'])) {
  555. $vars['header'] = array();
  556. }
  557. $count = 0;
  558. foreach ($vars['rows'] as $num => $row) {
  559. $vars['row_classes'][$num] = array();
  560. if ($row_class_special) {
  561. $vars['row_classes'][$num][] = ($count++ % 2 == 0) ? 'odd' : 'even';
  562. }
  563. if ($row_class = $handler->get_row_class($num)) {
  564. $vars['row_classes'][$num][] = $row_class;
  565. }
  566. }
  567. if ($row_class_special) {
  568. $vars['row_classes'][0][] = 'views-row-first';
  569. $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
  570. }
  571. $vars['classes_array'][] = 'views-table';
  572. if (empty($vars['rows']) && !empty($options['empty_table'])) {
  573. $vars['rows'][0][0] = $view->display_handler->render_area('empty');
  574. // Calculate the amounts of rows with output.
  575. $vars['field_attributes'][0][0]['colspan'] = count($vars['header']);
  576. $vars['field_classes'][0][0] = 'views-empty';
  577. }
  578. if (!empty($options['sticky'])) {
  579. drupal_add_js('misc/tableheader.js');
  580. $vars['classes_array'][] = "sticky-enabled";
  581. }
  582. $vars['classes_array'][] = 'cols-' . count($vars['header']);
  583. // Add the summary to the list if set.
  584. if (!empty($handler->options['summary'])) {
  585. $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
  586. }
  587. // Add the caption to the list if set.
  588. if (!empty($handler->options['caption'])) {
  589. $vars['caption'] = filter_xss_admin($handler->options['caption']);
  590. }
  591. else {
  592. $vars['caption'] = '';
  593. }
  594. }
  595. /**
  596. * Display a view as a grid style.
  597. */
  598. function template_preprocess_views_view_grid(&$vars) {
  599. $view = $vars['view'];
  600. $result = $view->result;
  601. $options = $view->style_plugin->options;
  602. $handler = $view->style_plugin;
  603. $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
  604. $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
  605. $columns = $options['columns'];
  606. $rows = array();
  607. $row_indexes = array();
  608. if ($options['alignment'] == 'horizontal') {
  609. $row = array();
  610. $col_count = 0;
  611. $row_count = 0;
  612. $count = 0;
  613. foreach ($vars['rows'] as $row_index => $item) {
  614. $count++;
  615. $row[] = $item;
  616. $row_indexes[$row_count][$col_count] = $row_index;
  617. $col_count++;
  618. if ($count % $columns == 0) {
  619. $rows[] = $row;
  620. $row = array();
  621. $col_count = 0;
  622. $row_count++;
  623. }
  624. }
  625. if ($row) {
  626. // Fill up the last line only if it's configured, but this is default.
  627. if (!empty($handler->options['fill_single_line'])) {
  628. for ($i = 0; $i < ($columns - $col_count); $i++) {
  629. $row[] = '';
  630. }
  631. }
  632. $rows[] = $row;
  633. }
  634. }
  635. else {
  636. $num_rows = floor(count($vars['rows']) / $columns);
  637. // The remainders are the 'odd' columns that are slightly longer.
  638. $remainders = count($vars['rows']) % $columns;
  639. $row = 0;
  640. $col = 0;
  641. foreach ($vars['rows'] as $count => $item) {
  642. $rows[$row][$col] = $item;
  643. $row_indexes[$row][$col] = $count;
  644. $row++;
  645. if (!$remainders && $row == $num_rows) {
  646. $row = 0;
  647. $col++;
  648. }
  649. elseif ($remainders && $row == $num_rows + 1) {
  650. $row = 0;
  651. $col++;
  652. $remainders--;
  653. }
  654. }
  655. // Fill out the row with empty values, if needed.
  656. if (!empty($handler->options['fill_single_line'])) {
  657. $column_fill = $columns;
  658. }
  659. else {
  660. $column_fill = count($rows[0]);
  661. }
  662. for ($i = 0; $i < $column_fill; $i++) {
  663. if (!isset($rows[count($rows) - 1][$i])) {
  664. $rows[count($rows) - 1][$i] = '';
  665. }
  666. }
  667. }
  668. // Apply the row classes.
  669. foreach ($rows as $row_number => $row) {
  670. $row_classes = array();
  671. if ($default_row_class) {
  672. $row_classes[] = 'row-' . ($row_number + 1);
  673. }
  674. if ($row_class_special) {
  675. if ($row_number == 0) {
  676. $row_classes[] = 'row-first';
  677. }
  678. if (count($rows) == ($row_number + 1)) {
  679. $row_classes[] = 'row-last';
  680. }
  681. }
  682. $vars['row_classes'][$row_number] = implode(' ', $row_classes);
  683. foreach ($rows[$row_number] as $column_number => $item) {
  684. $column_classes = array();
  685. if ($default_row_class) {
  686. $column_classes[] = 'col-' . ($column_number + 1);
  687. }
  688. if ($row_class_special) {
  689. if ($column_number == 0) {
  690. $column_classes[] = 'col-first';
  691. }
  692. elseif (count($rows[$row_number]) == ($column_number + 1)) {
  693. $column_classes[] = 'col-last';
  694. }
  695. }
  696. if (isset($row_indexes[$row_number][$column_number]) && $column_class = $view->style_plugin->get_row_class($row_indexes[$row_number][$column_number])) {
  697. $column_classes[] = $column_class;
  698. }
  699. $vars['column_classes'][$row_number][$column_number] = implode(' ', $column_classes);
  700. }
  701. }
  702. $vars['rows'] = $rows;
  703. $vars['class'] = 'views-view-grid cols-' . $columns;
  704. // Add the summary to the list if set.
  705. if (!empty($handler->options['summary'])) {
  706. $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
  707. }
  708. // Add the caption to the list if set.
  709. if (!empty($handler->options['caption'])) {
  710. $vars['caption'] = filter_xss_admin($handler->options['caption']);
  711. }
  712. else {
  713. $vars['caption'] = '';
  714. }
  715. }
  716. /**
  717. * Display the simple view of rows one after another.
  718. */
  719. function template_preprocess_views_view_unformatted(&$vars) {
  720. $view = $vars['view'];
  721. $rows = $vars['rows'];
  722. $style = $view->style_plugin;
  723. $options = $style->options;
  724. $vars['classes_array'] = array();
  725. $vars['classes'] = array();
  726. $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : FALSE;
  727. $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : FALSE;
  728. // Set up striping values.
  729. $count = 0;
  730. $max = count($rows);
  731. foreach ($rows as $id => $row) {
  732. $count++;
  733. if ($default_row_class) {
  734. $vars['classes'][$id][] = 'views-row';
  735. $vars['classes'][$id][] = 'views-row-' . $count;
  736. }
  737. if ($row_class_special) {
  738. $vars['classes'][$id][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
  739. if ($count == 1) {
  740. $vars['classes'][$id][] = 'views-row-first';
  741. }
  742. if ($count == $max) {
  743. $vars['classes'][$id][] = 'views-row-last';
  744. }
  745. }
  746. if ($row_class = $view->style_plugin->get_row_class($id)) {
  747. $vars['classes'][$id][] = $row_class;
  748. }
  749. // Flatten the classes to a string for each row for the template file.
  750. $vars['classes_array'][$id] = isset($vars['classes'][$id]) ? implode(' ', $vars['classes'][$id]) : '';
  751. }
  752. }
  753. /**
  754. * Display the view as an HTML list element.
  755. */
  756. function template_preprocess_views_view_list(&$vars) {
  757. $handler = $vars['view']->style_plugin;
  758. $class = explode(' ', $handler->options['class']);
  759. $class = array_map('views_clean_css_identifier', $class);
  760. $wrapper_class = explode(' ', $handler->options['wrapper_class']);
  761. $wrapper_class = array_map('views_clean_css_identifier', $wrapper_class);
  762. $vars['class'] = implode(' ', $class);
  763. $vars['wrapper_class'] = implode(' ', $wrapper_class);
  764. $vars['wrapper_prefix'] = '';
  765. $vars['wrapper_suffix'] = '';
  766. $vars['list_type_prefix'] = '<' . $handler->options['type'] . '>';
  767. $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
  768. if ($vars['wrapper_class']) {
  769. $vars['wrapper_prefix'] = '<div class="' . $vars['wrapper_class'] . '">';
  770. $vars['wrapper_suffix'] = '</div>';
  771. }
  772. if ($vars['class']) {
  773. $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' class="' . $vars['class'] . '">';
  774. }
  775. template_preprocess_views_view_unformatted($vars);
  776. }
  777. /**
  778. * Preprocess an RSS feed.
  779. */
  780. function template_preprocess_views_view_rss(&$vars) {
  781. global $base_url;
  782. global $language;
  783. $view = &$vars['view'];
  784. $options = &$vars['options'];
  785. $items = &$vars['rows'];
  786. $style = &$view->style_plugin;
  787. // The RSS 2.0 "spec" doesn't indicate HTML can be used in the description.
  788. // We strip all HTML tags, but need to prevent double encoding from properly
  789. // escaped source data (such as &amp becoming &amp;amp;).
  790. $vars['description'] = check_plain(decode_entities(strip_tags($style->get_description())));
  791. if ($view->display_handler->get_option('sitename_title')) {
  792. $title = variable_get('site_name', 'Drupal');
  793. if ($slogan = variable_get('site_slogan', '')) {
  794. $title .= ' - ' . $slogan;
  795. }
  796. }
  797. else {
  798. $title = $view->get_title();
  799. }
  800. $vars['title'] = check_plain($title);
  801. // Figure out which display which has a path we're using for this feed. If
  802. // there isn't one, use the global $base_url.
  803. $link_display_id = $view->display_handler->get_link_display();
  804. if ($link_display_id && !empty($view->display[$link_display_id])) {
  805. $path = $view->display[$link_display_id]->handler->get_path();
  806. }
  807. if ($path) {
  808. $path = $view->get_url(NULL, $path);
  809. $url_options = array('absolute' => TRUE);
  810. if (!empty($view->exposed_raw_input)) {
  811. $url_options['query'] = $view->exposed_raw_input;
  812. }
  813. // Compare the link to the default home page; if it's the default home
  814. // page, just use $base_url.
  815. if ($path == variable_get('site_frontpage', 'node')) {
  816. $path = '';
  817. }
  818. $vars['link'] = check_url(url($path, $url_options));
  819. }
  820. $vars['langcode'] = check_plain($language->language);
  821. $vars['namespaces'] = drupal_attributes($style->namespaces);
  822. $vars['items'] = $items;
  823. $vars['channel_elements'] = format_xml_elements($style->channel_elements);
  824. // During live preview we don't want to output the header since the contents
  825. // of the feed are being displayed inside a normal HTML page.
  826. if (empty($vars['view']->live_preview)) {
  827. drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  828. }
  829. }
  830. /**
  831. * Default theme function for all RSS rows.
  832. */
  833. function template_preprocess_views_view_row_rss(&$vars) {
  834. $view = &$vars['view'];
  835. $options = &$vars['options'];
  836. $item = &$vars['row'];
  837. $vars['title'] = check_plain($item->title);
  838. $vars['link'] = check_url($item->link);
  839. $vars['description'] = check_plain($item->description);
  840. $vars['item_elements'] = empty($item->elements) ? '' : format_xml_elements($item->elements);
  841. }
  842. /**
  843. * Default theme function for all filter forms.
  844. */
  845. function template_preprocess_views_exposed_form(&$vars) {
  846. $form = &$vars['form'];
  847. // Put all single checkboxes together in the last spot.
  848. $checkboxes = '';
  849. if (!empty($form['q'])) {
  850. $vars['q'] = drupal_render($form['q']);
  851. }
  852. $vars['widgets'] = array();
  853. foreach ($form['#info'] as $id => $info) {
  854. // Set aside checkboxes.
  855. if (isset($form[$info['value']]['#type']) && $form[$info['value']]['#type'] == 'checkbox') {
  856. $checkboxes .= drupal_render($form[$info['value']]);
  857. continue;
  858. }
  859. $widget = new stdClass();
  860. // Set up defaults so that there's always something there.
  861. $widget->label = $widget->operator = $widget->widget = $widget->description = NULL;
  862. $widget->id = isset($form[$info['value']]['#id']) ? $form[$info['value']]['#id'] : '';
  863. if (!empty($info['label'])) {
  864. $widget->label = check_plain($info['label']);
  865. }
  866. if (!empty($info['operator'])) {
  867. $widget->operator = drupal_render($form[$info['operator']]);
  868. }
  869. $widget->widget = drupal_render($form[$info['value']]);
  870. if (!empty($info['description'])) {
  871. $widget->description = check_plain($info['description']);
  872. }
  873. $vars['widgets'][$id] = $widget;
  874. }
  875. // Wrap up all the checkboxes we set aside into a widget.
  876. if ($checkboxes) {
  877. $widget = new stdClass();
  878. // Set up defaults so that there's always something there.
  879. $widget->label = $widget->operator = $widget->widget = NULL;
  880. $widget->id = 'checkboxes';
  881. $widget->widget = $checkboxes;
  882. $vars['widgets']['checkboxes'] = $widget;
  883. }
  884. if (isset($form['sort_by'])) {
  885. $vars['sort_by'] = drupal_render($form['sort_by']);
  886. $vars['sort_order'] = drupal_render($form['sort_order']);
  887. }
  888. if (isset($form['items_per_page'])) {
  889. $vars['items_per_page'] = drupal_render($form['items_per_page']);
  890. }
  891. if (isset($form['offset'])) {
  892. $vars['offset'] = drupal_render($form['offset']);
  893. }
  894. if (isset($form['reset'])) {
  895. $vars['reset_button'] = drupal_render($form['reset']);
  896. }
  897. // This includes the submit button.
  898. $vars['button'] = drupal_render_children($form);
  899. }
  900. /**
  901. * Theme function for a View with form elements: replace the placeholders.
  902. */
  903. function theme_views_form_views_form($variables) {
  904. $form = $variables['form'];
  905. // Placeholders and their substitutions (usually rendered form elements).
  906. $search = array();
  907. $replace = array();
  908. // Add in substitutions provided by the form.
  909. foreach ($form['#substitutions']['#value'] as $substitution) {
  910. $field_name = $substitution['field_name'];
  911. $row_id = $substitution['row_id'];
  912. $search[] = $substitution['placeholder'];
  913. $replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
  914. }
  915. // Add in substitutions from hook_views_form_substitutions().
  916. $substitutions = module_invoke_all('views_form_substitutions');
  917. foreach ($substitutions as $placeholder => $substitution) {
  918. $search[] = $placeholder;
  919. $replace[] = $substitution;
  920. }
  921. // Apply substitutions to the rendered output.
  922. $form['output']['#markup'] = str_replace($search, $replace, $form['output']['#markup']);
  923. // Render and add remaining form fields.
  924. return drupal_render_children($form);
  925. }
  926. /**
  927. * theme function for a mini pager.
  928. */
  929. function theme_views_mini_pager($vars) {
  930. global $pager_page_array, $pager_total;
  931. $tags = $vars['tags'];
  932. $element = $vars['element'];
  933. $parameters = $vars['parameters'];
  934. // Current is the page we are currently paged to.
  935. $pager_current = $pager_page_array[$element] + 1;
  936. // Max is the maximum page number.
  937. $pager_max = $pager_total[$element];
  938. // End of marker calculations.
  939. if ($pager_total[$element] > 1) {
  940. $li_previous = theme('pager_previous',
  941. array(
  942. 'text' => (isset($tags[1]) ? $tags[1] : t('‹‹')),
  943. 'element' => $element,
  944. 'interval' => 1,
  945. 'parameters' => $parameters,
  946. )
  947. );
  948. if (empty($li_previous)) {
  949. $li_previous = "&nbsp;";
  950. }
  951. $li_next = theme('pager_next',
  952. array(
  953. 'text' => (isset($tags[3]) ? $tags[3] : t('››')),
  954. 'element' => $element,
  955. 'interval' => 1,
  956. 'parameters' => $parameters,
  957. )
  958. );
  959. if (empty($li_next)) {
  960. $li_next = "&nbsp;";
  961. }
  962. $items[] = array(
  963. 'class' => array('pager-previous'),
  964. 'data' => $li_previous,
  965. );
  966. $items[] = array(
  967. 'class' => array('pager-current'),
  968. 'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
  969. );
  970. $items[] = array(
  971. 'class' => array('pager-next'),
  972. 'data' => $li_next,
  973. );
  974. return theme('item_list',
  975. array(
  976. 'items' => $items,
  977. 'title' => NULL,
  978. 'type' => 'ul',
  979. 'attributes' => array('class' => array('pager')),
  980. )
  981. );
  982. }
  983. }
  984. /**
  985. * Generic <div> container function.
  986. */
  987. function theme_views_container($variables) {
  988. $element = $variables['element'];
  989. return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';
  990. }
  991. /**
  992. * @defgroup views_templates Views template files
  993. * @{
  994. * All views templates can be overridden with a variety of names, using
  995. * the view, the display ID of the view, the display type of the view,
  996. * or some combination thereof.
  997. *
  998. * For each view, there will be a minimum of two templates used. The first
  999. * is used for all views: views-view.tpl.php.
  1000. *
  1001. * The second template is determined by the style selected for the view. Note
  1002. * that certain aspects of the view can also change which style is used; for
  1003. * example, arguments which provide a summary view might change the style to
  1004. * one of the special summary styles.
  1005. *
  1006. * The default style for all views is views-view-unformatted.tpl.php
  1007. *
  1008. * Many styles will then farm out the actual display of each row to a row
  1009. * style; the default row style is views-view-fields.tpl.php.
  1010. *
  1011. * Here is an example of all the templates that will be tried in the following
  1012. * case:
  1013. *
  1014. * View, named foobar. Style: unformatted. Row style: Fields. Display: Page.
  1015. *
  1016. * - views-view--foobar--page.tpl.php
  1017. * - views-view--page.tpl.php
  1018. * - views-view--foobar.tpl.php
  1019. * - views-view.tpl.php
  1020. *
  1021. * - views-view-unformatted--foobar--page.tpl.php
  1022. * - views-view-unformatted--page.tpl.php
  1023. * - views-view-unformatted--foobar.tpl.php
  1024. * - views-view-unformatted.tpl.php
  1025. *
  1026. * - views-view-fields--foobar--page.tpl.php
  1027. * - views-view-fields--page.tpl.php
  1028. * - views-view-fields--foobar.tpl.php
  1029. * - views-view-fields.tpl.php
  1030. *
  1031. * Important! When adding a new template to your theme, be sure to flush the
  1032. * theme registry cache!
  1033. *
  1034. * @see _views_theme_functions()
  1035. * @}
  1036. */