views_plugin_style.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 = array_merge($classes, explode(' ', 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_clean_css_identifier($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. '#description' => t('Some modules add HTML to the rendered output and prevent the rows from grouping correctly. Stripping the HTML tags should correct this.'),
  213. '#dependency' => array(
  214. 'edit-style-options-grouping-' . $i . '-field' => array_keys($field_labels),
  215. )
  216. );
  217. }
  218. }
  219. }
  220. if ($this->uses_row_class()) {
  221. $form['row_class'] = array(
  222. '#title' => t('Row class'),
  223. '#description' => t('The class to provide on each row.'),
  224. '#type' => 'textfield',
  225. '#default_value' => $this->options['row_class'],
  226. );
  227. if ($this->uses_fields()) {
  228. $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.');
  229. }
  230. $form['default_row_class'] = array(
  231. '#title' => t('Add views row classes'),
  232. '#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.'),
  233. '#type' => 'checkbox',
  234. '#default_value' => $this->options['default_row_class'],
  235. );
  236. $form['row_class_special'] = array(
  237. '#title' => t('Add striping (odd/even), first/last row classes'),
  238. '#description' => t('Add css classes to the first and last line, as well as odd/even classes for striping.'),
  239. '#type' => 'checkbox',
  240. '#default_value' => $this->options['row_class_special'],
  241. );
  242. }
  243. if (!$this->uses_fields() || !empty($this->options['uses_fields'])) {
  244. $form['uses_fields'] = array(
  245. '#type' => 'checkbox',
  246. '#title' => t('Force using fields'),
  247. '#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.'),
  248. '#default_value' => $this->options['uses_fields'],
  249. );
  250. }
  251. }
  252. function options_validate(&$form, &$form_state) {
  253. // Don't run validation on style plugins without the grouping setting.
  254. if (isset($form_state['values']['style_options']['grouping'])) {
  255. // Don't save grouping if no field is specified.
  256. foreach ($form_state['values']['style_options']['grouping'] as $index => $grouping) {
  257. if (empty($grouping['field'])) {
  258. unset($form_state['values']['style_options']['grouping'][$index]);
  259. }
  260. }
  261. }
  262. }
  263. /**
  264. * Called by the view builder to see if this style handler wants to
  265. * interfere with the sorts. If so it should build; if it returns
  266. * any non-TRUE value, normal sorting will NOT be added to the query.
  267. */
  268. function build_sort() { return TRUE; }
  269. /**
  270. * Called by the view builder to let the style build a second set of
  271. * sorts that will come after any other sorts in the view.
  272. */
  273. function build_sort_post() { }
  274. /**
  275. * Allow the style to do stuff before each row is rendered.
  276. *
  277. * @param $result
  278. * The full array of results from the query.
  279. */
  280. function pre_render($result) {
  281. if (!empty($this->row_plugin)) {
  282. $this->row_plugin->pre_render($result);
  283. }
  284. }
  285. /**
  286. * Render the display in this style.
  287. */
  288. function render() {
  289. if ($this->uses_row_plugin() && empty($this->row_plugin)) {
  290. debug('views_plugin_style_default: Missing row plugin');
  291. return;
  292. }
  293. // Group the rows according to the grouping instructions, if specified.
  294. $sets = $this->render_grouping(
  295. $this->view->result,
  296. $this->options['grouping'],
  297. TRUE
  298. );
  299. return $this->render_grouping_sets($sets);
  300. }
  301. /**
  302. * Render the grouping sets.
  303. *
  304. * Plugins may override this method if they wish some other way of handling
  305. * grouping.
  306. *
  307. * @param $sets
  308. * Array containing the grouping sets to render.
  309. * @param $level
  310. * Integer indicating the hierarchical level of the grouping.
  311. *
  312. * @return string
  313. * Rendered output of given grouping sets.
  314. */
  315. function render_grouping_sets($sets, $level = 0) {
  316. $output = '';
  317. foreach ($sets as $set) {
  318. $row = reset($set['rows']);
  319. // Render as a grouping set.
  320. if (is_array($row) && isset($row['group'])) {
  321. $output .= theme(views_theme_functions('views_view_grouping', $this->view, $this->display),
  322. array(
  323. 'view' => $this->view,
  324. 'grouping' => $this->options['grouping'][$level],
  325. 'grouping_level' => $level,
  326. 'rows' => $set['rows'],
  327. 'title' => $set['group'])
  328. );
  329. }
  330. // Render as a record set.
  331. else {
  332. if ($this->uses_row_plugin()) {
  333. foreach ($set['rows'] as $index => $row) {
  334. $this->view->row_index = $index;
  335. $set['rows'][$index] = $this->row_plugin->render($row);
  336. }
  337. }
  338. $output .= theme($this->theme_functions(),
  339. array(
  340. 'view' => $this->view,
  341. 'options' => $this->options,
  342. 'grouping_level' => $level,
  343. 'rows' => $set['rows'],
  344. 'title' => $set['group'])
  345. );
  346. }
  347. }
  348. unset($this->view->row_index);
  349. return $output;
  350. }
  351. /**
  352. * Group records as needed for rendering.
  353. *
  354. * @param $records
  355. * An array of records from the view to group.
  356. * @param $groupings
  357. * An array of grouping instructions on which fields to group. If empty, the
  358. * result set will be given a single group with an empty string as a label.
  359. * @param $group_rendered
  360. * Boolean value whether to use the rendered or the raw field value for
  361. * grouping. If set to NULL the return is structured as before
  362. * Views 7.x-3.0-rc2. After Views 7.x-3.0 this boolean is only used if
  363. * $groupings is an old-style string or if the rendered option is missing
  364. * for a grouping instruction.
  365. * @return
  366. * The grouped record set.
  367. * A nested set structure is generated if multiple grouping fields are used.
  368. *
  369. * @code
  370. * array(
  371. * 'grouping_field_1:grouping_1' => array(
  372. * 'group' => 'grouping_field_1:content_1',
  373. * 'rows' => array(
  374. * 'grouping_field_2:grouping_a' => array(
  375. * 'group' => 'grouping_field_2:content_a',
  376. * 'rows' => array(
  377. * $row_index_1 => $row_1,
  378. * $row_index_2 => $row_2,
  379. * // ...
  380. * )
  381. * ),
  382. * ),
  383. * ),
  384. * 'grouping_field_1:grouping_2' => array(
  385. * // ...
  386. * ),
  387. * )
  388. * @endcode
  389. */
  390. function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
  391. // This is for backward compatibility, when $groupings was a string containing
  392. // the ID of a single field.
  393. if (is_string($groupings)) {
  394. $rendered = $group_rendered === NULL ? TRUE : $group_rendered;
  395. $groupings = array(array('field' => $groupings, 'rendered' => $rendered));
  396. }
  397. // Make sure fields are rendered
  398. $this->render_fields($this->view->result);
  399. $sets = array();
  400. if ($groupings) {
  401. foreach ($records as $index => $row) {
  402. // Iterate through configured grouping fields to determine the
  403. // hierarchically positioned set where the current row belongs to.
  404. // While iterating, parent groups, that do not exist yet, are added.
  405. $set = &$sets;
  406. foreach ($groupings as $info) {
  407. $field = $info['field'];
  408. $rendered = isset($info['rendered']) ? $info['rendered'] : $group_rendered;
  409. $rendered_strip = isset($info['rendered_strip']) ? $info['rendered_strip'] : FALSE;
  410. $grouping = '';
  411. $group_content = '';
  412. // Group on the rendered version of the field, not the raw. That way,
  413. // we can control any special formatting of the grouping field through
  414. // the admin or theme layer or anywhere else we'd like.
  415. if (isset($this->view->field[$field])) {
  416. $group_content = $this->get_field($index, $field);
  417. if ($this->view->field[$field]->options['label']) {
  418. $group_content = $this->view->field[$field]->options['label'] . ': ' . $group_content;
  419. }
  420. if ($rendered) {
  421. $grouping = $group_content;
  422. if ($rendered_strip) {
  423. $group_content = $grouping = strip_tags(htmlspecialchars_decode($group_content));
  424. }
  425. }
  426. else {
  427. $grouping = $this->get_field_value($index, $field);
  428. // Not all field handlers return a scalar value,
  429. // e.g. views_handler_field_field.
  430. if (!is_scalar($grouping)) {
  431. $grouping = md5(serialize($grouping));
  432. }
  433. }
  434. }
  435. // Create the group if it does not exist yet.
  436. if (empty($set[$grouping])) {
  437. $set[$grouping]['group'] = $group_content;
  438. $set[$grouping]['rows'] = array();
  439. }
  440. // Move the set reference into the row set of the group we just determined.
  441. $set = &$set[$grouping]['rows'];
  442. }
  443. // Add the row to the hierarchically positioned row set we just determined.
  444. $set[$index] = $row;
  445. }
  446. }
  447. else {
  448. // Create a single group with an empty grouping field.
  449. $sets[''] = array(
  450. 'group' => '',
  451. 'rows' => $records,
  452. );
  453. }
  454. // If this parameter isn't explicitly set modify the output to be fully
  455. // backward compatible to code before Views 7.x-3.0-rc2.
  456. // @TODO Remove this as soon as possible e.g. October 2020
  457. if ($group_rendered === NULL) {
  458. $old_style_sets = array();
  459. foreach ($sets as $group) {
  460. $old_style_sets[$group['group']] = $group['rows'];
  461. }
  462. $sets = $old_style_sets;
  463. }
  464. return $sets;
  465. }
  466. /**
  467. * Render all of the fields for a given style and store them on the object.
  468. *
  469. * @param $result
  470. * The result array from $view->result
  471. */
  472. function render_fields($result) {
  473. if (!$this->uses_fields()) {
  474. return;
  475. }
  476. if (!isset($this->rendered_fields)) {
  477. $this->rendered_fields = array();
  478. $this->view->row_index = 0;
  479. $keys = array_keys($this->view->field);
  480. // If all fields have a field::access FALSE there might be no fields, so
  481. // there is no reason to execute this code.
  482. if (!empty($keys)) {
  483. foreach ($result as $count => $row) {
  484. $this->view->row_index = $count;
  485. foreach ($keys as $id) {
  486. $this->rendered_fields[$count][$id] = $this->view->field[$id]->theme($row);
  487. }
  488. $this->row_tokens[$count] = $this->view->field[$id]->get_render_tokens(array());
  489. }
  490. }
  491. unset($this->view->row_index);
  492. }
  493. return $this->rendered_fields;
  494. }
  495. /**
  496. * Get a rendered field.
  497. *
  498. * @param $index
  499. * The index count of the row.
  500. * @param $field
  501. * The id of the field.
  502. */
  503. function get_field($index, $field) {
  504. if (!isset($this->rendered_fields)) {
  505. $this->render_fields($this->view->result);
  506. }
  507. if (isset($this->rendered_fields[$index][$field])) {
  508. return $this->rendered_fields[$index][$field];
  509. }
  510. }
  511. /**
  512. * Get the raw field value.
  513. *
  514. * @param $index
  515. * The index count of the row.
  516. * @param $field
  517. * The id of the field.
  518. */
  519. function get_field_value($index, $field) {
  520. $this->view->row_index = $index;
  521. $value = $this->view->field[$field]->get_value($this->view->result[$index]);
  522. unset($this->view->row_index);
  523. return $value;
  524. }
  525. function validate() {
  526. $errors = parent::validate();
  527. if ($this->uses_row_plugin()) {
  528. $plugin = $this->display->handler->get_plugin('row');
  529. if (empty($plugin)) {
  530. $errors[] = t('Style @style requires a row style but the row plugin is invalid.', array('@style' => $this->definition['title']));
  531. }
  532. else {
  533. $result = $plugin->validate();
  534. if (!empty($result) && is_array($result)) {
  535. $errors = array_merge($errors, $result);
  536. }
  537. }
  538. }
  539. return $errors;
  540. }
  541. function query() {
  542. parent::query();
  543. if (isset($this->row_plugin)) {
  544. $this->row_plugin->query();
  545. }
  546. }
  547. }
  548. /**
  549. * @}
  550. */