LastTopicBlock.php in Opigno forum 3.x
File
src/Plugin/Block/LastTopicBlock.php
View source
<?php
namespace Drupal\opigno_forum\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\forum\ForumManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LastTopicBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $forumManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ForumManagerInterface $forum_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->forumManager = $forum_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('forum_manager'));
}
public function build() {
$tid = $this->configuration["taxonomy_term"];
$build = $this->forumManager
->getTopics($tid, $this
->currentUser());
$build['content'] = [
'#theme' => 'opigno_forum_last_topics_block',
'topics' => array_slice($build['topics'] ?: [], 0, 4),
];
return $build;
}
private function currentUser() : AccountInterface {
return \Drupal::currentUser();
}
}