Newer
Older
Dries Buytaert
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* @file
* Contains \Drupal\statistics\Plugin\block\block\StatisticsPopularBlock.
*/
namespace Drupal\statistics\Plugin\block\block;
use Drupal\block\BlockBase;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Provides a 'Popular content' block.
*
* @Plugin(
* id = "statistics_popular_block",
* subject = @Translation("Popular content"),
* module = "statistics"
* )
*/
class StatisticsPopularBlock extends BlockBase {
/**
* Number of day's top views to display.
*
* @var int
*/
protected $day_list;
/**
* Number of all time views to display.
*
* @var int
*/
protected $all_time_list;
/**
* Number of most recent views to display.
*
* @var int
*/
protected $last_list;
/**
* Overrides \Drupal\block\BlockBase::blockSettings().
*/
public function blockSettings() {
return array(
'top_day_num' => 0,
'top_all_num' => 0,
'top_last_num' => 0
);
}
/**
* Overrides \Drupal\block\BlockBase::blockAccess().
*/
public function blockAccess() {
if (user_access('access content')) {
$daytop = $this->configuration['top_day_num'];
if (!$daytop || !($result = statistics_title_list('daycount', $daytop)) || !($this->day_list = node_title_list($result, t("Today's:")))) {
return FALSE;
}
$alltimetop = $this->configuration['top_all_num'];
if (!$alltimetop || !($result = statistics_title_list('totalcount', $alltimetop)) || !($this->all_time_list = node_title_list($result, t('All time:')))) {
return FALSE;
}
$lasttop = $this->configuration['top_last_num'];
if (!$lasttop || !($result = statistics_title_list('timestamp', $lasttop)) || !($this->last_list = node_title_list($result, t('Last viewed:')))) {
return FALSE;
}
return TRUE;
}
return FALSE;
}
/**
* Overrides \Drupal\block\BlockBase::blockForm().
*/
public function blockForm($form, &$form_state) {
// Popular content block settings.
$numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
$form['statistics_block_top_day_num'] = array(
'#type' => 'select',
'#title' => t("Number of day's top views to display"),
'#default_value' => $this->configuration['top_day_num'],
'#options' => $numbers,
'#description' => t('How many content items to display in "day" list.'),
);
$form['statistics_block_top_all_num'] = array(
'#type' => 'select',
'#title' => t('Number of all time views to display'),
'#default_value' => $this->configuration['top_all_num'],
'#options' => $numbers,
'#description' => t('How many content items to display in "all time" list.'),
);
$form['statistics_block_top_last_num'] = array(
'#type' => 'select',
'#title' => t('Number of most recent views to display'),
'#default_value' => $this->configuration['top_last_num'],
'#options' => $numbers,
'#description' => t('How many content items to display in "recently viewed" list.'),
);
return $form;
}
/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, &$form_state) {
$this->configuration['top_day_num'] = $form_state['values']['statistics_block_top_day_num'];
$this->configuration['top_all_num'] = $form_state['values']['statistics_block_top_all_num'];
$this->configuration['top_last_num'] = $form_state['values']['statistics_block_top_last_num'];
}
/**
* Implements \Drupal\block\BlockBase::blockBuild().
*/
public function blockBuild() {
$content = array();
if ($this->day_list) {
$content['top_day'] = $this->day_list;
$content['top_day']['#suffix'] = '<br />';
}
if ($this->all_time_list) {
$content['top_all'] = $this->all_time_list;
$content['top_all']['#suffix'] = '<br />';
}
if ($this->last_list) {
$content['top_last'] = $this->last_list;
$content['top_last']['#suffix'] = '<br />';
}
return $content;
}
}