panels_views_plugin_row_fields.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @file
  4. * Contains the base row style plugin.
  5. */
  6. /**
  7. * The basic 'fields' row plugin.
  8. *
  9. * This displays fields one after another, giving options for inline
  10. * or not.
  11. *
  12. * @ingroup views_row_plugins
  13. */
  14. class panels_views_plugin_row_fields extends views_plugin_row_fields {
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['layout'] = array('default' => 'twocol');
  18. $options['regions'] = array('default' => array());
  19. return $options;
  20. }
  21. /**
  22. * Provide a form for setting options.
  23. */
  24. function options_form(&$form, &$form_state) {
  25. parent::options_form($form, $form_state);
  26. ctools_include('plugins', 'panels');
  27. $layouts = panels_get_layouts();
  28. $options = array();
  29. foreach ($layouts as $name => $layout) {
  30. if (empty($layout['builder'])) {
  31. $options[$name] = $layout['title'];
  32. }
  33. if ($name == $this->options['layout']) {
  34. $current_layout = $layout;
  35. }
  36. }
  37. $form['layout'] = array(
  38. '#prefix' => '<div class="container-inline">',
  39. '#type' => 'select',
  40. '#options' => $options,
  41. '#title' => t('Panel layout'),
  42. '#default_value' => $this->options['layout'],
  43. );
  44. $form['change'] = array(
  45. '#type' => 'submit',
  46. '#value' => t('Change'),
  47. '#submit' => array('panels_change_layout_button'),
  48. '#suffix' => '</div>',
  49. );
  50. if (!empty($current_layout)) {
  51. $fields = $this->display->handler->get_field_labels();
  52. $regions = panels_get_regions($current_layout, panels_new_display());
  53. foreach ($fields as $id => $title) {
  54. $form['regions'][$id] = array(
  55. '#type' => 'select',
  56. '#title' => $title,
  57. '#options' => $regions,
  58. );
  59. if (!empty($this->options['regions'][$id]) && !empty($regions[$this->options['regions'][$id]])) {
  60. $form['regions'][$id]['#default_value'] = $this->options['regions'][$id];
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Perform any necessary changes to the form values prior to storage.
  67. * There is no need for this function to actually store the data.
  68. */
  69. function options_submit(&$form, &$form_state) {
  70. $form_state['values']['row_options']['inline'] = array_filter($form_state['values']['row_options']['inline']);
  71. }
  72. /**
  73. * Render a row object. This usually passes through to a theme template
  74. * of some form, but not always.
  75. */
  76. function render($row) {
  77. ctools_include('plugins', 'panels');
  78. $layout = panels_get_layout($this->options['layout']);
  79. if (!$layout) {
  80. // Fall back to normal behavior if the layout is somehow invalid. This
  81. // can happen if the layout was removed, for example.
  82. return theme($this->theme_functions(), array('view' => $this->view, 'options' => $this->options, 'row' => $row));
  83. }
  84. // Store a backup copy of the array because we're going to be screwing
  85. // with this a lot.
  86. $fields = $this->view->field;
  87. unset($this->view->field);
  88. $meta = 'standard';
  89. // This row style gets run many times; only run this code once.
  90. if (empty($this->region_fields)) {
  91. $this->region_fields = array();
  92. $regions = panels_get_regions($layout, panels_new_display());
  93. // Ensure each region has an empty array.
  94. foreach ($regions as $region_id => $name) {
  95. if (empty($default_region)) {
  96. $default_region = $region_id;
  97. }
  98. $this->region_fields[$region_id] = array();
  99. }
  100. // Go through all our fields and place them in regions according to the
  101. // settings.
  102. foreach ($fields as $id => $field) {
  103. $region_id = ''; // ensure we don't accidentlly use the last field's region.
  104. if (!empty($this->options['regions'][$id]) && !empty($regions[$this->options['regions'][$id]])) {
  105. $region_id = $this->options['regions'][$id];
  106. }
  107. else {
  108. // Fallback to putting unknown fields into the first region.
  109. $region_id = $default_region;
  110. }
  111. // Ensure this works in PHP4 by keeping the reference.
  112. $this->region_fields[$region_id][$id] = &$fields[$id];
  113. }
  114. // We don't need to set 'inline' for every record, so we do it inside
  115. // this loop. We do need to set inline if we are in the live preview
  116. // so that the CSS will get transmitted via javascript:
  117. $meta = !empty($this->view->live_preview) ? 'inline' : 'standard';
  118. }
  119. // Now that we have distributed our fields, go through the regions and
  120. // render them into the content array.
  121. foreach ($this->region_fields as $region_id => $fields_list) {
  122. $this->view->field = $fields_list;
  123. $content[$region_id] = theme($this->theme_functions(), array('view' => $this->view, 'options' => $this->options, 'row' => $row));
  124. }
  125. // Restore our $fields array.
  126. $this->view->field = $fields;
  127. // Now that we have a rendered content array, render it.
  128. return panels_print_layout($layout, $content, $meta);
  129. }
  130. }
  131. /**
  132. * Override handler for views_ui_edit_display_form.
  133. */
  134. function panels_change_layout_button($form, &$form_state) {
  135. $display = &$form_state['view']->display[$form_state['display_id']];
  136. $display->handler->options_submit($form, $form_state);
  137. views_ui_cache_set($form_state['view']);
  138. $form_state['rerender'] = TRUE;
  139. $form_state['rebuild'] = TRUE;
  140. }