webformblock.module in Webform Block 6.3
Same filename and directory in other branches
Expose webform nodes as Drupal blocks.
File
webformblock.moduleView source
<?php
/**
* @file
* Expose webform nodes as Drupal blocks.
*/
/**
* Add a checkbox on webform node editing page under 'advanced'.
* Allows the activating of a corresponding block
*
*/
function webformblock_form_alter(&$form, $form_state, $form_id) {
$node = $form['#parameters'][2];
// Webform 2
if ($form_id == 'webform_node_form') {
$form['webform']['advanced']['addblock'] = array(
'#type' => 'checkbox',
'#return_value' => 1,
'#default_value' => webformblock_exists($node->nid),
'#title' => t('Generate a block'),
'#description' => t('Allow this form to appear in its own block which can be positioned in any block region.'),
'#weight' => -20,
);
}
elseif ($form_id == 'webform_configure_form') {
$form['advanced']['addblock'] = array(
'#type' => 'checkbox',
'#return_value' => 1,
'#default_value' => webformblock_exists($node->nid),
'#title' => t('Generate a block'),
'#description' => t('Allow this form to appear in its own block which can be positioned in any block region.'),
'#weight' => -20,
);
$form['advanced']['use_teaser'] = array(
'#type' => 'checkbox',
'#return_value' => 1,
'#default_value' => variable_get('webformblock_use_teaser_' . $node->nid, FALSE),
'#title' => t('Display the node teaser in the generated block'),
'#weight' => -19,
);
$form['#submit'][] = 'webformblock_config_form_submit';
}
if (strpos($form_id, 'webform_client_form_') === 0) {
if (webformblock_exists($node->nid)) {
// @todo: using an anchor only makes sense if errors are displayed close
// to the form.
//$anchor = str_replace(array('/', '_'), '', strtolower(drupal_get_path_alias('node/' . $node->nid)));
//$form['#action'] = url(drupal_get_path_alias($_GET['q']), array('fragment' => $anchor));
$current_url = url(drupal_get_path_alias($_GET['q']));
$form['#action'] = $current_url;
}
}
}
/**
* Webform 3 configure form submit handler (node/[nid]/webform/configure)
*/
function webformblock_config_form_submit($form, &$form_state) {
$nid = $form['nid']['#value'];
if ($form_state['values']['addblock'] === 1) {
webformblock_insert($nid);
}
else {
webformblock_delete($nid);
}
variable_set('webformblock_use_teaser_' . $nid, $form_state['values']['use_teaser']);
}
/**
* Implements hook_nodeapi().
*
* Mostly for Webform 2, this takes care of inserting and removing the blocks.
*/
function webformblock_nodeapi(&$node, $op, $a3 = NULL, $a4 = NUL) {
switch ($op) {
case 'load':
// Webform 2
$node->webform['addblock'] = webformblock_exists($node->nid);
break;
case 'update':
// Webform 2
if ($node->webform['addblock'] == 1) {
webformblock_insert($node->nid);
}
elseif ($node->webform['addblock'] === 0) {
webformblock_delete($node->nid, $node->webform['addblock']);
}
break;
case 'insert':
// Webform 2
webformblock_insert($node->nid, $node->webform['addblock']);
break;
case 'delete':
webformblock_delete($node->nid);
break;
}
}
/**
* Exposes blocks to match any webforms which have been activated
*/
function webformblock_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$block = webformblock_list();
break;
case 'configure':
return array(
'editnode' => array(
'#type' => 'item',
'#value' => l(t('Edit webform'), "node/{$delta}/edit", array(
'query' => "destination=admin/build/block/configure/webformblock/{$delta}",
)),
),
);
break;
case 'view':
$block = webformblock_view($delta);
break;
}
return $block;
}
/**
* Generate a list of blocks corresponding to webforms which have been ticked
* to generate a block
*/
function webformblock_list() {
$blocks = array();
$wfb = db_query("SELECT n.title, n.nid FROM {webform_block} wfb LEFT JOIN {node} n ON wfb.nid = n.nid WHERE n.type = 'webform' AND n.status = 1");
while ($block = db_fetch_object($wfb)) {
$blocks[$block->nid] = array(
'info' => t('Webform: @title', array(
'@title' => $block->title,
)),
//'visibility' => '', // mark block not to show on same page as actual webform
//'pages' => '', // need to get node path from db
'cache' => BLOCK_CACHE_GLOBAL,
);
}
return $blocks;
}
/**
* Use the block delta to load in the corresponding node and output as a block body
* Block content can be themed as if it was a node-webform.tpl.php etc.
*/
function webformblock_view($nid) {
if (!webformblock_exists($nid)) {
return;
}
$node = node_load($nid);
// Check if the user can add another submission.
if ($node->webform['submit_limit'] != -1) {
// -1: Submissions are never throttled.
module_load_include('inc', 'webform', 'webform_submissions');
// Return if the limit is exceeded and page cache is not active.
if (($limit_exceeded = _webform_submission_limit_check($node)) && ($user->uid != 0 || variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED)) {
return;
}
}
// Remember the node title to use for the block title, but don't print the title in the block content
$block_subject = check_plain($node->title);
$node->title = '';
$teaser = variable_get('webformblock_use_teaser_' . $node->nid, FALSE);
$node = node_view($node, $teaser, FALSE, FALSE);
// Provide an anchor to jump to the embedded contact form
$anchorname = form_clean_id(str_replace('/', '', strtolower(drupal_get_path_alias('node/' . $node->nid))));
$content = '<a name="' . $anchorname . '"></a>' . $node;
$block = array(
'subject' => $block_subject,
'content' => $content,
);
return $block;
}
/**
* Calculates whether a webformblock exists for a node id.
*
* @return TRUE if the webform node id is listed in the webform_block table.
*/
function webformblock_exists($nid) {
$result = db_result(db_query("SELECT nid FROM {webform_block} WHERE nid = %d", $nid));
return $result > 0 ? TRUE : FALSE;
}
/**
* Legacy function to avoid breaking sites that use this old function.
*
* @todo Remove in future version.
*/
function webformblock_nodestatus($nid) {
webformblock_exists($nid);
}
/**
* Insert a block and a reference to the webform node into the database.
*/
function webformblock_insert($nid) {
if (webformblock_exists($nid)) {
return;
}
global $theme_key;
$node = node_load($nid);
db_query("INSERT INTO {webform_block} (nid) VALUES (%d)", $nid);
db_query('INSERT INTO {blocks} (module, delta, theme, status, weight, region, custom, throttle, visibility, pages, title, cache) VALUES ("webformblock", %d, "%s", 1, 0, "", 0, 0, 1, "", "%s", 1)', $nid, $theme_key, $node->title);
}
/**
* Delete a block and the reference to the webform node from the database.
*/
function webformblock_delete($nid) {
if (!webformblock_exists($nid)) {
return;
}
db_query("DELETE FROM {webform_block} WHERE nid = %d", $nid);
db_query("DELETE FROM {blocks} WHERE module = 'webformblock' AND delta = %d", $nid);
}
Functions
Name![]() |
Description |
---|---|
webformblock_block | Exposes blocks to match any webforms which have been activated |
webformblock_config_form_submit | Webform 3 configure form submit handler (node/[nid]/webform/configure) |
webformblock_delete | Delete a block and the reference to the webform node from the database. |
webformblock_exists | Calculates whether a webformblock exists for a node id. |
webformblock_form_alter | Add a checkbox on webform node editing page under 'advanced'. Allows the activating of a corresponding block |
webformblock_insert | Insert a block and a reference to the webform node into the database. |
webformblock_list | Generate a list of blocks corresponding to webforms which have been ticked to generate a block |
webformblock_nodeapi | Implements hook_nodeapi(). |
webformblock_nodestatus | Legacy function to avoid breaking sites that use this old function. |
webformblock_view | Use the block delta to load in the corresponding node and output as a block body Block content can be themed as if it was a node-webform.tpl.php etc. |