class CloudBuilder in TagCloud 2.0.x
Same name and namespace in other branches
- 8 src/CloudBuilder.php \Drupal\tagclouds\CloudBuilder
- 1.0.x src/CloudBuilder.php \Drupal\tagclouds\CloudBuilder
Class CloudBuilder.
@package Drupal\tagclouds
Hierarchy
- class \Drupal\tagclouds\CloudBuilder implements CloudBuilderInterface
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'
1 service uses CloudBuilder
File
- src/
CloudBuilder.php, line 14
Namespace
Drupal\tagcloudsView 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
->toUrl();
$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
->toUrl();
$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
->toUrl();
$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
->toUrl();
$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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CloudBuilder:: |
protected | property | The config factory. | |
CloudBuilder:: |
public | function |
Returns a render array for the tags. Overrides CloudBuilderInterface:: |
|
CloudBuilder:: |
private | function | Display Single Tag with Style | |
CloudBuilder:: |
private | function | ||
CloudBuilder:: |
private | function | ||
CloudBuilder:: |
private | function | Display Single Tag with Style | |
CloudBuilder:: |
public | function | Constructor. |