views_row.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @file
  4. * Allow a view context to display individual rows.
  5. */
  6. $plugin = array(
  7. 'title' => t('View row'),
  8. 'category' => t('View context'),
  9. 'icon' => 'icon_views_page.png',
  10. 'description' => t('Display all or a specific amount of rows from a loaded view context.'),
  11. 'required context' => new ctools_context_required(t('View'), 'view'),
  12. 'defaults' => array(
  13. 'rows' => array(),
  14. 'use_fields' => array(),
  15. 'fields' => array(),
  16. ),
  17. 'add form' => array(
  18. 'views_content_views_row_content_type_edit_form' => t('Select context'),
  19. 'views_content_views_row_edit' => t('Configure rows'),
  20. ),
  21. 'edit form' => array(
  22. 'views_content_views_row_content_type_edit_form' => t('Select context'),
  23. 'views_content_views_row_edit' => t('Configure rows'),
  24. ),
  25. );
  26. /**
  27. * Render the node_terms content type.
  28. */
  29. function views_content_views_row_content_type_render($subtype, $conf, $panel_args, $context) {
  30. if (empty($context) || empty($context->data)) {
  31. return;
  32. }
  33. // Build the content type block.
  34. $block = new stdClass();
  35. $block->module = 'views_row';
  36. $block->delta = $context->argument;
  37. $block->title = '';
  38. $block->content = '';
  39. // This guarantees the view is rendered normally which must happen.
  40. $view = views_content_context_get_view($context);
  41. $output = views_content_context_get_output($context);
  42. $sets = array();
  43. $plugin = $view->style_plugin;
  44. // If all rows have to be displayed then simply get the key of all rows.
  45. $row_indexes = array();
  46. if (empty($conf['rows'])) {
  47. if (is_array($output['rows'])) {
  48. $row_indexes = array_keys($output['rows']);
  49. }
  50. }
  51. else {
  52. // If a subset of rows is requested collect the list of row keys.
  53. foreach ($conf['rows'] as $index) {
  54. $row_indexes[] = $index - 1;
  55. }
  56. }
  57. if (empty($conf['use_fields']) || empty($plugin->row_plugin)) {
  58. foreach ($row_indexes as $row_index) {
  59. if (isset($output['rows'][$row_index])) {
  60. $sets[$plugin->groups[$row_index]][$row_index] = $output['rows'][$row_index];
  61. }
  62. }
  63. }
  64. else {
  65. // If we're using specific fields, go through and poke the 'exclude' flag.
  66. foreach ($view->field as $id => $field) {
  67. $view->field[$id]->options['exclude'] = empty($conf['fields'][$id]);
  68. }
  69. // Rerender just the rows we need.
  70. foreach ($row_indexes as $row_index) {
  71. $view->row_index = $row_index;
  72. if (!empty($view->result[$view->row_index])) {
  73. $sets[$plugin->groups[$view->row_index]][$view->row_index] = $plugin->row_plugin->render($view->result[$view->row_index]);
  74. }
  75. unset($view->row_index);
  76. }
  77. }
  78. foreach ($sets as $title => $rows) {
  79. $block->content .= theme($plugin->theme_functions(),
  80. array(
  81. 'view' => $view,
  82. 'options' => $plugin->options,
  83. 'rows' => $rows,
  84. 'title' => $title,
  85. )
  86. );
  87. }
  88. return $block;
  89. }
  90. function views_content_views_row_content_type_edit_form($form, &$form_state) {
  91. // This form does nothing; it exists to let the main form select the view context.
  92. return $form;
  93. }
  94. function views_content_views_row_content_type_edit_form_submit($form, &$form_state) {
  95. }
  96. function views_content_views_row_edit($form, &$form_state) {
  97. $conf = $form_state['conf'];
  98. $contexts = $form_state['contexts'];
  99. if (empty($contexts[$conf['context']])) {
  100. $form['markup'] = array('#markup' => '<p>' . t('Invalid context selected.') . '</p>');
  101. return $form;
  102. }
  103. if (!isset($contexts[$conf['context']]->argument)) {
  104. $name = $contexts[$conf['context']]->placeholder['conf']['name'];
  105. list($plugin, $view_data) = explode(':', $name);
  106. list($view_name, $display_id) = explode('-', $view_data);
  107. }
  108. else {
  109. $view_data = $contexts[$conf['context']]->argument;
  110. list($view_name, $display_id) = explode(':', $view_data);
  111. }
  112. $contexts[$conf['context']]->data['name'] = $view_name;
  113. $contexts[$conf['context']]->data['display'] = $display_id;
  114. $view = views_content_context_get_view($contexts[$conf['context']]);
  115. if (empty($view)) {
  116. $form['markup'] = array('#markup' => '<p>' . t('Context contains an invalid view.') . '</p>');
  117. return $form;
  118. }
  119. ctools_include('dependent');
  120. $form['limit_rows'] = array(
  121. '#type' => 'checkbox',
  122. '#title' => t('Limit rows'),
  123. '#default_value' => (int) !empty($conf['rows']),
  124. );
  125. $view->init_pager();
  126. $rows = $view->get_items_per_page();
  127. if (!empty($rows)) {
  128. foreach (range(1, $rows) as $row) {
  129. $options[$row] = t('Row @number', array('@number' => $row));
  130. }
  131. $form['rows'] = array(
  132. '#type' => 'checkboxes',
  133. '#title' => t('Select rows'),
  134. '#options' => $options,
  135. '#default_value' => $conf['rows'],
  136. '#dependency' => array('edit-limit-rows' => array(TRUE)),
  137. );
  138. }
  139. else {
  140. $form['rows'] = array('#markup' => '<p>' . t('The view must have a maximum number of items set to use this setting.') . '</p>');
  141. return $form;
  142. }
  143. if ($view->display_handler->uses_fields()) {
  144. $form['use_fields'] = array(
  145. '#type' => 'checkbox',
  146. '#title' => t('Display specific fields'),
  147. '#default_value' => $conf['use_fields'],
  148. );
  149. $form['fields'] = array(
  150. '#type' => 'checkboxes',
  151. '#options' => $view->display_handler->get_field_labels(),
  152. '#default_value' => $conf['fields'],
  153. '#prefix' => '<div id="edit-fields-wrapper"><div id="edit-fields">',
  154. '#suffix' => '</div></div>',
  155. '#dependency' => array('edit-use-fields' => array(TRUE)),
  156. );
  157. }
  158. return $form;
  159. }
  160. function views_content_views_row_edit_validate(&$form, &$form_state) {
  161. }
  162. function views_content_views_row_edit_submit(&$form, &$form_state) {
  163. $form_state['conf']['rows'] = array_filter($form_state['values']['rows']);
  164. $form_state['conf']['use_fields'] = $form_state['values']['use_fields'];
  165. $form_state['conf']['fields'] = array_filter($form_state['values']['fields']);
  166. }
  167. function views_content_views_row_content_type_admin_info($subtype, $conf, $contexts) {
  168. // Go through this route to make sure we catch changes in configuration
  169. // that can happen.
  170. $plugin = ctools_get_content_type('views_row');
  171. $context = ctools_content_select_context($plugin, $subtype, $conf, $contexts);
  172. $block = new stdClass();
  173. $block->title = t('Row information');
  174. if (!empty($conf['use_fields'])) {
  175. $display_fields = array();
  176. $view = views_content_context_get_view($context);
  177. if (empty($view)) {
  178. $block->title = t('Broken view');
  179. return $block;
  180. }
  181. $fields = $view->display_handler->get_field_labels();
  182. foreach ($conf['fields'] as $field) {
  183. if (!empty($fields[$field])) {
  184. $display_fields[$field] = '"<em>' . check_plain($fields[$field]) . '</em>"';
  185. }
  186. }
  187. if ($display_fields) {
  188. $block->content = t('Displaying: !fields', array('!fields' => implode(', ', $display_fields)));
  189. }
  190. else {
  191. $block->content = t('Displaying no fields due to misconfiguration.');
  192. }
  193. }
  194. else {
  195. $block->content = t('Displaying the configured row.');
  196. }
  197. return $block;
  198. }
  199. function views_content_views_row_content_type_admin_title($subtype, $conf, $context) {
  200. $rows = array_filter($conf['rows']);
  201. $rows = empty($rows) ? t('Show all') : implode(', ', $rows);
  202. return format_plural(count($rows),
  203. '"@context" row @rows',
  204. '"@context" rows @rows',
  205. array('@context' => $context->identifier, '@rows' => $rows)
  206. );
  207. }