Index: views_cloud_plugin_summary_style_cloud.inc =================================================================== --- views_cloud_plugin_summary_style_cloud.inc (revision 936) +++ views_cloud_plugin_summary_style_cloud.inc (working copy) @@ -7,6 +7,7 @@ function option_definition() { $options = parent::option_definition(); $options['count'] = array('default' => FALSE); + $options['sort'] = array('default' => 'desc'); $options['randomize'] = array('default' => TRUE); $options['sizes'] = array('default' => '7'); $options['font_size'] = array('default' => ''); @@ -17,9 +18,20 @@ function options_form(&$form, &$form_state) { parent::options_form($form, $form_state); + $form['sort'] = array( + '#type' => 'radios', + '#title' => t('Override sort to show'), + '#options' => array( + 'desc' => t('Most popular items by count'), + 'random' => t('Random items'), + ), + '#description' => t('This will ignore all normal sorting and sort instead by the item count.'), + '#default_value' => $this->options['sort'], + ); + $form['randomize'] = array( '#type' => 'checkbox', - '#title' => t('Randomize the order of items'), + '#title' => t('Randomize the order of shown items'), '#description' => t("This setting respects the View's sort order when limiting large paged lists, but shuffles each list of items when displayed on the page."), '#default_value' => $this->options['randomize'], ); @@ -60,4 +72,36 @@ '#default_value' => $this->options['algorithm'], ); } + + function query() { + parent::query(); + + // Add a filter to include a certain count threshold. + if ($this->options['count_filter']) { + $this->view->query->add_where(0, 'num_records > %d', $this->options['count_filter']); + } + + // Override normal sorting and sort by number of results. + if ($this->options['sort'] == 'random') { + global $db_type; + switch ($db_type) { + case 'mysql': + case 'mysqli': + $formula = 'RAND()'; + break; + case 'pgsql': + $formula = 'RANDOM()'; + break; + } + if (!empty($formula)) { + $this->view->query->orderby = array(); + $this->view->query->add_field(NULL, $formula, 'random', array('aggregate' => TRUE)); + $this->view->query->orderby[] = 'random DESC'; + } + } + elseif ($this->options['sort']) { + $this->view->query->orderby = array(); + $this->view->query->add_orderby(NULL, NULL, $this->options['sort'], 'num_records'); + } + } }