Created
June 26, 2017 10:12
-
-
Save vensires/991e7265b1d2cba581b703b6964c216c to your computer and use it in GitHub Desktop.
A custom module to use as a base for a request form. It also contains pathauto implementation so that
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = Request Forms | |
core = 7.x | |
description = Creates the request forms of the website | |
dependencies[] = pathauto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* The request_form.module file | |
*/ | |
// @todo Change [NID] to the correct nid of the webform node. | |
define('REQUEST_FORMS_WEBFORM_NID', '[NID]'); | |
/** | |
* Delivers an empty page. | |
* | |
* Use the following function for menu items that don't have their own page | |
* content but rather load another page (eg. a webform) in their own url. | |
* | |
* Using the default drupal_deliver_html_page() for this kind of action, the | |
* drupal_page_footer() function gets called twice resulting in the "Headers | |
* already sent" error. | |
* | |
* @see drupal_deliver_html_page() | |
*/ | |
function request_forms_deliver_empty_page($page_callback_result) { | |
// Nothing here. | |
} | |
/** | |
* Implements hook_menu(). | |
*/ | |
function request_forms_menu() { | |
$items['request-forms/form/%node'] = array( | |
'delivery callback' => 'request_forms_deliver_empty_page', | |
'title' => 'Request Form', | |
'page callback' => 'request_forms_form_page_callback', | |
'page arguments' => array(2), | |
'access arguments' => array('access content'), | |
'file' => 'request_forms.pages.inc', | |
); | |
return $items; | |
} | |
/** | |
* Implements hook_form_FORM_ID_alter(). | |
* | |
* @todo Change NID to the correct nid of the webform node. | |
*/ | |
function request_forms_form_webform_client_form_NID_alter(&$form, &$form_state) { | |
$item = menu_get_item(); | |
if (isset($item['map'][2]->nid)) { | |
$node = $item['map'][2]; | |
// Add a custom_return_url property to be used later for redirecting back. | |
$destination = drupal_get_destination(); | |
if (empty($form['#custom_return_url'])) { | |
$form['#custom_return_url'] = $destination['destination']; | |
} | |
$form['#submit'][] = 'request_forms_form_webform_client_form_submit'; | |
// Autofill the values for the node we came from. | |
$form['submitted']['nid']['#value'] = $node->nid; | |
$form['submitted']['node']['#value'] = $title; | |
// Hide them afterwards. | |
$form['submitted']['nid']['#access'] = FALSE; | |
$form['submitted']['node']['#access'] = FALSE; | |
$form['submitted']['reference_number']['#value'] = check_plain($node->field_reference_number[LANGUAGE_NONE][0]['value']); | |
// Add a suffix to the submit button to go back to the node. | |
$form['actions']['submit']['#suffix'] = l(t('Go Back to @title', array('@title' => $node->title)), 'node/' . $node->nid, array('attributes' => array('class' => array('go-back-property')))); | |
// Get current page title and append the name of the villa. | |
drupal_set_title(format_string('@current - @title', array( | |
'@current' => drupal_get_title(), | |
'@title' => $node->title, | |
); | |
} | |
else { | |
/* | |
* This "locks" the webform so that anyone targeting the webform directly gets | |
* redirected to the frontpage. Please make sure you really want this as this | |
* will create redirection for every page this webform gets displayed in. | |
drupal_goto('<front>'); | |
} | |
} | |
/** | |
* Implements hook_pathauto(). | |
*/ | |
function request_forms_pathauto($op) { | |
switch ($op) { | |
case 'settings': | |
$settings = array( | |
'module' => 'request_forms', | |
'token_type' => 'node', | |
'groupheader' => t('Request Forms'), | |
'patterndescr' => t('Path for each Request Form'), | |
'patternitems' => array( | |
'request_form' => 'Path for each Request Form', | |
// ... | |
), | |
'patterndefault' => 'form/request/[node:title]', | |
'batch_update_callback' => 'request_forms_pathauto_bulkupdate', | |
); | |
return (object) $settings; | |
break; | |
} | |
} | |
/** | |
* Implements hook_pathauto_bulkupdate. | |
* | |
* @todo Change the TYPE1 and TYPE2 to the content types you want. | |
*/ | |
function request_forms_pathauto_bulkupdate() { | |
// Find all nids for the content types we want. | |
$query = db_select('node', 'n') | |
->condition('n.type', array(TYPE1, TYPE2)) | |
->fields('n', array('nid')); | |
$results = $query->execute()->fetchCol(); | |
$count = 0; | |
foreach($results as $nid) { | |
$node = node_load($nid); | |
request_forms_generate_aliases($node, $node->type); | |
$count++; | |
} | |
drupal_set_message($count . " nodes were updated."); | |
} | |
/** | |
* Callback responsible for generation of a url alias for our paths. | |
*/ | |
function request_forms_generate_aliases($node, $type) { | |
module_load_include('inc', 'pathauto'); | |
switch ($type) { | |
case TYPE1: | |
case TYPE2: | |
pathauto_create_alias('request_forms', 'update', "request-forms/form/{$node->nid}", array('node' => $node)); | |
break; | |
} | |
} | |
/** | |
* Implements hook_node_insert(). | |
*/ | |
function request_forms_node_insert($node) { | |
request_forms_node_save($node); | |
} | |
/** | |
* Implements hook_node_update(). | |
*/ | |
function request_forms_node_update($node) { | |
request_forms_node_save($node); | |
} | |
/** | |
* Callback to call after node insert/update. | |
*/ | |
function request_forms_node_save($node) { | |
if(isset($node->nid)) { | |
if (in_array($node->type, array(TYPE1, TYPE2)) { | |
request_forms_generate_aliases($node, $node->type); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains the page callbacks for the module. | |
*/ | |
/** | |
* Page callback for request-forms/form/%. | |
* | |
* @param object $node | |
* The node object. | |
*/ | |
function request_forms_form_page_callback($node) { | |
// Act like the webform page and deliver its contents. | |
menu_execute_active_handler('node/' . REQUEST_FORMS_WEBFORM_NID); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment