3 namespace Drupal\statistics\Plugin\Block;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Block\BlockBase;
7 use Drupal\Core\Entity\EntityRepositoryInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Render\RendererInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14 use Drupal\statistics\StatisticsStorageInterface;
17 * Provides a 'Popular content' block.
20 * id = "statistics_popular_block",
21 * admin_label = @Translation("Popular content")
24 class StatisticsPopularBlock extends BlockBase implements ContainerFactoryPluginInterface {
27 * The entity type manager.
29 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
31 protected $entityTypeManager;
34 * The entity repository service.
36 * @var \Drupal\Core\Entity\EntityRepositoryInterface
38 protected $entityRepository;
41 * The storage for statistics.
43 * @var \Drupal\statistics\StatisticsStorageInterface
45 protected $statisticsStorage;
48 * @var \Drupal\Core\Render\RendererInterface
53 * Constructs an StatisticsPopularBlock object.
55 * @param array $configuration
56 * A configuration array containing information about the plugin instance.
57 * @param string $plugin_id
58 * The plugin_id for the plugin instance.
59 * @param mixed $plugin_definition
60 * The plugin implementation definition.
61 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
62 * The entity type manager.
63 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
64 * The entity repository service
65 * @param \Drupal\statistics\StatisticsStorageInterface $statistics_storage
66 * The storage for statistics.
68 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, StatisticsStorageInterface $statistics_storage, RendererInterface $renderer) {
69 parent::__construct($configuration, $plugin_id, $plugin_definition);
70 $this->entityTypeManager = $entity_type_manager;
71 $this->entityRepository = $entity_repository;
72 $this->statisticsStorage = $statistics_storage;
73 $this->renderer = $renderer;
79 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
84 $container->get('entity_type.manager'),
85 $container->get('entity.repository'),
86 $container->get('statistics.storage.node'),
87 $container->get('renderer')
94 public function defaultConfiguration() {
105 protected function blockAccess(AccountInterface $account) {
106 return AccessResult::allowedIfHasPermission($account, 'access content');
112 public function blockForm($form, FormStateInterface $form_state) {
113 // Popular content block settings.
114 $numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40];
115 $numbers = ['0' => $this->t('Disabled')] + array_combine($numbers, $numbers);
116 $form['statistics_block_top_day_num'] = [
118 '#title' => $this->t("Number of day's top views to display"),
119 '#default_value' => $this->configuration['top_day_num'],
120 '#options' => $numbers,
121 '#description' => $this->t('How many content items to display in "day" list.'),
123 $form['statistics_block_top_all_num'] = [
125 '#title' => $this->t('Number of all time views to display'),
126 '#default_value' => $this->configuration['top_all_num'],
127 '#options' => $numbers,
128 '#description' => $this->t('How many content items to display in "all time" list.'),
130 $form['statistics_block_top_last_num'] = [
132 '#title' => $this->t('Number of most recent views to display'),
133 '#default_value' => $this->configuration['top_last_num'],
134 '#options' => $numbers,
135 '#description' => $this->t('How many content items to display in "recently viewed" list.'),
143 public function blockSubmit($form, FormStateInterface $form_state) {
144 $this->configuration['top_day_num'] = $form_state->getValue('statistics_block_top_day_num');
145 $this->configuration['top_all_num'] = $form_state->getValue('statistics_block_top_all_num');
146 $this->configuration['top_last_num'] = $form_state->getValue('statistics_block_top_last_num');
152 public function build() {
155 if ($this->configuration['top_day_num'] > 0) {
156 $nids = $this->statisticsStorage->fetchAll('daycount', $this->configuration['top_day_num']);
158 $content['top_day'] = $this->nodeTitleList($nids, $this->t("Today's:"));
159 $content['top_day']['#suffix'] = '<br />';
163 if ($this->configuration['top_all_num'] > 0) {
164 $nids = $this->statisticsStorage->fetchAll('totalcount', $this->configuration['top_all_num']);
166 $content['top_all'] = $this->nodeTitleList($nids, $this->t('All time:'));
167 $content['top_all']['#suffix'] = '<br />';
171 if ($this->configuration['top_last_num'] > 0) {
172 $nids = $this->statisticsStorage->fetchAll('timestamp', $this->configuration['top_last_num']);
173 $content['top_last'] = $this->nodeTitleList($nids, $this->t('Last viewed:'));
174 $content['top_last']['#suffix'] = '<br />';
181 * Generates the ordered array of node links for build().
184 * An ordered array of node ids.
185 * @param string $title
186 * The title for the list.
189 * A render array for the list.
191 protected function nodeTitleList(array $nids, $title) {
192 $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);
195 foreach ($nids as $nid) {
196 $node = $this->entityRepository->getTranslationFromContext($nodes[$nid]);
199 '#title' => $node->getTitle(),
200 '#url' => $node->urlInfo('canonical'),
202 $this->renderer->addCacheableDependency($item, $node);
207 '#theme' => 'item_list__node',
211 'tags' => $this->entityTypeManager->getDefinition('node')->getListCacheTags(),