/[drupal]/contributions/docs/developer/examples/block_example.module
ViewVC logotype

Contents of /contributions/docs/developer/examples/block_example.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.12 - (show annotations) (download) (as text)
Thu Oct 1 18:40:14 2009 UTC (8 weeks, 1 day ago) by jhodgdon
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +96 -72 lines
File MIME type: text/x-php
#345866, #581416 by alexanderpas, jhodgdon: Update block_example.module for Drupal 7.
1 <?php
2 // $Id: block_example.module,v 1.11 2008/09/15 21:57:07 davereid Exp $
3
4 /**
5 * @file
6 * This is an example outlining how a module can define blocks that can be
7 * displayed on various pages of a site.
8 */
9
10 /**
11 * Implementation of hook_block_list().
12 *
13 * This hook declares to Drupal what blocks are provided by the module.
14 */
15 function block_example_block_list() {
16 // This hook returns an array, each component of which is an array of block
17 // information. The array keys are the 'delta' values used in other block
18 // hooks.
19
20 // The required block information is a block description, which is shown
21 // to the site administrator in the list of possible blocks. You can also
22 // provide initial settings for block weight, status, etc.
23
24 // This sample only provides a description string.
25 $blocks['configurable-text'] = array(
26 'info' => t('Example: configurable text string'),
27 );
28
29 // This sample shows how to provide default settings. In this case we'll
30 // enable the block and make it visible only on 'node/*' pages.
31 $blocks['empty'] = array(
32 'info' => t('Example: empty block'),
33 'status' => TRUE,
34 'weight' => 0,
35 'visibility' => 1,
36 'pages' => 'node/*',
37 );
38
39 return $blocks;
40 }
41
42 /**
43 * Implementation of hook_block_configure().
44 *
45 * This hook declares configuration options for blocks provided by this module.
46 */
47 function block_example_block_configure($delta = '') {
48 // The $delta parameter tells us which block is being configured.
49 // In this example, we'll allow the administrator to customize
50 // the text of the 'configurable text string' block defined in this module.
51
52 $form = array();
53 if ($delta == 'configurable-text') {
54 // All we need to provide is the specific configuration options for our
55 // block. Drupal will take care of the standard block configuration options
56 // (block title, page visibility, etc.) and the save button.
57 $form['block_example_string'] = array(
58 '#type' => 'textfield',
59 '#title' => t('Block contents'),
60 '#size' => 60,
61 '#description' => t('This text will appear in the example block.'),
62 '#default_value' => variable_get('block_example_string', t('Some example content.')),
63 );
64 }
65 return $form;
66 }
67
68 /**
69 * Implementation of hook_block_save().
70 *
71 * This hook declares how the configured options for a block
72 * provided by this module are saved.
73 */
74 function block_example_block_save($delta = '', $edit = array()) {
75 // We need to save settings from the configuration form.
76 // We need to check $delta to make sure we are saving the right block.
77 if ($delta == 'configurable-text') {
78 // Have Drupal save the string to the database.
79 variable_set('block_example_string', $edit['block_example_string']);
80 }
81 return;
82 }
83
84 /**
85 * Implementation of hook_block_view().
86 *
87 * This hook generates the contents of the blocks themselves.
88 */
89 function block_example_block_view($delta = '') {
90 //The $delta parameter tells us which block is being requested.
91 switch ($delta) {
92 case 'configurable-text':
93 // The subject is displayed at the top of the block. Note that it
94 // should be passed through t() for translation.
95 $block['subject'] = t('Title of block #1');
96 // The content of the block is typically generated by calling a custom
97 // function.
98 $block['content'] = block_example_contents(1);
99 break;
100 case 'empty':
101 $block['subject'] = t('Title of block #2');
102 $block['content'] = block_example_contents(2);
103 break;
104 }
105 return $block;
106 }
107
108 /**
109 * A module-defined block content function.
110 */
111 function block_example_contents($which_block) {
112 switch ($which_block) {
113 case 1:
114 // Modules would typically perform some database queries to fetch the
115 // content for their blocks. Here, we'll just use the variable set in the
116 // block configuration or, if none has set, a default value.
117 return variable_get('block_example_string', t('A default value.'));
118
119 case 2:
120 // It is possible that your block will not have any content, since it is
121 // probably dynamically constructed. In this case, Drupal will not display
122 // the block at all.
123 return;
124 }
125 }
126

  ViewVC Help
Powered by ViewVC 1.1.2