Add a block to show top searches the way tagadelic shows tags. The more a phrase is searched for, the bigger it shows.

Comments

ShaunPatrick’s picture

sweet!

ShaunPatrick’s picture

Where can i find the break that its putting after every word?

z.stolar’s picture

which break are you referring to?

tjodolv’s picture

I could not find this feature in RC3. Not sure if you already have it working in a dev version, but I made a preprocess function for my theme to do this, based on code from the Tagadelic module.

Here is my preprocess function, in case you want to take a look at the implementation.

<?php
function MYTHEME_top_searches_block($top_searches) {
  // The calculations for weighting items. Shamelessly borrowed from tagadelic.module (lines 238-265), 
  // all credit should go to the maintainer (ber) and MatheMagician (Steve Wittens) of that module.

  $steps = 5; // The number of levels we are weighting with.
  $searches = array();

  $min = 1e9; // Here we use natural logarithms as a basis for calculating weight. I do not know why.
  $max = -1e9;
  foreach ($top_searches as $search) {
    $search->number_of_searches = $search->counter; // We need to store the actual search counter here,
    $search->counter = log($search->counter);       // because here we overwrite the original with log().
    $min = min($min, $search->counter);             // These lines figure out if we have a new min or max.
    $max = max($max, $search->counter);
    $searches[] = $search;                          // And finally store the information in our array.
  }

  // Note: we need to ensure the range is slightly too large to make sure even
  // the largest element is rounded down.
  $range = max(.01, $max - $min) * 1.0001;          // Math magic, do not ask me...
  
  $searchcloud = array();                           // A new array - this will be used to create the output.
  foreach ($searches as $key => $value) {           // Weight calculation and addition to our new array.
    $searchcloud[] = array('weight' => 1 + floor($steps * ($value->counter - $min) / $range), 'item' => $value);
  }

  // Get the variable setting of whether or not to display the actual search count.
  $show_counters = variable_get('top_searches_show_counters', 0); 

  // Set up the actual output array and then go through our prepared array with the weights.
  $items = array();
  foreach ($searchcloud as $key => $q) {
    $level = $q['weight']; // This is the key for adding a weight class to the links.
    $items[] = l($q['item']->q, 
                 'search/node/' . $q['item']->q, 
                 array('attributes' => array('class' => "search-cloud-item level-$level"))) 
                 . ($show_counters ? ' (' . $q['item']->number_of_searches . ')' : '');
  }

  return theme('item_list', $items, NULL, 'ul', array('class' => 'search-cloud'));

} // function MYTHEME_top_searches_block
?>
carok’s picture

tjodolv, could you please give me an example of using the code above?

Which file should this function be placed in and can you please give me an example of how and where this will be called from?

I thought it was a case of replacing the "theme_top_searches_block" code with what you have above but that is not changing how the block is displayed, I'm pretty new to Drupal so any help is very much appreciated.

Thanks!!!

tjodolv’s picture

This function is a template preprocess function, and those functions should be placed in the template.php file of the current theme you are using. If you are using Garland, go to the /themes/garland/template.php file and add this at the end. If you are using a custom theme, the file should be at /sites/all/themes/YOURTHEMENAME/template.php.

edit:
You should never replace any code in a module (and especially not in Drupal core). This is one of the reasons we have the PHPTemplate theme engine, to do these kinds of "rewrites" to change the look and feel of the site. If you need a module's functionality changed, you either need a different or additional module, or a patch with the additional functionality should be submitted to the module in question.

Balbo’s picture

+1

Grayside’s picture

Just discovered this module and am wandering the issue queue to learn it's status.

Rather than creating a special template file, it makes more sense to me to expose the top searches data to Views. At that point, you can take advantage of all the display mechanisms and module integrations that Views has to offer, including Views Cloud and Tagadelic Views.

aalireza’s picture

Version: 6.x-1.0-rc3 » 7.x-1.0
Assigned: Unassigned » aalireza

how can do this for drupal 7?

Phinkalily’s picture

I also want to have this feature for top searches. I also hope that top searches will have different display styles such as clouds and other.

Yet Another User’s picture

#10, I want the same for my website. Can anyone help?

Acertijo’s picture

+1