summaryrefslogtreecommitdiff
path: root/modules/block/block.api.php
blob: c1f5d6145b2886f75e2c6630c72012979ee93442 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
// $Id$

/**
 * @file
 * Hooks provided by the Block module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * List of all blocks defined by the module.
 *
 * Any module can export a block (or blocks) to be displayed by defining
 * the _block hook. This hook is called by theme.inc to display a block,
 * and also by block.module to procure the list of available blocks.
 *
 * @return
 *   An associative array whose keys define the $delta
 *   for each block and whose values contain the block descriptions. Each
 *   block description is itself an associative array, with the following
 *   key-value pairs:
 *   - 'info': (required) The human-readable name of the block.
 *   - 'cache': A bitmask of flags describing how the block should behave with
 *     respect to block caching. The following shortcut bitmasks are provided
 *     as constants in block.module:
 *     - BLOCK_CACHE_PER_ROLE (default): The block can change depending on the
 *       roles the user viewing the page belongs to.
 *     - BLOCK_CACHE_PER_USER: The block can change depending on the user
 *       viewing the page. This setting can be resource-consuming for sites
 *       with large number of users, and should only be used when
 *       BLOCK_CACHE_PER_ROLE is not sufficient.
 *     - BLOCK_CACHE_PER_PAGE: The block can change depending on the page
 *       being viewed.
 *     - BLOCK_CACHE_GLOBAL: The block is the same for every user on every
 *       page where it is visible.
 *     - BLOCK_NO_CACHE: The block should not get cached.
 *   - 'weight', 'status', 'region', 'visibility', 'pages':
 *     You can give your blocks an explicit weight, enable them, limit them to
 *     given pages, etc. These settings will be registered when the block is first
 *     loaded at admin/block, and from there can be changed manually via block
 *     administration.
 *     Note that if you set a region that isn't available in a given theme, the
 *     block will be registered instead to that theme's default region (the first
 *     item in the _regions array).
 *
 * After completing your blocks, do not forget to enable them in the
 * block admin menu.
 *
 * For a detailed usage example, see block_example.module.
 */
function hook_block_list() {
  $blocks['exciting'] = array(
    'info' => t('An exciting block provided by Mymodule.'),
    'weight' => 0,
    'status' => 1,
    'region' => 'left',
    // BLOCK_CACHE_PER_ROLE will be assumed for block 0.
  );

  $blocks['amazing'] = array(
    'info' => t('An amazing block provided by Mymodule.'),
    'cache' => BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE,
  );

  return $blocks;
}

/**
 * Configuration form for the block.
 *
 * Any module can export a block (or blocks) to be displayed by defining
 * the _block hook. This hook is called by theme.inc to display a block,
 * and also by block.module to procure the list of available blocks.
 *
 * @param $delta
 *   Which block to return. This is a descriptive string used to identify
 *   blocks within each module and also within the theme system.
 *   The $delta for each block is defined within the array that your module
 *   returns when the hook_block_list() implementation is called.
 * @return
 *   Optionally return the configuration form.
 *
 * After completing your blocks, do not forget to enable them in the
 * block admin menu.
 *
 * For a detailed usage example, see block_example.module.
 */
function hook_block_configure($delta = '') {
  if ($delta == 'exciting') {
    $form['items'] = array(
      '#type' => 'select',
      '#title' => t('Number of items'),
      '#default_value' => variable_get('mymodule_block_items', 0),
      '#options' => array('1', '2', '3'),
    );
    return $form;
  }
}

/**
 * Save the configuration options.
 *
 * Any module can export a block (or blocks) to be displayed by defining
 * the _block hook. This hook is called by theme.inc to display a block,
 * and also by block.module to procure the list of available blocks.
 *
 * @param $delta
 *   Which block to save the settings for. This is a descriptive string used
 *   to identify blocks within each module and also within the theme system.
 *   The $delta for each block is defined within the array that your module
 *   returns when the hook_block_list() implementation is called.
 * @param $edit
 *   The submitted form data from the configuration form.
 *
 * After completing your blocks, do not forget to enable them in the
 * block admin menu.
 *
 * For a detailed usage example, see block_example.module.
 */
function hook_block_save($delta = '', $edit = array()) {
  if ($delta == 'exciting') {
    variable_set('mymodule_block_items', $edit['items']);
  }
}

/**
 * Process the block when enabled in a region in order to view its contents.
 *
 * Any module can export a block (or blocks) to be displayed by defining
 * the _block hook. This hook is called by theme.inc to display a block,
 * and also by block.module to procure the list of available blocks.
 *
 * @param $delta
 *   Which block to return. This is a descriptive string used to identify
 *   blocks within each module and also within the theme system.
 *   The $delta for each block is defined within the array that your module
 *   returns when the hook_block_list() implementation is called.
 * @return
 *   An array which must define a 'subject' element and a 'content' element
 *   defining the block indexed by $delta.
 *
 * The functions mymodule_display_block_exciting and _amazing, as used in the
 * example, should of course be defined somewhere in your module and return the
 * content you want to display to your users. If the "content" element is empty,
 * no block will be displayed even if "subject" is present.
 *
 * After completing your blocks, do not forget to enable them in the
 * block admin menu.
 *
 * For a detailed usage example, see block_example.module.
 */
function hook_block_view($delta = '') {
  switch ($delta) {
    case 'exciting':
      $block = array(
        'subject' => t('Default title of the exciting block'),
        'content' => mymodule_display_block_exciting(),
      );
      break;
    case 'amazing':
      $block = array(
        'subject' => t('Default title of the amazing block'),
        'content' => mymodule_display_block_amazing(),
      );
      break;
  }
  return $block;
}

/**
 * Act on blocks prior to rendering.
 *
 * This hook allows you to add, remove or modify blocks in the block list. The
 * block list contains the block definitions not the rendered blocks. The blocks
 * are rendered after the modules have had a chance to manipulate the block
 * list.
 * Alternatively you can set $block->content here, which will override the
 * content of the block and prevent hook_block_view() from running.
 *
 * @param $blocks
 *   An array of $blocks, keyed by $bid
 *
 * This example shows how to achieve language specific visibility setting for
 * blocks.
 */
function hook_block_list_alter(&$blocks) {
  global $language, $theme_key;

  $result = db_query('SELECT module, delta, language FROM {my_table}');
  $block_languages = array();
  foreach ($result as $record) {
    $block_languages[$record->module][$record->delta][$record->language] = TRUE;
  }

  foreach ($blocks as $key => $block) {
    // Any module using this alter should inspect the data before changing it,
    // to ensure it is what they expect.
    if ($block->theme != $theme_key || $block->status != 1) {
      // This block was added by a contrib module, leave it in the list.
      continue;
    }

    if (!isset($block_languages[$block->module][$block->delta])) {
      // No language setting for this block, leave it in the list.
      continue;
    }

    if (!isset($block_languages[$block->module][$block->delta][$language->language])) {
      // This block should not be displayed with the active language, remove
      // from the list.
      unset($blocks[$key]);
    }
  }
}

/**
 * @} End of "addtogroup hooks".
 */