View source
<?php
function formblock_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$form['formblock'] = array(
'#type' => 'fieldset',
'#title' => t('Form block'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$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),
'#options' => $options,
'#description' => t('Enable this option to show the submission guidelines in the block above the form.'),
'#process' => array(
'formblock_dependent_process',
),
'#dependency' => array(
'edit-formblock-expose' => array(
'1',
),
),
);
}
}
function formblock_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks = array();
foreach (node_get_types('names') as $type => $name) {
if (variable_get('formblock_expose_' . $type, 0)) {
$blocks[$type] = array(
'info' => t('@name form block', array(
'@name' => $name,
)),
'cache' => BLOCK_NO_CACHE,
);
}
}
$blocks['user_register'] = array(
'info' => t('User registration form'),
'cache' => BLOCK_NO_CACHE,
);
if (module_exists('contact')) {
$blocks['contact_site'] = array(
'info' => t('Site-wide contact form'),
'cache' => BLOCK_NO_CACHE,
);
}
$blocks['user_password_request'] = array(
'info' => t('Request new password form'),
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
case 'view':
switch ($delta) {
case 'user_register':
global $user;
if (!$user->uid && variable_get('user_register', 1)) {
drupal_add_css(drupal_get_path('module', 'formblock') . '/formblock.css', 'module', 'all');
return array(
'subject' => t('Create new account'),
'content' => drupal_get_form('user_register'),
);
}
break;
case 'user_password_request':
global $user;
if (!$user->uid) {
drupal_add_css(drupal_get_path('module', 'formblock') . '/formblock.css', 'module', 'all');
module_load_include('inc', 'user', 'user.pages');
return array(
'subject' => t('Request new password'),
'content' => drupal_get_form('user_pass'),
);
}
break;
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 {
drupal_add_css(drupal_get_path('module', 'formblock') . '/formblock.css', 'module', 'all');
module_load_include('inc', 'contact', 'contact.pages');
$content = drupal_get_form('contact_mail_page');
}
return array(
'subject' => t('Contact'),
'content' => $content,
);
}
break;
default:
return formblock_get_block($delta);
}
}
}
function formblock_get_block($type) {
if (node_access('create', $type) && variable_get('formblock_expose_' . $type, 0)) {
module_load_include('inc', 'node', 'node.pages');
$title = drupal_get_title();
$form = node_add($type);
drupal_set_title($title);
$node_type = node_get_types('type', $type);
$help = '';
if (variable_get('formblock_show_help_' . $type, 0)) {
$help = !empty($node_type->help) ? '<p>' . filter_xss_admin($node_type->help) . '</p>' : '';
}
return array(
'subject' => t('@type form', array(
'@type' => $node_type->name,
)),
'content' => $help . $form,
);
}
}
function formblock_dependent_process($element, $edit, &$form_state, &$form) {
if (isset($element['#dependency'])) {
if (!isset($element['#dependency_count'])) {
$element['#dependency_count'] = 1;
}
if (!isset($element['#dependency_type'])) {
$element['#dependency_type'] = 'hide';
}
$js = array(
'values' => $element['#dependency'],
'num' => $element['#dependency_count'],
'type' => $element['#dependency_type'],
);
if (!empty($form_state['ajax'])) {
$form_state['js settings']['formblock']['dependent'][$element['#id']] = $js;
}
else {
$path = drupal_get_path('module', 'formblock');
drupal_add_js($path . '/formblock.js', 'module', 'header');
$options['formblock']['dependent'][$element['#id']] = $js;
drupal_add_js($options, 'setting');
}
}
return $element;
}