views_plugin_style.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_style.
  5. */
  6. /**
  7. * @defgroup views_style_plugins Views style plugins
  8. * @{
  9. * Style plugins control how a view is rendered. For example, they
  10. * can choose to display a collection of fields, node_view() output,
  11. * table output, or any kind of crazy output they want.
  12. *
  13. * Many style plugins can have an optional 'row' plugin, that displays
  14. * a single record. Not all style plugins can utilize this, so it is
  15. * up to the plugin to set this up and call through to the row plugin.
  16. *
  17. * @see hook_views_plugins()
  18. */
  19. /**
  20. * Base class to define a style plugin handler.
  21. */
  22. class views_plugin_style extends views_plugin {
  23. /**
  24. * Store all available tokens row rows.
  25. */
  26. var $row_tokens = array();
  27. /**
  28. * Contains the row plugin, if it's initialized
  29. * and the style itself supports it.
  30. *
  31. * @var views_plugin_row
  32. */
  33. var $row_plugin;
  34. /**
  35. * Initialize a style plugin.
  36. *
  37. * @param $view
  38. * @param $display
  39. * @param $options
  40. * The style options might come externally as the style can be sourced
  41. * from at least two locations. If it's not included, look on the display.
  42. */
  43. function init(&$view, &$display, $options = NULL) {
  44. $this->view = &$view;
  45. $this->display = &$display;
  46. // Overlay incoming options on top of defaults
  47. $this->unpack_options($this->options, isset($options) ? $options : $display->handler->get_option('style_options'));
  48. if ($this->uses_row_plugin() && $display->handler->get_option('row_plugin')) {
  49. $this->row_plugin = $display->handler->get_plugin('row');
  50. }
  51. $this->options += array(
  52. 'grouping' => array(),
  53. );
  54. $this->definition += array(
  55. 'uses grouping' => TRUE,
  56. );
  57. }
  58. function destroy() {
  59. parent::destroy();
  60. if (isset($this->row_plugin)) {
  61. $this->row_plugin->destroy();
  62. }
  63. }
  64. /**
  65. * Return TRUE if this style also uses a row plugin.
  66. */
  67. function uses_row_plugin() {
  68. return !empty($this->definition['uses row plugin']);
  69. }
  70. /**
  71. * Return TRUE if this style also uses a row plugin.
  72. */
  73. function uses_row_class() {
  74. return !empty($this->definition['uses row class']);
  75. }
  76. /**
  77. * Return TRUE if this style also uses fields.
  78. *
  79. * @return bool
  80. */
  81. function uses_fields() {
  82. // If we use a row plugin, ask the row plugin. Chances are, we don't
  83. // care, it does.
  84. $row_uses_fields = FALSE;
  85. if ($this->uses_row_plugin() && !empty($this->row_plugin)) {
  86. $row_uses_fields = $this->row_plugin->uses_fields();
  87. }
  88. // Otherwise, check the definition or the option.
  89. return $row_uses_fields || !empty($this->definition['uses fields']) || !empty($this->options['uses_fields']);
  90. }
  91. /**
  92. * Return TRUE if this style uses tokens.
  93. *
  94. * Used to ensure we don't fetch tokens when not needed for performance.
  95. */
  96. function uses_tokens() {
  97. if ($this->uses_row_class()) {
  98. $class = $this->options['row_class'];
  99. if (strpos($class, '[') !== FALSE || strpos($class, '!') !== FALSE || strpos($class, '%') !== FALSE) {
  100. return TRUE;
  101. }
  102. }
  103. }
  104. /**
  105. * Return the token replaced row class for the specified row.
  106. */
  107. function get_row_class($row_index) {
  108. if ($this->uses_row_class()) {
  109. $class = $this->options['row_class'];
  110. if ($this->uses_fields() && $this->view->field) {
  111. $classes = array();
  112. // Explode the value by whitespace, this allows the function to handle
  113. // a single class name and multiple class names that are then tokenized.
  114. foreach(explode(' ', $class) as $token_class) {
  115. $classes[] = strip_tags($this->tokenize_value($token_class, $row_index));
  116. }
  117. }
  118. else {
  119. $classes = explode(' ', $class);
  120. }
  121. // Convert whatever the result is to a nice clean class name
  122. foreach ($classes as &$class) {
  123. $class = drupal_html_class($class);
  124. }
  125. return implode(' ', $classes);
  126. }
  127. }
  128. /**
  129. * Take a value and apply token replacement logic to it.
  130. */
  131. function tokenize_value($value, $row_index) {
  132. if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
  133. $fake_item = array(
  134. 'alter_text' => TRUE,
  135. 'text' => $value,
  136. );
  137. // Row tokens might be empty, for example for node row style.
  138. $tokens = isset($this->row_tokens[$row_index]) ? $this->row_tokens[$row_index] : array();
  139. if (!empty($this->view->build_info['substitutions'])) {
  140. $tokens += $this->view->build_info['substitutions'];
  141. }
  142. if ($tokens) {
  143. $value = strtr($value, $tokens);
  144. }
  145. }
  146. return $value;
  147. }
  148. /**
  149. * Should the output of the style plugin be rendered even if it's a empty view.
  150. */
  151. function even_empty() {
  152. return !empty($this->definition['even empty']);
  153. }
  154. function option_definition() {
  155. $options = parent::option_definition();
  156. $options['grouping'] = array('default' => array());
  157. if ($this->uses_row_class()) {
  158. $options['row_class'] = array('default' => '');
  159. $options['default_row_class'] = array('default' => TRUE, 'bool' => TRUE);
  160. $options['row_class_special'] = array('default' => TRUE, 'bool' => TRUE);
  161. }
  162. $options['uses_fields'] = array('default' => FALSE, 'bool' => TRUE);
  163. return $options;
  164. }
  165. function options_form(&$form, &$form_state) {
  166. parent::options_form($form, $form_state);
  167. // Only fields-based views can handle grouping. Style plugins can also exclude
  168. // themselves from being groupable by setting their "use grouping" definition
  169. // key to FALSE.
  170. // @TODO: Document "uses grouping" in docs.php when docs.php is written.
  171. if ($this->uses_fields() && $this->definition['uses grouping']) {
  172. $options = array('' => t('- None -'));
  173. $field_labels = $this->display->handler->get_field_labels(TRUE);
  174. $options += $field_labels;
  175. // If there are no fields, we can't group on them.
  176. if (count($options) > 1) {
  177. // This is for backward compatibility, when there was just a single select form.
  178. if (is_string($this->options['grouping'])) {
  179. $grouping = $this->options['grouping'];
  180. $this->options['grouping'] = array();
  181. $this->options['grouping'][0]['field'] = $grouping;
  182. }
  183. if (isset($this->options['group_rendered']) && is_string($this->options['group_rendered'])) {
  184. $this->options['grouping'][0]['rendered'] = $this->options['group_rendered'];
  185. unset($this->options['group_rendered']);
  186. }
  187. $c = count($this->options['grouping']);
  188. // Add a form for every grouping, plus one.
  189. for ($i = 0; $i <= $c; $i++) {
  190. $grouping = !empty($this->options['grouping'][$i]) ? $this->options['grouping'][$i] : array();
  191. $grouping += array('field' => '', 'rendered' => TRUE, 'rendered_strip' => FALSE);
  192. $form['grouping'][$i]['field'] = array(
  193. '#type' => 'select',
  194. '#title' => t('Grouping field Nr.@number', array('@number' => $i + 1)),
  195. '#options' => $options,
  196. '#default_value' => $grouping['field'],
  197. '#description' => t('You may optionally specify a field by which to group the records. Leave blank to not group.'),
  198. );
  199. $form['grouping'][$i]['rendered'] = array(
  200. '#type' => 'checkbox',
  201. '#title' => t('Use rendered output to group rows'),
  202. '#default_value' => $grouping['rendered'],
  203. '#description' => t('If enabled the rendered output of the grouping field is used to group the rows.'),
  204. '#dependency' => array(
  205. 'edit-style-options-grouping-' . $i . '-field' => array_keys($field_labels),
  206. )
  207. );
  208. $form['grouping'][$i]['rendered_strip'] = array(
  209. '#type' => 'checkbox',
  210. '#title' => t('Remove tags from rendered output'),
  211. '#default_value' => $grouping['rendered_strip'],
  212. '#dependency' => array(
  213. 'edit-style-options-grouping-' . $i . '-field' => array_keys($field_labels),
  214. )
  215. );
  216. }
  217. }
  218. }
  219. if ($this->uses_row_class()) {
  220. $form['row_class'] = array(
  221. '#title' => t('Row class'),
  222. '#description' => t('The class to provide on each row.'),
  223. '#type' => 'textfield',
  224. '#default_value' => $this->options['row_class'],
  225. );
  226. if ($this->uses_fields()) {
  227. $form['row_class']['#description'] .= ' ' . t('You may use field tokens from as per the "Replacement patterns" used in "Rewrite the output of this field" for all fields.');
  228. }
  229. $form['default_row_class'] = array(
  230. '#title' => t('Add views row classes'),
  231. '#description' => t('Add the default row classes like views-row-1 to the output. You can use this to quickly reduce the amount of markup the view provides by default, at the cost of making it more difficult to apply CSS.'),
  232. '#type' => 'checkbox',
  233. '#default_value' => $this->options['default_row_class'],
  234. );
  235. $form['row_class_special'] = array(
  236. '#title' => t('Add striping (odd/even), first/last row classes'),
  237. '#description' => t('Add css classes to the first and last line, as well as odd/even classes for striping.'),
  238. '#type' => 'checkbox',
  239. '#default_value' => $this->options['row_class_special'],
  240. );
  241. }
  242. if (!$this->uses_fields() || !empty($this->options['uses_fields'])) {
  243. $form['uses_fields'] = array(
  244. '#type' => 'checkbox',
  245. '#title' => t('Force using fields'),
  246. '#description' => t('If neither the row nor the style plugin supports fields, this field allows to enable them, so you can for example use groupby.'),
  247. '#default_value' => $this->options['uses_fields'],
  248. );
  249. }
  250. }
  251. function options_validate(&$form, &$form_state) {
  252. // Don't run validation on style plugins without the grouping setting.
  253. if (isset($form_state['values']['style_options']['grouping'])) {
  254. // Don't save grouping if no field is specified.
  255. foreach ($form_state['values']['style_options']['grouping'] as $index => $grouping) {
  256. if (empty($grouping['field'])) {
  257. unset($form_state['values']['style_options']['grouping'][$index]);
  258. }
  259. }
  260. }
  261. }
  262. /**
  263. * Called by the view builder to see if this style handler wants to
  264. * interfere with the sorts. If so it should build; if it returns
  265. * any non-TRUE value, normal sorting will NOT be added to the query.
  266. */
  267. function build_sort() { return TRUE; }
  268. /**
  269. * Called by the view builder to let the style build a second set of
  270. * sorts that will come after any other sorts in the view.
  271. */
  272. function build_sort_post() { }
  273. /**
  274. * Allow the style to do stuff before each row is rendered.
  275. *
  276. * @param $result
  277. * The full array of results from the query.
  278. */
  279. function pre_render($result) {
  280. if (!empty($this->row_plugin)) {
  281. $this->row_plugin->pre_render($result);
  282. }
  283. }
  284. /**
  285. * Render the display in this style.
  286. */
  287. function render() {
  288. if ($this->uses_row_plugin() && empty($this->row_plugin)) {
  289. debug('views_plugin_style_default: Missing row plugin');
  290. return;
  291. }
  292. // Group the rows according to the grouping instructions, if specified.
  293. $sets = $this->render_grouping(
  294. $this->view->result,
  295. $this->options['grouping'],
  296. TRUE
  297. );
  298. return $this->render_grouping_sets($sets);
  299. }
  300. /**
  301. * Render the grouping sets.
  302. *
  303. * Plugins may override this method if they wish some other way of handling
  304. * grouping.
  305. *
  306. * @param $sets
  307. * Array containing the grouping sets to render.
  308. * @param $level
  309. * Integer indicating the hierarchical level of the grouping.
  310. *
  311. * @return string
  312. * Rendered output of given grouping sets.
  313. */
  314. function render_grouping_sets($sets, $level = 0) {
  315. $output = '';
  316. foreach ($sets as $set) {
  317. $row = reset($set['rows']);
  318. // Render as a grouping set.
  319. if (is_array($row) && isset($row['group'])) {
  320. $output .= theme(views_theme_functions('views_view_grouping', $this->view, $this->display),
  321. array(
  322. 'view' => $this->view,
  323. 'grouping' => $this->options['grouping'][$level],
  324. 'grouping_level' => $level,
  325. 'rows' => $set['rows'],
  326. 'title' => $set['group'])
  327. );
  328. }
  329. // Render as a record set.
  330. else {
  331. if ($this->uses_row_plugin()) {
  332. foreach ($set['rows'] as $index => $row) {
  333. $this->view->row_index = $index;
  334. $set['rows'][$index] = $this->row_plugin->render($row);
  335. }
  336. }
  337. $output .= theme($this->theme_functions(),
  338. array(
  339. 'view' => $this->view,
  340. 'options' => $this->options,
  341. 'grouping_level' => $level,
  342. 'rows' => $set['rows'],
  343. 'title' => $set['group'])
  344. );
  345. }
  346. }
  347. unset($this->view->row_index);
  348. return $output;
  349. }
  350. /**
  351. * Group records as needed for rendering.
  352. *
  353. * @param $records
  354. * An array of records from the view to group.
  355. * @param $groupings
  356. * An array of grouping instructions on which fields to group. If empty, the
  357. * result set will be given a single group with an empty string as a label.
  358. * @param $group_rendered
  359. * Boolean value whether to use the rendered or the raw field value for
  360. * grouping. If set to NULL the return is structured as before
  361. * Views 7.x-3.0-rc2. After Views 7.x-3.0 this boolean is only used if
  362. * $groupings is an old-style string or if the rendered option is missing
  363. * for a grouping instruction.
  364. * @return
  365. * The grouped record set.
  366. * A nested set structure is generated if multiple grouping fields are used.
  367. *
  368. * @code
  369. * array(
  370. * 'grouping_field_1:grouping_1' => array(
  371. * 'group' => 'grouping_field_1:content_1',
  372. * 'rows' => array(
  373. * 'grouping_field_2:grouping_a' => array(
  374. * 'group' => 'grouping_field_2:content_a',
  375. * 'rows' => array(
  376. * $row_index_1 => $row_1,
  377. * $row_index_2 => $row_2,
  378. * // ...
  379. * )
  380. * ),
  381. * ),
  382. * ),
  383. * 'grouping_field_1:grouping_2' => array(
  384. * // ...
  385. * ),
  386. * )
  387. * @endcode
  388. */
  389. function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
  390. // This is for backward compatibility, when $groupings was a string containing
  391. // the ID of a single field.
  392. if (is_string($groupings)) {
  393. $rendered = $group_rendered === NULL ? TRUE : $group_rendered;
  394. $groupings = array(array('field' => $groupings, 'rendered' => $rendered));
  395. }
  396. // Make sure fields are rendered
  397. $this->render_fields($this->view->result);
  398. $sets = array();
  399. if ($groupings) {
  400. foreach ($records as $index => $row) {
  401. // Iterate through configured grouping fields to determine the
  402. // hierarchically positioned set where the current row belongs to.
  403. // While iterating, parent groups, that do not exist yet, are added.
  404. $set = &$sets;
  405. foreach ($groupings as $info) {
  406. $field = $info['field'];
  407. $rendered = isset($info['rendered']) ? $info['rendered'] : $group_rendered;
  408. $rendered_strip = isset($info['rendered_strip']) ? $info['rendered_strip'] : FALSE;
  409. $grouping = '';
  410. $group_content = '';
  411. // Group on the rendered version of the field, not the raw. That way,
  412. // we can control any special formatting of the grouping field through
  413. // the admin or theme layer or anywhere else we'd like.
  414. if (isset($this->view->field[$field])) {
  415. $group_content = $this->get_field($index, $field);
  416. if ($this->view->field[$field]->options['label']) {
  417. $group_content = $this->view->field[$field]->options['label'] . ': ' . $group_content;
  418. }
  419. if ($rendered) {
  420. $grouping = $group_content;
  421. if ($rendered_strip) {
  422. $group_content = $grouping = strip_tags(htmlspecialchars_decode($group_content));
  423. }
  424. }
  425. else {
  426. $grouping = $this->get_field_value($index, $field);
  427. // Not all field handlers return a scalar value,
  428. // e.g. views_handler_field_field.
  429. if (!is_scalar($grouping)) {
  430. $grouping = md5(serialize($grouping));
  431. }
  432. }
  433. }
  434. // Create the group if it does not exist yet.
  435. if (empty($set[$grouping])) {
  436. $set[$grouping]['group'] = $group_content;
  437. $set[$grouping]['rows'] = array();
  438. }
  439. // Move the set reference into the row set of the group we just determined.
  440. $set = &$set[$grouping]['rows'];
  441. }
  442. // Add the row to the hierarchically positioned row set we just determined.
  443. $set[$index] = $row;
  444. }
  445. }
  446. else {
  447. // Create a single group with an empty grouping field.
  448. $sets[''] = array(
  449. 'group' => '',
  450. 'rows' => $records,
  451. );
  452. }
  453. // If this parameter isn't explicitly set modify the output to be fully
  454. // backward compatible to code before Views 7.x-3.0-rc2.
  455. // @TODO Remove this as soon as possible e.g. October 2020
  456. if ($group_rendered === NULL) {
  457. $old_style_sets = array();
  458. foreach ($sets as $group) {
  459. $old_style_sets[$group['group']] = $group['rows'];
  460. }
  461. $sets = $old_style_sets;
  462. }
  463. return $sets;
  464. }
  465. /**
  466. * Render all of the fields for a given style and store them on the object.
  467. *
  468. * @param $result
  469. * The result array from $view->result
  470. */
  471. function render_fields($result) {
  472. if (!$this->uses_fields()) {
  473. return;
  474. }
  475. if (!isset($this->rendered_fields)) {
  476. $this->rendered_fields = array();
  477. $this->view->row_index = 0;
  478. $keys = array_keys($this->view->field);
  479. // If all fields have a field::access FALSE there might be no fields, so
  480. // there is no reason to execute this code.
  481. if (!empty($keys)) {
  482. foreach ($result as $count => $row) {
  483. $this->view->row_index = $count;
  484. foreach ($keys as $id) {
  485. $this->rendered_fields[$count][$id] = $this->view->field[$id]->theme($row);
  486. }
  487. $this->row_tokens[$count] = $this->view->field[$id]->get_render_tokens(array());
  488. }
  489. }
  490. unset($this->view->row_index);
  491. }
  492. return $this->rendered_fields;
  493. }
  494. /**
  495. * Get a rendered field.
  496. *
  497. * @param $index
  498. * The index count of the row.
  499. * @param $field
  500. * The id of the field.
  501. */
  502. function get_field($index, $field) {
  503. if (!isset($this->rendered_fields)) {
  504. $this->render_fields($this->view->result);
  505. }
  506. if (isset($this->rendered_fields[$index][$field])) {
  507. return $this->rendered_fields[$index][$field];
  508. }
  509. }
  510. /**
  511. * Get the raw field value.
  512. *
  513. * @param $index
  514. * The index count of the row.
  515. * @param $field
  516. * The id of the field.
  517. */
  518. function get_field_value($index, $field) {
  519. $this->view->row_index = $index;
  520. $value = $this->view->field[$field]->get_value($this->view->result[$index]);
  521. unset($this->view->row_index);
  522. return $value;
  523. }
  524. function validate() {
  525. $errors = parent::validate();
  526. if ($this->uses_row_plugin()) {
  527. $plugin = $this->display->handler->get_plugin('row');
  528. if (empty($plugin)) {
  529. $errors[] = t('Style @style requires a row style but the row plugin is invalid.', array('@style' => $this->definition['title']));
  530. }
  531. else {
  532. $result = $plugin->validate();
  533. if (!empty($result) && is_array($result)) {
  534. $errors = array_merge($errors, $result);
  535. }
  536. }
  537. }
  538. return $errors;
  539. }
  540. function query() {
  541. parent::query();
  542. if (isset($this->row_plugin)) {
  543. $this->row_plugin->query();
  544. }
  545. }
  546. }
  547. /**
  548. * @}
  549. */