Marking all the config fields as required
[project/fb_likebox.git] / fb_likebox.module
1 <?php
2
3 /**
4 * @file
5 * Simple module that provides a configurable block with Facebook Likebox's plugin.
6 */
7
8 /**
9 * Implements hook_block_info().
10 */
11 function fb_likebox_block_info() {
12 $blocks['0']['info'] = t('@site_name on Facebook', array('@site_name' => variable_get('site_name', 'Default site name')));
13 $blocks['0']['cache'] = DRUPAL_NO_CACHE;
14 return $blocks;
15 }
16
17 /**
18 * Implements hook_block_configure().
19 */
20 function fb_likebox_block_configure($delta = '') {
21 switch ($delta) {
22 case '0':
23 // Facebook Widget settings.
24 $form['fb_likebox_display_settings'] = array(
25 '#type' => 'fieldset',
26 '#title' => t('Display options'),
27 '#collapsible' => FALSE,
28 );
29 $form['fb_likebox_theming_settings'] = array(
30 '#type' => 'fieldset',
31 '#title' => t('Theming Settings'),
32 '#collapsible' => FALSE,
33 );
34 // Display settings.
35 $form['fb_likebox_display_settings']['fb_likebox_url'] = array(
36 '#type' => 'textfield',
37 '#title' => t('Facebook Page URL'),
38 '#default_value' => variable_get('fb_likebox_url', 'http://www.facebook.com/wikisaber.es'),
39 '#description' => t('Enter the Facebook Page URL. I.e.: http://www.facebook.com/wikisaber.es'),
40 '#required' => TRUE,
41 );
42 $form['fb_likebox_display_settings']['fb_likebox_colorscheme'] = array(
43 '#type' => 'select',
44 '#title' => t('Color Scheme'),
45 '#default_value' => variable_get('fb_likebox_colorscheme', 'light'),
46 '#options' => array(
47 'light' => t('Light'),
48 'dark' => t('Dark'),
49 ),
50 '#description' => t("The color scheme for the plugin. Options: 'light', 'dark'"),
51 '#required' => TRUE,
52 );
53 $form['fb_likebox_display_settings']['fb_likebox_header'] = array(
54 '#type' => 'select',
55 '#title' => t('Header'),
56 '#default_value' => variable_get('fb_likebox_header', 'true'),
57 '#options' => array(
58 'false' => t('No'),
59 'true' => t('Yes'),
60 ),
61 '#description' => t('Specifies whether to display the Facebook header at the top of the plugin'),
62 '#required' => TRUE,
63 );
64 $form['fb_likebox_display_settings']['fb_likebox_stream'] = array(
65 '#type' => 'select',
66 '#title' => t('Stream'),
67 '#default_value' => variable_get('fb_likebox_stream', 'true'),
68 '#options' => array(
69 'false' => t('No'),
70 'true' => t('Yes'),
71 ),
72 '#description' => t("Specifies whether to display a stream of the latest posts from the Page's wall"),
73 '#required' => TRUE,
74 );
75 $form['fb_likebox_display_settings']['fb_likebox_show_faces'] = array(
76 '#type' => 'select',
77 '#title' => t('Show Faces'),
78 '#default_value' => variable_get('fb_likebox_show_faces', 'true'),
79 '#options' => array(
80 'false' => t('No'),
81 'true' => t('Yes'),
82 ),
83 '#description' => t('Specifies whether or not to display profile photos in the plugin.'),
84 '#required' => TRUE,
85 );
86 $form['fb_likebox_display_settings']['fb_likebox_scrolling'] = array(
87 '#type' => 'select',
88 '#title' => t('Scrolling'),
89 '#default_value' => variable_get('fb_likebox_scrolling', 'no'),
90 '#options' => array(
91 'no' => t('Disabled'),
92 'yes' => t('Enabled'),
93 ),
94 '#description' => t('Enables vertical scrolling'),
95 '#required' => TRUE,
96 );
97 // Theming settings.
98 $form['fb_likebox_theming_settings']['fb_likebox_width'] = array(
99 '#type' => 'textfield',
100 '#title' => t('Width'),
101 '#default_value' => variable_get('fb_likebox_width', '292'),
102 '#description' => t('The width of the Facebook likebox in pixels'),
103 '#required' => TRUE,
104 );
105 $form['fb_likebox_theming_settings']['fb_likebox_height'] = array(
106 '#type' => 'textfield',
107 '#title' => t('Height'),
108 '#default_value' => variable_get('fb_likebox_height', '556'),
109 '#description' => t('The height of the plugin in pixels. The default height provided by Facebook API varies based on number of faces to display, and whether the stream is displayed. With the stream displayed, and 10 faces the default height is 556px. With no faces, and no stream the default height is 63px. You will need to play with these value if you haved disabled those features and you want the block to be displayed without an empty section.'),
110 '#required' => TRUE,
111 );
112 return $form;
113 }
114 }
115
116 /**
117 * Implements hook_block_view().
118 */
119 function fb_likebox_block_view($delta = '') {
120 switch ($delta) {
121 case '0':
122 // Get the values given in the admin form.
123 $fb_url = variable_get('fb_likebox_url', 'http://www.facebook.com/wikisaber.es');
124 $fb_colorscheme = variable_get('fb_likebox_colorscheme', 'light');
125 $fb_header = variable_get('fb_likebox_header', 'true');
126 $fb_stream = variable_get('fb_likebox_stream', 'true');
127 $fb_show_faces = variable_get('fb_likebox_show_faces', 'true');
128 $fb_scrolling = variable_get('fb_likebox_scrolling', 'no');
129 $fb_width = variable_get('fb_likebox_width', '292');
130 $fb_height = variable_get('fb_likebox_height', '556');
131 // Set the subject of the block
132 $block['subject'] = t('@site_name on Facebook', array('@site_name' => variable_get('site_name', 'Default site name')));
133 // Call to the theming function.
134 $block['content'] = theme('fb_likebox_facebook', array('fb_url' => $fb_url, 'fb_colorscheme' => $fb_colorscheme, 'fb_header' => $fb_header, 'fb_stream' => $fb_stream,
135 'fb_show_faces' => $fb_show_faces, 'fb_scrolling' => $fb_scrolling, 'fb_width' => $fb_width, 'fb_height' => $fb_height, ));
136 return $block;
137 }
138 }
139
140 /**
141 * Implements hook_block_save().
142 */
143 function fb_likebox_block_save($delta = '', $edit = array()) {
144 switch ($delta) {
145 case '0':
146 // Set the values given in the block form
147 variable_set('fb_likebox_url', check_url($edit['fb_likebox_url']));
148 variable_set('fb_likebox_colorscheme', check_plain($edit['fb_likebox_colorscheme']));
149 variable_set('fb_likebox_header', check_plain($edit['fb_likebox_header']));
150 variable_set('fb_likebox_stream', check_plain($edit['fb_likebox_stream']));
151 variable_set('fb_likebox_show_faces', check_plain($edit['fb_likebox_show_faces']));
152 variable_set('fb_likebox_scrolling', check_plain($edit['fb_likebox_scrolling']));
153 variable_set('fb_likebox_width', check_plain($edit['fb_likebox_width']));
154 variable_set('fb_likebox_height', check_plain($edit['fb_likebox_height']));
155 }
156 }
157
158 /**
159 * Implements hook_theme().
160 */
161 function fb_likebox_theme($existing, $type, $theme, $path) {
162 return array(
163 'fb_likebox_facebook' => array(
164 'variables' => array(
165 'fb_url' => NULL,
166 'fb_colorscheme' => NULL,
167 'fb_header' => NULL,
168 'fb_stream' => NULL,
169 'fb_show_faces' => NULL,
170 'fb_scrolling' => NULL,
171 'fb_width' => NULL,
172 'fb_height' => NULL,
173 ),
174 'template' => 'fb_likebox',
175 'path' => $path . '/templates',
176 ),
177 );
178 }
179
180 /**
181 * Implements hook_form_FORM_ID_alter().
182 *
183 * Adds validation of block configuration custom fields.
184 */
185 function fb_likebox_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
186 if ($form['module']['#value'] == 'fb_likebox') {
187 $form['#validate'][] = '_fb_likebox_validate_block_settings';
188 }
189 }
190
191
192 /**
193 * Perform the validation of the block settings.
194 */
195 function _fb_likebox_validate_block_settings(&$form, $form_state) {
196 // Facebook display settings validation.
197 $fb_url = $form_state['values']['fb_likebox_url'];
198 if (!valid_url($fb_url, TRUE)) {
199 form_set_error('fb_likebox_url', t('Please enter a valid url'));
200 }
201
202 // Facebook theming settings validation.
203 $fb_width = $form_state['values']['fb_likebox_width'];
204 if (!is_numeric($fb_width) || intval($fb_width) <= 0) {
205 form_set_error('fb_likebox_width', t('Width should be a number bigger than 0'));
206 }
207
208 $fb_height = $form_state['values']['fb_likebox_height'];
209 if (!is_numeric($fb_height) || intval($fb_height) <= 0) {
210 form_set_error('fb_likebox_height', t('Height should be a number bigger than 0'));
211 }
212 }