formblock.module
4.96 KB
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
<?php
/**
* Implements hook_form_alter().
*/
function formblock_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['#node_type']->type)) {
$form['formblock'] = array(
'#type' => 'fieldset',
'#title' => t('Form block'),
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array('formblock-node-type-settings-form'),
),
'#attached' => array(
'js' => array(drupal_get_path('module', 'formblock') . '/formblock.js'),
),
);
$form['formblock']['formblock_expose'] = array(
'#type' => 'checkbox',
'#title' => t('Enable data entry from a block'),
'#default_value' => variable_get('formblock_expose_' . $form['#node_type']->type, 0),
'#description' => t('Enable this option to make the entry form for this content type available as a block.'),
);
$form['formblock']['formblock_show_help'] = array(
'#type' => 'checkbox',
'#title' => t('Show submission guidelines'),
'#default_value' => variable_get('formblock_show_help_' . $form['#node_type']->type, 0),
'#description' => t('Enable this option to show the submission guidelines in the block above the form.'),
'#states' => array(
'visible' => array(
':input[name="formblock_expose"]' => array('checked' => TRUE),
),
),
);
}
elseif (strpos($form_id, '_node_form') !== FALSE) {
// Make sure we have necessary includes.
if (empty($form_state['build_info']['files']) || !in_array('modules/node/node.pages.inc', $form_state['build_info']['files'])) {
form_load_include($form_state, 'inc', 'node', 'node.pages');
}
}
}
/**
* Implements hook_block_info().
*/
function formblock_block_info() {
$blocks = array();
foreach (node_type_get_names() as $type => $name) {
if (variable_get('formblock_expose_' . $type, 0)) {
$blocks[$type] = array(
'info' => t('@name form block', array('@name' => $name)),
'cache' => DRUPAL_NO_CACHE,
);
}
}
$blocks['user_register'] = array(
'info' => t('User registration form'),
'cache' => DRUPAL_NO_CACHE,
);
if (module_exists('contact')) {
$blocks['contact_site'] = array(
'info' => t('Site-wide contact form'),
'cache' => DRUPAL_NO_CACHE,
);
}
$blocks['user_password_request'] = array(
'info' => t('Request new password form'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function formblock_block_view($delta = '') {
$block = array();
$block['content']['#attached']['css'][] = drupal_get_path('module', 'formblock') . '/formblock.css';
switch ($delta) {
case 'user_register':
global $user;
// Don't display the form to logged in users or if registration is disabled
if (!$user->uid && variable_get('user_register', 1)) {
$block['content']['form'] = drupal_get_form('user_register_form');
$block['subject'] = t('Create new account');
return $block;
}
break;
case 'user_password_request':
module_load_include('inc', 'user', 'user.pages');
$block['subject'] = t('Request new password');
$block['content']['form'] = drupal_get_form('user_pass');
return $block;
case 'contact_site':
if (user_access('access site-wide contact form') && module_exists('contact')) {
if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
$content = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
}
else {
module_load_include('inc', 'contact', 'contact.pages');
$content = drupal_get_form('contact_site_form');
}
$block['subject'] = t('Contact');
$block['content']['form'] = $content;
return $block;
}
break;
default:
return formblock_get_block($delta);
}
}
/**
* Generate a block containing a node entry form.
*/
function formblock_get_block($type) {
if (node_access('create', $type) && variable_get('formblock_expose_' . $type, 0)) {
$block = array();
// Include page handler for node_add()
module_load_include('inc', 'node', 'node.pages');
// Note title before rendering of form.
$title = drupal_get_title();
$form = node_add($type);
// Restore title, which will have been overridden.
drupal_set_title($title, PASS_THROUGH);
// Get the help
$node_type = node_type_load($type);
if (variable_get('formblock_show_help_' . $type, 0)) {
$block['content']['help'] = array('#markup' => !empty($node_type->help) ? '<p>' . filter_xss_admin($node_type->help) . '</p>' : '');
}
// Add our CSS
$block['content']['#attached']['css'][] = drupal_get_path('module', 'formblock') . '/formblock.css';
$block['subject'] = t('@type form', array('@type' => $node_type->name));
$block['content']['form'] = $form;
return $block;
}
}