annotate core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\statistics\Plugin\Block;
Chris@0 4
Chris@0 5 use Drupal\Core\Access\AccessResult;
Chris@0 6 use Drupal\Core\Block\BlockBase;
Chris@0 7 use Drupal\Core\Entity\EntityRepositoryInterface;
Chris@0 8 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 9 use Drupal\Core\Form\FormStateInterface;
Chris@0 10 use Drupal\Core\Render\RendererInterface;
Chris@0 11 use Drupal\Core\Session\AccountInterface;
Chris@0 12 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Chris@0 14 use Drupal\statistics\StatisticsStorageInterface;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Provides a 'Popular content' block.
Chris@0 18 *
Chris@0 19 * @Block(
Chris@0 20 * id = "statistics_popular_block",
Chris@0 21 * admin_label = @Translation("Popular content")
Chris@0 22 * )
Chris@0 23 */
Chris@0 24 class StatisticsPopularBlock extends BlockBase implements ContainerFactoryPluginInterface {
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The entity type manager.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 30 */
Chris@0 31 protected $entityTypeManager;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The entity repository service.
Chris@0 35 *
Chris@0 36 * @var \Drupal\Core\Entity\EntityRepositoryInterface
Chris@0 37 */
Chris@0 38 protected $entityRepository;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * The storage for statistics.
Chris@0 42 *
Chris@0 43 * @var \Drupal\statistics\StatisticsStorageInterface
Chris@0 44 */
Chris@0 45 protected $statisticsStorage;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * @var \Drupal\Core\Render\RendererInterface
Chris@0 49 */
Chris@0 50 protected $renderer;
Chris@0 51
Chris@0 52 /**
Chris@0 53 * Constructs an StatisticsPopularBlock object.
Chris@0 54 *
Chris@0 55 * @param array $configuration
Chris@0 56 * A configuration array containing information about the plugin instance.
Chris@0 57 * @param string $plugin_id
Chris@0 58 * The plugin_id for the plugin instance.
Chris@0 59 * @param mixed $plugin_definition
Chris@0 60 * The plugin implementation definition.
Chris@0 61 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 62 * The entity type manager.
Chris@0 63 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
Chris@0 64 * The entity repository service
Chris@0 65 * @param \Drupal\statistics\StatisticsStorageInterface $statistics_storage
Chris@0 66 * The storage for statistics.
Chris@0 67 */
Chris@0 68 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, StatisticsStorageInterface $statistics_storage, RendererInterface $renderer) {
Chris@0 69 parent::__construct($configuration, $plugin_id, $plugin_definition);
Chris@0 70 $this->entityTypeManager = $entity_type_manager;
Chris@0 71 $this->entityRepository = $entity_repository;
Chris@0 72 $this->statisticsStorage = $statistics_storage;
Chris@0 73 $this->renderer = $renderer;
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * {@inheritdoc}
Chris@0 78 */
Chris@0 79 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
Chris@0 80 return new static(
Chris@0 81 $configuration,
Chris@0 82 $plugin_id,
Chris@0 83 $plugin_definition,
Chris@0 84 $container->get('entity_type.manager'),
Chris@0 85 $container->get('entity.repository'),
Chris@0 86 $container->get('statistics.storage.node'),
Chris@0 87 $container->get('renderer')
Chris@0 88 );
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 */
Chris@0 94 public function defaultConfiguration() {
Chris@0 95 return [
Chris@0 96 'top_day_num' => 0,
Chris@0 97 'top_all_num' => 0,
Chris@17 98 'top_last_num' => 0,
Chris@0 99 ];
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * {@inheritdoc}
Chris@0 104 */
Chris@0 105 protected function blockAccess(AccountInterface $account) {
Chris@0 106 return AccessResult::allowedIfHasPermission($account, 'access content');
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * {@inheritdoc}
Chris@0 111 */
Chris@0 112 public function blockForm($form, FormStateInterface $form_state) {
Chris@0 113 // Popular content block settings.
Chris@0 114 $numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40];
Chris@0 115 $numbers = ['0' => $this->t('Disabled')] + array_combine($numbers, $numbers);
Chris@0 116 $form['statistics_block_top_day_num'] = [
Chris@0 117 '#type' => 'select',
Chris@0 118 '#title' => $this->t("Number of day's top views to display"),
Chris@0 119 '#default_value' => $this->configuration['top_day_num'],
Chris@0 120 '#options' => $numbers,
Chris@0 121 '#description' => $this->t('How many content items to display in "day" list.'),
Chris@0 122 ];
Chris@0 123 $form['statistics_block_top_all_num'] = [
Chris@0 124 '#type' => 'select',
Chris@0 125 '#title' => $this->t('Number of all time views to display'),
Chris@0 126 '#default_value' => $this->configuration['top_all_num'],
Chris@0 127 '#options' => $numbers,
Chris@0 128 '#description' => $this->t('How many content items to display in "all time" list.'),
Chris@0 129 ];
Chris@0 130 $form['statistics_block_top_last_num'] = [
Chris@0 131 '#type' => 'select',
Chris@0 132 '#title' => $this->t('Number of most recent views to display'),
Chris@0 133 '#default_value' => $this->configuration['top_last_num'],
Chris@0 134 '#options' => $numbers,
Chris@0 135 '#description' => $this->t('How many content items to display in "recently viewed" list.'),
Chris@0 136 ];
Chris@0 137 return $form;
Chris@0 138 }
Chris@0 139
Chris@0 140 /**
Chris@0 141 * {@inheritdoc}
Chris@0 142 */
Chris@0 143 public function blockSubmit($form, FormStateInterface $form_state) {
Chris@0 144 $this->configuration['top_day_num'] = $form_state->getValue('statistics_block_top_day_num');
Chris@0 145 $this->configuration['top_all_num'] = $form_state->getValue('statistics_block_top_all_num');
Chris@0 146 $this->configuration['top_last_num'] = $form_state->getValue('statistics_block_top_last_num');
Chris@0 147 }
Chris@0 148
Chris@0 149 /**
Chris@0 150 * {@inheritdoc}
Chris@0 151 */
Chris@0 152 public function build() {
Chris@0 153 $content = [];
Chris@0 154
Chris@0 155 if ($this->configuration['top_day_num'] > 0) {
Chris@0 156 $nids = $this->statisticsStorage->fetchAll('daycount', $this->configuration['top_day_num']);
Chris@0 157 if ($nids) {
Chris@0 158 $content['top_day'] = $this->nodeTitleList($nids, $this->t("Today's:"));
Chris@0 159 $content['top_day']['#suffix'] = '<br />';
Chris@0 160 }
Chris@0 161 }
Chris@0 162
Chris@0 163 if ($this->configuration['top_all_num'] > 0) {
Chris@0 164 $nids = $this->statisticsStorage->fetchAll('totalcount', $this->configuration['top_all_num']);
Chris@0 165 if ($nids) {
Chris@0 166 $content['top_all'] = $this->nodeTitleList($nids, $this->t('All time:'));
Chris@0 167 $content['top_all']['#suffix'] = '<br />';
Chris@0 168 }
Chris@0 169 }
Chris@0 170
Chris@0 171 if ($this->configuration['top_last_num'] > 0) {
Chris@0 172 $nids = $this->statisticsStorage->fetchAll('timestamp', $this->configuration['top_last_num']);
Chris@0 173 $content['top_last'] = $this->nodeTitleList($nids, $this->t('Last viewed:'));
Chris@0 174 $content['top_last']['#suffix'] = '<br />';
Chris@0 175 }
Chris@0 176
Chris@0 177 return $content;
Chris@0 178 }
Chris@0 179
Chris@0 180 /**
Chris@0 181 * Generates the ordered array of node links for build().
Chris@0 182 *
Chris@0 183 * @param int[] $nids
Chris@0 184 * An ordered array of node ids.
Chris@0 185 * @param string $title
Chris@0 186 * The title for the list.
Chris@0 187 *
Chris@0 188 * @return array
Chris@0 189 * A render array for the list.
Chris@0 190 */
Chris@0 191 protected function nodeTitleList(array $nids, $title) {
Chris@0 192 $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);
Chris@0 193
Chris@0 194 $items = [];
Chris@0 195 foreach ($nids as $nid) {
Chris@0 196 $node = $this->entityRepository->getTranslationFromContext($nodes[$nid]);
Chris@14 197 $item = $node->toLink()->toRenderable();
Chris@0 198 $this->renderer->addCacheableDependency($item, $node);
Chris@0 199 $items[] = $item;
Chris@0 200 }
Chris@0 201
Chris@0 202 return [
Chris@0 203 '#theme' => 'item_list__node',
Chris@0 204 '#items' => $items,
Chris@0 205 '#title' => $title,
Chris@0 206 '#cache' => [
Chris@0 207 'tags' => $this->entityTypeManager->getDefinition('node')->getListCacheTags(),
Chris@0 208 ],
Chris@0 209 ];
Chris@0 210 }
Chris@0 211
Chris@0 212 }