-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_admin_pagination.module
200 lines (177 loc) · 8.5 KB
/
block_admin_pagination.module
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* @file
* block_admin_pagination.module
*/
function block_admin_pagination_preprocess_block_admin_pagination_display_form(&$variables) {
// this is mostly ported over wholesale from block.admin.inc, with changes
$variables['block_regions'] = $variables['form']['block_regions']['#value'];
if (isset($variables['block_regions'][BLOCK_REGION_NONE])) {
$variables['block_regions'][BLOCK_REGION_NONE] = t('Disabled');
}
foreach ($variables['block_regions'] as $key => $value) {
// Initialize an empty array for the region.
$variables['block_listing'][$key] = array();
}
// Initialize disabled blocks array.
$variables['block_listing'][BLOCK_REGION_NONE] = array();
// Add each block in the form to the appropriate place in the block listing.
foreach (element_children($variables['form']['blocks']) as $i) {
$block = &$variables['form']['blocks'][$i];
// Fetch the region for the current block.
$region = (isset($block['region']['#default_value']) ? $block['region']['#default_value'] : BLOCK_REGION_NONE);
// Set special classes needed for table drag and drop.
$block['region']['#attributes']['class'] = array('block-region-select', 'block-region-' . $region);
$block['weight']['#attributes']['class'] = array('block-weight', 'block-weight-' . $region);
// if the block is in an enabled region, render its form now
if ($region != BLOCK_REGION_NONE) {
$variables['block_listing'][$region][$i] = new stdClass();
$variables['block_listing'][$region][$i]->row_class = !empty($block['#attributes']['class']) ? implode(' ', $block['#attributes']['class']) : '';
$variables['block_listing'][$region][$i]->block_modified = !empty($block['#attributes']['class']) && in_array('block-modified', $block['#attributes']['class']);
$variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
$variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
$variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
$variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
$variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
$variables['block_listing'][$region][$i]->printed = FALSE;
}
else {
// if it's a disabled block, just store it for the time being
// we need the count
$variables['block_listing'][$region][$i] = $block;
unset($variables['form']['blocks'][$i]);
}
}
// set up the pager
$number_per_page = variable_get('block_admin_pager_number_per_page', 25);
$total = count($variables['block_listing'][BLOCK_REGION_NONE]);
$page = pager_default_initialize($total, $number_per_page);
// label the disabled region more appropriately
$beginning = ($page * $number_per_page) + 1;
$end = ($page + 1) * $number_per_page;
$end = $end > $total ? $total : $end;
$variables['block_regions'][BLOCK_REGION_NONE] = t('Disabled (@beginning - @end of @total)', array('@beginning' => $beginning, '@end' => $end, '@total' => $total));
$index = 0; // this is strictly for tracking where we are in the array, for the 'slice'
foreach ($variables['block_listing'][BLOCK_REGION_NONE] as $delta => $block) {
// because this is an assoc. array we can't do a real slice. fake it.
if (($index < ($page * $number_per_page)) || ($index > (($page * $number_per_page + $number_per_page) - 1))) {
// it's outside our pager range; just toss it
unset($variables['block_listing'][BLOCK_REGION_NONE][$delta]);
}
else {
// now do the processing on the disabled blocks that happened earlier for the other regions
$variables['block_listing'][BLOCK_REGION_NONE][$delta] = new stdClass();
$variables['block_listing'][BLOCK_REGION_NONE][$delta]->row_class = !empty($block['#attributes']['class']) ? implode(' ', $block['#attributes']['class']) : '';
$variables['block_listing'][BLOCK_REGION_NONE][$delta]->block_modified = !empty($block['#attributes']['class']) && in_array('block-modified', $block['#attributes']['class']);
$variables['block_listing'][BLOCK_REGION_NONE][$delta]->block_title = drupal_render($block['info']);
$variables['block_listing'][BLOCK_REGION_NONE][$delta]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
$variables['block_listing'][BLOCK_REGION_NONE][$delta]->weight_select = drupal_render($block['weight']);
$variables['block_listing'][BLOCK_REGION_NONE][$delta]->configure_link = drupal_render($block['configure']);
$variables['block_listing'][BLOCK_REGION_NONE][$delta]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
$variables['block_listing'][BLOCK_REGION_NONE][$delta]->printed = FALSE;
}
$index++;
}
$variables['form_submit'] = drupal_render_children($variables['form']);
$variables['form_submit'] = '<h4>See more Disabled blocks</h4>' . theme('pager', array('tags' => NULL)) . $variables['form_submit'];
}
/**
* Implements hook_menu().
*/
function block_admin_pagination_menu() {
$items['admin/config/user-interface/block-admin-pagination'] = array(
'title' => 'Block Admin Pagination',
'description' => 'Configure settings for Block admin pagination',
'page callback' => 'drupal_get_form',
'page arguments' => array('block_admin_pagination_settings_form'),
'access arguments' => array('administer block admin pagination'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function block_admin_pagination_permission() {
return array(
'administer block admin pagination' => array(
'title' => t('Administer Block Admin Pagination'),
'description' => t('Configure settings for the Block Admin Pagination module.'),
),
);
}
/**
* Settings form for Block Admin Pagination
*/
function block_admin_pagination_settings_form() {
$form = array();
$form['block_admin_pager_number_per_page'] = array(
'#type' => 'textfield',
'#title' => t('Number of Disabled Blocks Per Page'),
'#description' => t('The number of disabled blocks you want to appear on a single block admin page. This should be no higher than 50.'),
'#default_value' => variable_get('block_admin_pager_number_per_page', 25),
);
$form['#validate'][] = 'block_admin_pagination_settings_form_validate';
return system_settings_form($form);
}
function block_admin_pagination_settings_form_validate($form, &$form_state) {
$value = $form_state['values']['block_admin_pager_number_per_page'];
if (!is_numeric($value) || ($value < 1) || ($value > 50)) {
form_set_error('block_admin_pager_number_per_page', t('The number of items per page must be an integer between 1 and 50.'));
}
}
/**
* Form submission handler for the main blocks administration form.
*
* @see block_admin_pagination_display_form()
*/
function block_admin_pagination_display_form_submit($form, &$form_state) {
$transaction = db_transaction();
try {
foreach ($form_state['values']['blocks'] as $block) {
$block['status'] = (int) (($block['region'] != BLOCK_REGION_NONE) && !empty($block['region']));
$block['region'] = $block['status'] ? $block['region'] : '-1';
db_update('block')
->fields(array(
'status' => $block['status'],
'weight' => $block['weight'],
'region' => $block['region'],
))
->condition('module', $block['module'])
->condition('delta', $block['delta'])
->condition('theme', $block['theme'])
->execute();
}
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('block', $e);
throw $e;
}
drupal_set_message(t('The block settings have been updated.'));
cache_clear_all();
}
/**
* Implements hook_theme().
*/
function block_admin_pagination_theme($existing, $type, $theme, $path) {
return array(
'block_admin_pagination_display_form' => array(
'template' => 'block-admin-pagination-display-form',
'render element' => 'form',
),
);
}
/**
* Implements hook_form_alter().
*/
function block_admin_pagination_form_alter(&$form, &$form_state, $form_id) {
if ($form_id === 'block_admin_display_form') {
$form['#theme'] = 'block_admin_pagination_display_form';
foreach ($form['#submit'] as $idx => $submit_hook) {
if ($submit_hook == 'block_admin_display_form_submit') {
unset($form['#submit'][$idx]);
$form['#submit'][] = 'block_admin_pagination_display_form_submit';
}
}
}
}