You are here

public function TagService::getTags in TagCloud 8

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

Return an array of tags.

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 sortTags() or by your own ordering data.

Parameters

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

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.

int $size: (optional) The number of tags that will be returned. Default to 60.

string $display: (optional) The type of display "style"=weighted,"count"=numbered display.

Return value

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

Overrides TagServiceInterface::getTags

File

src/TagService.php, line 87

Class

TagService
Class TagService.

Namespace

Drupal\tagclouds

Code

public function getTags(array $vids, $steps = 6, $size = 60, $display = NULL) {

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

  // Check if the cache exists.
  $cache_name = 'tagclouds_cache_' . $options;
  $cache = $this->cacheStore
    ->get($cache_name);
  $tags = [];

  // Make sure cache has data.
  if (!empty($cache->data)) {
    $tags = $cache->data;
  }
  else {
    if (count($vids) == 0) {
      return [];
    }
    $config = $this->configFactory
      ->get('tagclouds.settings');
    $query = \Drupal::database()
      ->select('taxonomy_term_data', 'td');
    $query
      ->addExpression('COUNT(td.tid)', 'count');
    $query
      ->fields('tfd', [
      'name',
      'description__value',
    ]);
    $query
      ->fields('td', [
      'tid',
      'vid',
    ]);
    $query
      ->addExpression('MIN(tn.nid)', 'nid');
    $query
      ->join('taxonomy_index', 'tn', 'td.tid = tn.tid');
    $query
      ->join('node_field_data', 'n', 'tn.nid = n.nid');
    $query
      ->join('taxonomy_term_field_data', 'tfd', 'tfd.tid = tn.tid');
    if ($config
      ->get('language_separation')) {
      $query
        ->condition('n.langcode', $language
        ->getId());
    }
    $query
      ->condition('td.vid', $vids);
    $query
      ->condition('n.status', 1);
    $query
      ->groupBy('td.tid')
      ->groupBy('td.vid')
      ->groupBy('tfd.name');
    $query
      ->groupBy('tfd.description__value');
    $query
      ->having('COUNT(td.tid)>0');
    $query
      ->orderBy('count', 'DESC');
    if ($size > 0) {
      $query
        ->range(0, $size);
    }
    $result = $query
      ->execute()
      ->fetchAll();
    foreach ($result as $tag) {
      $tags[$tag->tid] = $tag;
    }
    if ($display == NULL) {
      $display = $config
        ->get('display_type');
    }
    $tags = $this
      ->buildWeightedTags($tags, $steps);
    $this->cacheStore
      ->set($cache_name, $tags, CacheBackendInterface::CACHE_PERMANENT, [
      'node_list',
      'taxonomy_term_list',
      'config:tagclouds.settings',
    ]);
  }
  return $tags;
}