View source
<?php
namespace Drupal\forum\Controller;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityAccessControlHandlerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\forum\ForumManagerInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\TermStorageInterface;
use Drupal\taxonomy\VocabularyStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ForumController extends ControllerBase {
protected $forumManager;
protected $vocabularyStorage;
protected $termStorage;
protected $nodeAccess;
protected $fieldMap;
protected $nodeTypeStorage;
protected $renderer;
protected $nodeEntityTypeDefinition;
protected $commentEntityTypeDefinition;
public function __construct(ForumManagerInterface $forum_manager, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, AccountInterface $current_user, EntityAccessControlHandlerInterface $node_access, array $field_map, EntityStorageInterface $node_type_storage, RendererInterface $renderer, EntityTypeInterface $node_entity_type_definition, EntityTypeInterface $comment_entity_type_definition) {
$this->forumManager = $forum_manager;
$this->vocabularyStorage = $vocabulary_storage;
$this->termStorage = $term_storage;
$this->currentUser = $current_user;
$this->nodeAccess = $node_access;
$this->fieldMap = $field_map;
$this->nodeTypeStorage = $node_type_storage;
$this->renderer = $renderer;
$this->nodeEntityTypeDefinition = $node_entity_type_definition;
$this->commentEntityTypeDefinition = $comment_entity_type_definition;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($container
->get('forum_manager'), $entity_type_manager
->getStorage('taxonomy_vocabulary'), $entity_type_manager
->getStorage('taxonomy_term'), $container
->get('current_user'), $entity_type_manager
->getAccessControlHandler('node'), $container
->get('entity_field.manager')
->getFieldMap(), $entity_type_manager
->getStorage('node_type'), $container
->get('renderer'), $entity_type_manager
->getDefinition('node'), $entity_type_manager
->getDefinition('comment'));
}
public function forumPage(TermInterface $taxonomy_term) {
$taxonomy_term->forums = $this->forumManager
->getChildren($this
->config('forum.settings')
->get('vocabulary'), $taxonomy_term
->id());
$taxonomy_term->parents = $this->termStorage
->loadAllParents($taxonomy_term
->id());
if (empty($taxonomy_term->forum_container->value)) {
$build = $this->forumManager
->getTopics($taxonomy_term
->id(), $this
->currentUser());
$topics = $build['topics'];
$header = $build['header'];
}
else {
$topics = [];
$header = [];
}
return $this
->build($taxonomy_term->forums, $taxonomy_term, $topics, $taxonomy_term->parents, $header);
}
public function forumIndex() {
$vocabulary = $this->vocabularyStorage
->load($this
->config('forum.settings')
->get('vocabulary'));
$index = $this->forumManager
->getIndex();
$build = $this
->build($index->forums, $index);
if (empty($index->forums)) {
$build['#title'] = $this
->t('No forums defined');
}
else {
$build['#title'] = $vocabulary
->label();
$this->renderer
->addCacheableDependency($build, $vocabulary);
}
return $build;
}
protected function build($forums, TermInterface $term, $topics = [], $parents = [], $header = []) {
$config = $this
->config('forum.settings');
$build = [
'#theme' => 'forums',
'#forums' => $forums,
'#topics' => $topics,
'#parents' => $parents,
'#header' => $header,
'#term' => $term,
'#sortby' => $config
->get('topics.order'),
'#forums_per_page' => $config
->get('topics.page_limit'),
];
if (empty($term->forum_container->value)) {
$build['#attached']['feed'][] = [
'taxonomy/term/' . $term
->id() . '/feed',
'RSS - ' . $term
->getName(),
];
}
$this->renderer
->addCacheableDependency($build, $config);
foreach ($forums as $forum) {
$this->renderer
->addCacheableDependency($build, $forum);
}
foreach ($topics as $topic) {
$this->renderer
->addCacheableDependency($build, $topic);
}
foreach ($parents as $parent) {
$this->renderer
->addCacheableDependency($build, $parent);
}
$this->renderer
->addCacheableDependency($build, $term);
$is_forum = empty($term->forum_container->value);
return [
'action' => $is_forum ? $this
->buildActionLinks($config
->get('vocabulary'), $term) : [],
'forum' => $build,
'#cache' => [
'tags' => Cache::mergeTags($this->nodeEntityTypeDefinition
->getListCacheTags(), $this->commentEntityTypeDefinition
->getListCacheTags()),
],
];
}
public function addForum() {
$vid = $this
->config('forum.settings')
->get('vocabulary');
$taxonomy_term = $this->termStorage
->create([
'vid' => $vid,
'forum_controller' => 0,
]);
return $this
->entityFormBuilder()
->getForm($taxonomy_term, 'forum');
}
public function addContainer() {
$vid = $this
->config('forum.settings')
->get('vocabulary');
$taxonomy_term = $this->termStorage
->create([
'vid' => $vid,
'forum_container' => 1,
]);
return $this
->entityFormBuilder()
->getForm($taxonomy_term, 'container');
}
protected function buildActionLinks($vid, TermInterface $forum_term = NULL) {
$user = $this
->currentUser();
$links = [];
foreach ($this->fieldMap['node']['taxonomy_forums']['bundles'] as $type) {
if ($this->nodeAccess
->createAccess($type)) {
$node_type = $this->nodeTypeStorage
->load($type);
$links[$type] = [
'#theme' => 'menu_local_action',
'#link' => [
'title' => $this
->t('Add new @node_type', [
'@node_type' => $this->nodeTypeStorage
->load($type)
->label(),
]),
'url' => Url::fromRoute('node.add', [
'node_type' => $type,
]),
],
'#cache' => [
'tags' => $node_type
->getCacheTags(),
],
];
if ($forum_term && $forum_term
->bundle() == $vid) {
$links[$type]['#link']['localized_options']['query']['forum_id'] = $forum_term
->id();
}
}
}
if (empty($links)) {
if ($user
->isAuthenticated()) {
$links['disallowed'] = [
'#markup' => $this
->t('You are not allowed to post new content in the forum.'),
];
}
else {
$links['login'] = [
'#theme' => 'menu_local_action',
'#link' => [
'title' => $this
->t('Log in to post new content in the forum.'),
'url' => Url::fromRoute('user.login', [], [
'query' => $this
->getDestinationArray(),
]),
],
'#prefix' => '<ul class="action-links">',
'#suffix' => '</ul>',
];
}
}
else {
$links['#prefix'] = '<ul class="action-links">';
$links['#suffix'] = '</ul>';
}
return $links;
}
}