You are here

function tagclouds_get_tags in TagCloud 7

Function that gets the information from the database, passes it along to the weight builder and returns these weighted tags. Note that the tags are unordered at this stage, hence they need ordering either by calling our api or by your own ordering data.

Parameters

$vids: Vocabulary ids representing the vocabularies where you want the tags from.

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

$size: The number of tags that will be returned.

$display OPTIONAL: The type of display "style"=weighted,"count"=numbered display

Return value

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

3 calls to tagclouds_get_tags()
tagclouds_block_view in ./tagclouds.module
Implements hook_block_view().
tagclouds_page_chunk in ./tagclouds.module
Menu callback renders a tagclouds page.
tagclouds_page_list in ./tagclouds.module
Menu callback renders a tagclouds page with listed items: each vocabulary.

File

./tagclouds.module, line 227

Code

function tagclouds_get_tags($vids, $steps = 6, $size = 60, $display = NULL) {

  // Build the options so we can cache multiple versions.
  global $language;
  $options = implode('_', $vids) . '_' . $language->language . '_' . $steps . '_' . $size . "_" . $display;

  // Check if the cache exists.
  $cache_name = 'tagclouds_cache_' . $options;
  $cache = cache_get($cache_name, 'cache_page');
  $tags = array();

  // Make sure cache has data.
  if (isset($cache->data)) {
    $tags = $cache->data;
  }
  else {
    if (!is_array($vids) || count($vids) == 0) {
      return array();
    }
    $query = db_select('taxonomy_term_data', 'td');
    $query
      ->addExpression('COUNT(*)', 'count');
    $query
      ->fields('td', array(
      'tid',
      'vid',
      'name',
      'description',
    ));
    $query
      ->addExpression('max(n.nid)', 'nid');
    $query
      ->join('taxonomy_index', 'tn', 'td.tid = tn.tid');

    // $query->join('node', 'n', 'tn.nid = n.nid');
    $query
      ->innerJoin('node', 'n', 'tn.nid = n.nid');
    $query
      ->addTag('node_access');
    if (!module_exists('entity_translation')) {
      if (variable_get('tagclouds_language_separation', 0)) {
        $query
          ->condition('n.language', $language->language);

        /* site is using node translation rather than entity_translation
           this will return taxonomy terms for that node translation
           unfortunately this will not work well for bundles/content types that are
           (entity translation enabled /field translated) */
      }
    }
    else {

      // entity translation module is enabled
      if (variable_get('tagclouds_language_separation_radios', 0) == 'entity') {

        /* TODO: would be nice to enhance tagclouds so that a translation enabled field_data_field_example-term-reference (example)
           can be used as a language condition for tid rather than taxonomy_index.  This would work
           much better for entity translation due to the fact that some terms are spelled exactly the
           same in more than one language so a translation enabled term reference will work best for
           this scenario.  This will require adding interface code to allow users to select the
           term-reference fields they want to tagcloud rather than select a taxonomy vocabulary */

        /*if using entity_translation you must add a condition on the term language
          rather than the node language as node language only finds nodes with
          'source' language = $language.  Using term language is more appropriate
          for entity_translation scenarios*/
        $query
          ->condition('td.language', $language->language);
      }
      elseif (variable_get('tagclouds_language_separation_radios', 0) == 1) {
        $query
          ->condition('n.language', $language->language);
      }
    }
    $query
      ->condition('td.vid', $vids);
    $query
      ->condition('n.status', 1);
    $query
      ->groupBy('td.tid')
      ->groupBy('td.vid')
      ->groupBy('td.name');

    //->groupBy('tn.nid');
    $query
      ->groupBy('td.description HAVING COUNT(*) > 0');
    $query
      ->orderBy('COUNT(*)', 'DESC');
    if ($size > 0) {
      $query
        ->range(0, $size);
    }
    drupal_alter('tagclouds_get_tags_query', $query, $size);
    $result = $query
      ->execute();
    foreach ($result as $tag) {
      $tags[$tag->tid] = $tag;
    }
    if ($display == NULL) {
      $display = variable_get('tagclouds_display_type', 'style');
    }
    $tags = tagclouds_build_weighted_tags($tags, $steps);
    cache_set($cache_name, $tags, 'cache_page', CACHE_TEMPORARY);
  }
  return $tags;
}