1802a4569c3f42b70ad881028c1bf636a5a96020
[yaffs-website] / web / core / modules / statistics / src / Plugin / Block / StatisticsPopularBlock.php
1 <?php
2
3 namespace Drupal\statistics\Plugin\Block;
4
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;
15
16 /**
17  * Provides a 'Popular content' block.
18  *
19  * @Block(
20  *   id = "statistics_popular_block",
21  *   admin_label = @Translation("Popular content")
22  * )
23  */
24 class StatisticsPopularBlock extends BlockBase implements ContainerFactoryPluginInterface {
25
26   /**
27    * The entity type manager.
28    *
29    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
30    */
31   protected $entityTypeManager;
32
33   /**
34    * The entity repository service.
35    *
36    * @var \Drupal\Core\Entity\EntityRepositoryInterface
37    */
38   protected $entityRepository;
39
40   /**
41    * The storage for statistics.
42    *
43    * @var \Drupal\statistics\StatisticsStorageInterface
44    */
45   protected $statisticsStorage;
46
47   /**
48    * @var \Drupal\Core\Render\RendererInterface
49    */
50   protected $renderer;
51
52   /**
53    * Constructs an StatisticsPopularBlock object.
54    *
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.
67    */
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;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
80     return new static(
81       $configuration,
82       $plugin_id,
83       $plugin_definition,
84       $container->get('entity_type.manager'),
85       $container->get('entity.repository'),
86       $container->get('statistics.storage.node'),
87       $container->get('renderer')
88     );
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function defaultConfiguration() {
95     return [
96       'top_day_num' => 0,
97       'top_all_num' => 0,
98       'top_last_num' => 0
99     ];
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   protected function blockAccess(AccountInterface $account) {
106     return AccessResult::allowedIfHasPermission($account, 'access content');
107   }
108
109   /**
110    * {@inheritdoc}
111    */
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'] = [
117      '#type' => 'select',
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.'),
122     ];
123     $form['statistics_block_top_all_num'] = [
124       '#type' => 'select',
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.'),
129     ];
130     $form['statistics_block_top_last_num'] = [
131       '#type' => 'select',
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.'),
136     ];
137     return $form;
138   }
139
140   /**
141    * {@inheritdoc}
142    */
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');
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function build() {
153     $content = [];
154
155     if ($this->configuration['top_day_num'] > 0) {
156       $nids = $this->statisticsStorage->fetchAll('daycount', $this->configuration['top_day_num']);
157       if ($nids) {
158         $content['top_day'] = $this->nodeTitleList($nids, $this->t("Today's:"));
159         $content['top_day']['#suffix'] = '<br />';
160       }
161     }
162
163     if ($this->configuration['top_all_num'] > 0) {
164       $nids = $this->statisticsStorage->fetchAll('totalcount', $this->configuration['top_all_num']);
165       if ($nids) {
166         $content['top_all'] = $this->nodeTitleList($nids, $this->t('All time:'));
167         $content['top_all']['#suffix'] = '<br />';
168       }
169     }
170
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 />';
175     }
176
177     return $content;
178   }
179
180   /**
181    * Generates the ordered array of node links for build().
182    *
183    * @param int[] $nids
184    *   An ordered array of node ids.
185    * @param string $title
186    *   The title for the list.
187    *
188    * @return array
189    *   A render array for the list.
190    */
191   protected function nodeTitleList(array $nids, $title) {
192     $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);
193
194     $items = [];
195     foreach ($nids as $nid) {
196       $node = $this->entityRepository->getTranslationFromContext($nodes[$nid]);
197       $item = [
198         '#type' => 'link',
199         '#title' => $node->getTitle(),
200         '#url' => $node->urlInfo('canonical'),
201       ];
202       $this->renderer->addCacheableDependency($item, $node);
203       $items[] = $item;
204     }
205
206     return [
207       '#theme' => 'item_list__node',
208       '#items' => $items,
209       '#title' => $title,
210       '#cache' => [
211         'tags' => $this->entityTypeManager->getDefinition('node')->getListCacheTags(),
212       ],
213     ];
214   }
215
216 }