You are here

class TagadelicCloudView in Tagadelic 8.3

Class TagadelicCloudView.

@package Drupal\tagadelic

Hierarchy

Expanded class hierarchy of TagadelicCloudView

1 file declares its use of TagadelicCloudView
tagadelic.theme.inc in ./tagadelic.theme.inc
Theme for tagadelic views.
1 string reference to 'TagadelicCloudView'
tagadelic.services.yml in ./tagadelic.services.yml
tagadelic.services.yml
1 service uses TagadelicCloudView
tagadelic.tagadelic_view in ./tagadelic.services.yml
Drupal\tagadelic\TagadelicCloudView

File

src/TagadelicCloudView.php, line 15

Namespace

Drupal\tagadelic
View source
class TagadelicCloudView extends TagadelicCloudBase {

  /**
   * A Drupal\views\ViewExecutable object.
   *
   */
  protected $view;

  /**
   * The field on each ResultRow object that will be used to create a tag.
   *
   */
  protected $count_field;

  /**
   * The field on each ResultRow object that will be used to create a tag.
   *
   */
  protected $override_sort = 0;

  /**
   * @param $view_results. An array of Drupal\views\ResultRow objects.
   *
   * @return $this; for chaining.
   */
  public function setView(\Drupal\views\ViewExecutable $view) {
    $this->view = $view;
    return $this;
  }

  /**
   * @param $override_sort. Flag to override the sort order set by the view.
   *
   * @return $this; for chaining.
   */
  public function setOverrideSort($override_sort) {
    $this->override_sort = $override_sort;
    return $this;
  }

  /**
   * @param $count_field. The field on each ResultRow object that will be used to create a tag.
   *
   * @return $this; for chaining.
   */
  public function setCountField($count_field) {
    $this->count_field = $count_field;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  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);
      }
    }
  }
  protected function cmp($a, $b) {
    return strcmp($b
      ->getCount(), $a
      ->getCount());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TagadelicCloudBase::$steps private property #Amount of steps to weight the cloud in. Defaults to 6. Means: 6 different sized tags.
TagadelicCloudBase::$tags protected property An array of TagadelicTag objects.
TagadelicCloudBase::addTag public function Add a TagadelicTag object to the tags array. Overrides TagadelicCloudInterface::addTag
TagadelicCloudBase::cb_sort_by_count private function
TagadelicCloudBase::cb_sort_by_name private function
TagadelicCloudBase::getTags public function Return an array of Tagadelic Tags. Overrides TagadelicCloudInterface::getTags
TagadelicCloudBase::recalculate protected function (Re)calculates the weights on the tags.
TagadelicCloudBase::resetTags private function
TagadelicCloudBase::sort protected function
TagadelicCloudBase::__construct function Constructor 1
TagadelicCloudView::$count_field protected property The field on each ResultRow object that will be used to create a tag.
TagadelicCloudView::$override_sort protected property The field on each ResultRow object that will be used to create a tag.
TagadelicCloudView::$view protected property A Drupal\views\ViewExecutable object.
TagadelicCloudView::cmp protected function
TagadelicCloudView::createTags public function Overrides TagadelicCloudBase::createTags
TagadelicCloudView::setCountField public function
TagadelicCloudView::setOverrideSort public function
TagadelicCloudView::setView public function