You are here

public function TagadelicCloudView::createTags in Tagadelic 8.3

Parameters

$options. An array of properties that may be needed to create the tags array.:

Poulate the member array of TagadelicTag objects

Return value

NULL.

Overrides TagadelicCloudBase::createTags

File

src/TagadelicCloudView.php, line 68

Class

TagadelicCloudView
Class TagadelicCloudView.

Namespace

Drupal\tagadelic

Code

public function createTags(array $options = array()) {
  if (!empty($options['view'])) {
    $this
      ->setView($options['view']);
  }
  else {
    return;
  }
  if (!empty($options['count_field'])) {
    $this
      ->setCountField($options['count_field']);
  }
  else {
    return;
  }
  if (!empty($options['override_sort'])) {
    $this
      ->setOverrideSort($options['override_sort']);
  }

  // The field for the count_field may be aliased
  // We may have a Drupal field such as comment count on node as the count field
  // First check to see if there is an alias in the query's fields
  foreach ($this->view->build_info['query']
    ->getFields() as $id => $field) {
    if ($field['field'] == $this->count_field) {
      $count_field_alias = $id;
      break;
    }
  }

  // If there is no alias in the fields there may be a field that uses a count in the aggregation settings
  // We therefore need to check the expressions
  if (!isset($count_field_alias)) {
    foreach ($this->view->build_info['query']
      ->getExpressions() as $id => $expression) {
      if (strpos($expression['expression'], 'COUNT') !== FALSE && strpos($expression['expression'], $this->count_field) !== FALSE) {
        $count_field_alias = $id;
      }
    }
  }
  foreach ($this->view->result as $id => $row) {

    // As the tags are not going to be used in the markup we can pass in dummy names and ids
    $name = md5(time() . $id);
    $count_field = $this->count_field;

    // If we there is no alias for the field assume it is present on the Resultrow object
    $prop = isset($count_field_alias) ? $count_field_alias : $count_field;
    if (isset($row->{$prop})) {
      $tag = new TagadelicTag($id + 1, $name, $row->{$prop});
      $this
        ->addTag($tag);
    }
  }
}