Lines: 99.07%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Plugin\Block; | |
| 4 | ||
| 5 | use Drupal\Core\Access\AccessResult; | |
| 6 | use Drupal\Core\Block\Attribute\Block; | |
| 7 | use Drupal\Core\Block\BlockBase; | |
| 8 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 9 | use Drupal\Core\Entity\EntityRepositoryInterface; | |
| 10 | use Drupal\Core\Entity\EntityTypeManagerInterface; | |
| 11 | use Drupal\Core\Form\FormStateInterface; | |
| 12 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
| 13 | use Drupal\Core\Render\RendererInterface; | |
| 14 | use Drupal\Core\Session\AccountInterface; | |
| 15 | use Drupal\Core\StringTranslation\TranslatableMarkup; | |
| 16 | use Drupal\visitors\VisitorsCounterInterface; | |
| 17 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 18 | ||
| 19 | /** | |
| 20 | * Provides a 'Popular content' block. | |
| 21 | */ | |
| 22 | #[Block( | |
| 23 | id: "visitors_popular_block", | |
| 24 | admin_label: new TranslatableMarkup("Popular content") | |
| 25 | )] | |
| 26 | final class VisitorsPopularBlock extends BlockBase implements ContainerFactoryPluginInterface { | |
| 27 | ||
| 28 | /** | |
| 29 | * The entity type manager. | |
| 30 | * | |
| 31 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface | |
| 32 | */ | |
| 33 | protected $entityTypeManager; | |
| 34 | ||
| 35 | /** | |
| 36 | * The entity repository service. | |
| 37 | * | |
| 38 | * @var \Drupal\Core\Entity\EntityRepositoryInterface | |
| 39 | */ | |
| 40 | protected $entityRepository; | |
| 41 | ||
| 42 | /** | |
| 43 | * The storage for visitor counter. | |
| 44 | * | |
| 45 | * @var \Drupal\visitors\VisitorsCounterInterface | |
| 46 | */ | |
| 47 | protected $statisticsStorage; | |
| 48 | ||
| 49 | /** | |
| 50 | * The renderer interface. | |
| 51 | * | |
| 52 | * @var \Drupal\Core\Render\RendererInterface | |
| 53 | */ | |
| 54 | protected $renderer; | |
| 55 | ||
| 56 | /** | |
| 57 | * The configuration factory. | |
| 58 | * | |
| 59 | * @var \Drupal\Core\Config\ConfigFactoryInterface | |
| 60 | */ | |
| 61 | protected $configFactory; | |
| 62 | ||
| 63 | /** | |
| 64 | * Constructs a StatisticsPopularBlock object. | |
| 65 | * | |
| 66 | * @param array $configuration | |
| 67 | * A configuration array containing information about the plugin instance. | |
| 68 | * @param string $plugin_id | |
| 69 | * The plugin_id for the plugin instance. | |
| 70 | * @param mixed $plugin_definition | |
| 71 | * The plugin implementation definition. | |
| 72 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | |
| 73 | * The entity type manager. | |
| 74 | * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository | |
| 75 | * The entity repository service. | |
| 76 | * @param \Drupal\visitors\VisitorsCounterInterface $statistics_storage | |
| 77 | * The storage for statistics. | |
| 78 | * @param \Drupal\Core\Render\RendererInterface $renderer | |
| 79 | * The renderer configuration array. | |
| 80 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | |
| 81 | * The configuration factory. | |
| 82 | */ | |
| 83 | public function __construct( | |
| 84 | array $configuration, | |
| 85 | $plugin_id, | |
| 86 | $plugin_definition, | |
| 87 | EntityTypeManagerInterface $entity_type_manager, | |
| 88 | EntityRepositoryInterface $entity_repository, | |
| 89 | VisitorsCounterInterface $statistics_storage, | |
| 90 | RendererInterface $renderer, | |
| 91 | ConfigFactoryInterface $config_factory, | |
| 92 | ) { | |
| 93 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 94 | 11 | $this->entityTypeManager = $entity_type_manager; |
| 95 | 11 | $this->entityRepository = $entity_repository; |
| 96 | 11 | $this->statisticsStorage = $statistics_storage; |
| 97 | 11 | $this->renderer = $renderer; |
| 98 | 11 | $this->configFactory = $config_factory; |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * {@inheritdoc} | |
| 103 | */ | |
| 104 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
| 105 | 11 | return new self( |
| 106 | 11 | $configuration, |
| 107 | 11 | $plugin_id, |
| 108 | 11 | $plugin_definition, |
| 109 | 11 | $container->get('entity_type.manager'), |
| 110 | 11 | $container->get('entity.repository'), |
| 111 | 11 | $container->get('visitors.counter'), |
| 112 | 11 | $container->get('renderer'), |
| 113 | 11 | $container->get('config.factory'), |
| 114 | 11 | ); |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * {@inheritdoc} | |
| 119 | */ | |
| 120 | public function defaultConfiguration() { | |
| 121 | 11 | return [ |
| 122 | 11 | 'top_day_num' => 0, |
| 123 | 11 | 'top_all_num' => 0, |
| 124 | 11 | 'top_last_num' => 0, |
| 125 | 11 | 'entity_type' => 'node', |
| 126 | 11 | ]; |
| 127 | } | |
| 128 | ||
| 129 | /** | |
| 130 | * {@inheritdoc} | |
| 131 | */ | |
| 132 | protected function blockAccess(AccountInterface $account) { | |
| 133 | 4 | $settings = $this->configFactory->get('visitors.settings'); |
| 134 | 4 | $entity_type = $this->configuration['entity_type'] ?? ''; |
| 135 | 4 | $allowed_entity_types = $settings->get('counter.entity_types'); |
| 136 | 4 | $disabled_or_entity_type_not_allowed = !$settings->get('counter.enabled') || !in_array($entity_type, $allowed_entity_types); |
| 137 | 4 | if ($disabled_or_entity_type_not_allowed) { |
| 138 | 2 | return AccessResult::forbidden(); |
| 139 | } | |
| 140 | ||
| 141 | 2 | return AccessResult::allowedIfHasPermission($account, 'access content'); |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * {@inheritdoc} | |
| 146 | */ | |
| 147 | public function blockForm($form, FormStateInterface $form_state) { | |
| 148 | // Popular content block settings. | |
| 149 | 1 | $numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40]; |
| 150 | 1 | $numbers = ['0' => $this->t('Disabled')] + array_combine($numbers, $numbers); |
| 151 | 1 | $form['statistics_block_top_day_num'] = [ |
| 152 | 1 | '#type' => 'select', |
| 153 | 1 | '#title' => $this->t("Number of day's top views to display"), |
| 154 | 1 | '#default_value' => $this->configuration['top_day_num'], |
| 155 | 1 | '#options' => $numbers, |
| 156 | 1 | '#description' => $this->t('How many content items to display in "day" list.'), |
| 157 | 1 | ]; |
| 158 | 1 | $form['statistics_block_top_all_num'] = [ |
| 159 | 1 | '#type' => 'select', |
| 160 | 1 | '#title' => $this->t('Number of all time views to display'), |
| 161 | 1 | '#default_value' => $this->configuration['top_all_num'], |
| 162 | 1 | '#options' => $numbers, |
| 163 | 1 | '#description' => $this->t('How many content items to display in "all time" list.'), |
| 164 | 1 | ]; |
| 165 | 1 | $form['statistics_block_top_last_num'] = [ |
| 166 | 1 | '#type' => 'select', |
| 167 | 1 | '#title' => $this->t('Number of most recent views to display'), |
| 168 | 1 | '#default_value' => $this->configuration['top_last_num'], |
| 169 | 1 | '#options' => $numbers, |
| 170 | 1 | '#description' => $this->t('How many content items to display in "recently viewed" list.'), |
| 171 | 1 | ]; |
| 172 | 1 | $form['entity_type'] = [ |
| 173 | 1 | '#type' => 'radios', |
| 174 | 1 | '#title' => $this->t('Entity types'), |
| 175 | 1 | '#options' => $this->entityTypes(), |
| 176 | 1 | '#default_value' => $this->configuration['entity_type'] ?? 'node', |
| 177 | 1 | '#description' => $this->t('Select entity types to display popular content.'), |
| 178 | 1 | ]; |
| 179 | ||
| 180 | 1 | return $form; |
| 181 | } | |
| 182 | ||
| 183 | /** | |
| 184 | * Returns a list of entity types. | |
| 185 | */ | |
| 186 | protected function entityTypes() { | |
| 187 | 1 | $allowed_entity_types = $this->configFactory->get('visitors.settings')->get('counter.entity_types'); |
| 188 | 1 | $entity_types_list = []; |
| 189 | 1 | $entity_definitions = $this->entityTypeManager->getDefinitions(); |
| 190 | 1 | foreach ($entity_definitions as $entity_name => $entity_definition) { |
| 191 | 1 | if (!in_array($entity_name, $allowed_entity_types)) { |
| 192 | 0 | continue; |
| 193 | } | |
| 194 | 1 | $entity_types_list[$entity_name] = (string) $entity_definition->getLabel(); |
| 195 | } | |
| 196 | 1 | asort($entity_types_list); |
| 197 | ||
| 198 | 1 | return $entity_types_list; |
| 199 | } | |
| 200 | ||
| 201 | /** | |
| 202 | * {@inheritdoc} | |
| 203 | */ | |
| 204 | public function blockSubmit($form, FormStateInterface $form_state) { | |
| 205 | 1 | $this->configuration['top_day_num'] = $form_state->getValue('statistics_block_top_day_num'); |
| 206 | 1 | $this->configuration['top_all_num'] = $form_state->getValue('statistics_block_top_all_num'); |
| 207 | 1 | $this->configuration['top_last_num'] = $form_state->getValue('statistics_block_top_last_num'); |
| 208 | 1 | $this->configuration['entity_type'] = $form_state->getValue('entity_type'); |
| 209 | } | |
| 210 | ||
| 211 | /** | |
| 212 | * {@inheritdoc} | |
| 213 | */ | |
| 214 | public function build() { | |
| 215 | 1 | $content = []; |
| 216 | 1 | $entity_type = $this->configuration['entity_type'] ?? 'node'; |
| 217 | 1 | if ($this->configuration['top_day_num'] > 0) { |
| 218 | 1 | $ids = $this->statisticsStorage->fetchAll($entity_type, 'today', $this->configuration['top_day_num']); |
| 219 | 1 | if ($ids) { |
| 220 | 1 | $content['top_day'] = $this->entityLabelList($ids, $this->t("Today's:")); |
| 221 | 1 | $content['top_day']['#suffix'] = '<br />'; |
| 222 | } | |
| 223 | } | |
| 224 | ||
| 225 | 1 | if ($this->configuration['top_all_num'] > 0) { |
| 226 | 1 | $ids = $this->statisticsStorage->fetchAll($entity_type, 'total', $this->configuration['top_all_num']); |
| 227 | 1 | if ($ids) { |
| 228 | 1 | $content['top_all'] = $this->entityLabelList($ids, $this->t('All time:')); |
| 229 | 1 | $content['top_all']['#suffix'] = '<br />'; |
| 230 | } | |
| 231 | } | |
| 232 | ||
| 233 | 1 | if ($this->configuration['top_last_num'] > 0) { |
| 234 | 1 | $ids = $this->statisticsStorage->fetchAll($entity_type, 'timestamp', $this->configuration['top_last_num']); |
| 235 | 1 | $content['top_last'] = $this->entityLabelList($ids, $this->t('Last viewed:')); |
| 236 | 1 | $content['top_last']['#suffix'] = '<br />'; |
| 237 | } | |
| 238 | ||
| 239 | 1 | return $content; |
| 240 | } | |
| 241 | ||
| 242 | /** | |
| 243 | * {@inheritdoc} | |
| 244 | */ | |
| 245 | public function getCacheTags() { | |
| 246 | 1 | return ['config:visitors.settings']; |
| 247 | } | |
| 248 | ||
| 249 | /** | |
| 250 | * Generates the ordered array of entity links for build(). | |
| 251 | * | |
| 252 | * @param int[] $ids | |
| 253 | * An ordered array of entity ids. | |
| 254 | * @param string $title | |
| 255 | * The title for the list. | |
| 256 | * | |
| 257 | * @return array | |
| 258 | * A render array for the list. | |
| 259 | */ | |
| 260 | protected function entityLabelList(array $ids, $title) { | |
| 261 | 1 | $entity_type = $this->configuration['entity_type'] ?? 'node'; |
| 262 | 1 | $entities = $this->entityTypeManager->getStorage($entity_type)->loadMultiple($ids); |
| 263 | ||
| 264 | 1 | $items = []; |
| 265 | 1 | foreach ($ids as $id) { |
| 266 | 1 | $entity = $this->entityRepository->getTranslationFromContext($entities[$id]); |
| 267 | 1 | $item = $entity->toLink()->toRenderable(); |
| 268 | 1 | $this->renderer->addCacheableDependency($item, $entity); |
| 269 | 1 | $items[] = $item; |
| 270 | } | |
| 271 | ||
| 272 | 1 | return [ |
| 273 | 1 | '#theme' => 'item_list', |
| 274 | 1 | '#items' => $items, |
| 275 | 1 | '#title' => $title, |
| 276 | 1 | '#cache' => [ |
| 277 | 1 | 'tags' => $this->entityTypeManager->getDefinition($entity_type)->getListCacheTags(), |
| 278 | 1 | ], |
| 279 | 1 | ]; |
| 280 | } | |
| 281 | ||
| 282 | } |