You are here

private function TagService::buildWeightedTags in TagCloud 8

Same name and namespace in other branches
  1. 2.0.x src/TagService.php \Drupal\tagclouds\TagService::buildWeightedTags()
  2. 1.0.x src/TagService.php \Drupal\tagclouds\TagService::buildWeightedTags()

Returns an array with weighted tags.

This is the hard part. People with better ideas are very very welcome to send these to ber@webschuur.com. Distribution is one thing that needs attention.

Parameters

$tags: A list of <em>objects</em> with the following attributes: $tag->count, $tag->tid, $tag->name and $tag->vid. Each Tag will be calculated and turned into a tag. Refer to tagclouds_get__tags() for an example.

int $steps: (optional) The amount of tag-sizes you will be using. If you give "12" you still get six different "weights". Defaults to 6.

Return value

array An <em>unordered</em> array with tags-objects, containing the attribute $tag->weight.

1 call to TagService::buildWeightedTags()
TagService::getTags in src/TagService.php
Return an array of tags.

File

src/TagService.php, line 167

Class

TagService
Class TagService.

Namespace

Drupal\tagclouds

Code

private function buildWeightedTags($tags, $steps = 6) {

  // Find minimum and maximum log-count. By our MatheMagician Steven Wittens
  // aka UnConeD.
  $tags_tmp = [];
  $min = 1000000000.0;
  $max = -1000000000.0;
  foreach ($tags as $id => $tag) {
    $tag->number_of_posts = $tag->count;
    $tag->weightcount = log($tag->count);
    $min = min($min, $tag->weightcount);
    $max = max($max, $tag->weightcount);
    $tags_tmp[$id] = $tag;
  }

  // Note: we need to ensure the range is slightly too large to make sure even
  // the largest element is rounded down.
  $range = max(0.01, $max - $min) * 1.0001;
  foreach ($tags_tmp as $key => $value) {
    $tags[$key]->weight = 1 + floor($steps * ($value->weightcount - $min) / $range);
  }
  return $tags;
}