TagcloudsListVocs.php in TagCloud 8
File
src/Controller/TagcloudsListVocs.php
View source
<?php
namespace Drupal\tagclouds\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\tagclouds\CloudBuilder;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\tagclouds\TagService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TagcloudsListVocs extends ControllerBase {
use CsvToArrayTrait;
protected $tagService;
protected $cloudBuilder;
public function __construct(TagService $tagService, CloudBuilder $cloudBuilder) {
$this->tagService = $tagService;
$this->cloudBuilder = $cloudBuilder;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tagclouds.tag'), $container
->get('tagclouds.cloud_builder'));
}
public function listVocs($tagclouds_vocs_str = NULL) {
$vocs = $this
->csvToArray($tagclouds_vocs_str);
if (empty($vocs)) {
throw new NotFoundHttpException();
}
$boxes = [];
foreach ($vocs as $vid) {
$vocabulary = Vocabulary::load($vid);
if ($vocabulary == FALSE) {
throw new NotFoundHttpException();
}
$config = $this
->config('tagclouds.settings');
$tags = $this->tagService
->getTags([
$vid,
], $config
->get('levels'), $config
->get('page_amount'));
$sorted_tags = $this->tagService
->sortTags($tags);
$cloud = $this->cloudBuilder
->build($sorted_tags);
if (!$cloud) {
throw new NotFoundHttpException();
}
$boxes[] = [
'#theme' => 'tagclouds_list_box',
'#vocabulary' => $vocabulary,
'#children' => $cloud,
];
}
$output = [
'#attached' => [
'library' => 'tagclouds/clouds',
],
'#type' => 'container',
'#children' => $boxes,
'#attributes' => [
'class' => 'wrapper tagclouds',
],
];
return $output;
}
}