TagadelicCloudView.php in Tagadelic 8.3
File
src/TagadelicCloudView.php
View source
<?php
namespace Drupal\tagadelic;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Link;
use Drupal\tagadelic\TagadelicCloudBase;
use Drupal\tagadelic\TagadelicTag;
class TagadelicCloudView extends TagadelicCloudBase {
protected $view;
protected $count_field;
protected $override_sort = 0;
public function setView(\Drupal\views\ViewExecutable $view) {
$this->view = $view;
return $this;
}
public function setOverrideSort($override_sort) {
$this->override_sort = $override_sort;
return $this;
}
public function setCountField($count_field) {
$this->count_field = $count_field;
return $this;
}
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']);
}
foreach ($this->view->build_info['query']
->getFields() as $id => $field) {
if ($field['field'] == $this->count_field) {
$count_field_alias = $id;
break;
}
}
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) {
$name = md5(time() . $id);
$count_field = $this->count_field;
$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);
}
}
}
protected function cmp($a, $b) {
return strcmp($b
->getCount(), $a
->getCount());
}
}