You are here

class CloudBuilder in TagCloud 8

Same name and namespace in other branches
  1. 2.0.x src/CloudBuilder.php \Drupal\tagclouds\CloudBuilder
  2. 1.0.x src/CloudBuilder.php \Drupal\tagclouds\CloudBuilder

Class CloudBuilder.

@package Drupal\tagclouds

Hierarchy

Expanded class hierarchy of CloudBuilder

2 files declare their use of CloudBuilder
TagcloudsListVocs.php in src/Controller/TagcloudsListVocs.php
TagcloudsTermsBlock.php in src/Plugin/Block/TagcloudsTermsBlock.php
1 string reference to 'CloudBuilder'
tagclouds.services.yml in ./tagclouds.services.yml
tagclouds.services.yml
1 service uses CloudBuilder
tagclouds.cloud_builder in ./tagclouds.services.yml
Drupal\tagclouds\CloudBuilder

File

src/CloudBuilder.php, line 14

Namespace

Drupal\tagclouds
View source
class CloudBuilder implements CloudBuilderInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function build(array $terms) {
    $output = [];
    $config = $this->configFactory
      ->getEditable('tagclouds.settings');
    $display = $config
      ->get('display_type');
    if (empty($display)) {
      $display = 'style';
    }
    if ($display == 'style') {
      foreach ($terms as $term) {
        $term_name = $term->name;
        $term_desc = $term->description__value;
        if ($term->count == 1 && $config
          ->get("display_node_link")) {
          $output[$term->tid] = $this
            ->displayNodeLinkWeight($term_name, $term->tid, $term->nid, $term->weight, $term_desc);
        }
        else {
          $output[$term->tid] = $this
            ->displayTermLinkWeight($term_name, $term->tid, $term->weight, $term_desc);
        }
      }
    }
    else {
      foreach ($terms as $term) {
        $term_name = $term->name;
        $term_desc = $term->description__value;
        if ($term->count == 1 && $config
          ->get("display_node_link")) {
          $output[$term->tid] = $this
            ->displayNodeLinkCount($term_name, $term->tid, $term->nid, $term->count, $term_desc);
        }
        else {
          $output[$term->tid] = $this
            ->displayTermLinkCount($term_name, $term->tid, $term->count, $term_desc);
        }
      }
    }
    return $output;
  }

  /**
   * Display Single Tag with Style
   */
  private function displayTermLinkWeight($name, $tid, $weight, $description) {
    if ($term = Term::load($tid)) {
      $uri = $term
        ->urlInfo();
      $options = $uri
        ->getOptions();
      $options['attributes']['class'][] = 'tagclouds';
      $options['attributes']['class'][] = 'level' . $weight;
      $uri
        ->setOptions($options);
      $build = [
        '#type' => 'link',
        '#prefix' => '<span class="tagclouds-term">',
        '#title' => $name,
        '#url' => $uri,
        '#suffix' => '</span>',
      ];
      return $build;
    }
  }
  private function displayNodeLinkWeight($name, $tid, $nid, $weight, $description) {
    if ($term = Term::load($tid) && ($node = Node::load($nid))) {
      $uri = $node
        ->urlInfo();
      $options = $uri
        ->getOptions();
      $options['attributes']['class'][] = 'tagclouds';
      $options['attributes']['class'][] = 'level' . $weight;
      $uri
        ->setOptions($options);
      $build = [
        '#type' => 'link',
        '#prefix' => '<span class="tagclouds-term">',
        '#title' => $name,
        '#url' => $uri,
        '#suffix' => '</span>',
      ];
      return $build;
    }
  }

  /**
   * Display Single Tag with Style
   */
  private function displayNodeLinkCount($name, $tid, $nid, $count, $description) {
    if ($term = Term::load($tid) && ($node = Node::load($nid))) {
      $uri = $node
        ->urlInfo();
      $options = $uri
        ->getOptions();
      $options['attributes']['class'][] = 'tagclouds';
      $build = [
        '#type' => 'link',
        '#prefix' => '<span class="tagclouds-term">',
        '#title' => $name,
        '#url' => $uri,
        '#suffix' => " ({$count})</span>",
      ];
      return $build;
    }
  }
  private function displayTermLinkCount($name, $tid, $count, $description) {
    if ($term = Term::load($tid)) {
      $uri = $term
        ->urlInfo();
      $options = $uri
        ->getOptions();
      $options['options']['attributes']['class'][] = 'tagclouds';
      $build = [
        '#type' => 'link',
        '#prefix' => '<span class="tagclouds-term">',
        '#title' => $name,
        '#url' => $uri,
        '#suffix' => "({$count}) </span>",
      ];
      return $build;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CloudBuilder::$configFactory protected property The config factory.
CloudBuilder::build public function Returns a render array for the tags. Overrides CloudBuilderInterface::build
CloudBuilder::displayNodeLinkCount private function Display Single Tag with Style
CloudBuilder::displayNodeLinkWeight private function
CloudBuilder::displayTermLinkCount private function
CloudBuilder::displayTermLinkWeight private function Display Single Tag with Style
CloudBuilder::__construct public function Constructor.