All I need to do is have a form that does this:
User enters postcode in text box
Upon submit user is redirected to mysite.com/[user postcode]
That's it! I know validation etc. would be desirable as well, but I just need to get this working for now. I don't mind if it's harcoded or utilises the Drupal form API (actually I'd prefer the former!).
I know this is dead simple, but unfortunately I'm coming from a front-end background and have a bit to learn about this sort of thing :(
Cheers!
This is pretty easy with the Form API and a custom module. You'll build a form using the Form API and add a submit handler that changes the redirect for the form to whatever you'd like. Finally, you'll need to create a way to access the form (either by creating a menu item or by creating a block).
Here's an example that implements a form like you want: you'll want to peruse the Form API reference to see all the options you have when building a form. It also provides two ways to access the form:
Using hook_menu() to provide a page for the form at http://example.com/test
Using hook_block() to provide a block containing the form that you can add and move around on the block administration page.
Example code:
// Form builder. Form ID = function name
function test_form($form_state) {
$form['postcode'] = array(
'#type' => 'textfield',
'#title' => t('Postcode'),
'#size' => 10,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Go'),
);
return $form;
}
// Form submit handler. Default handler is formid_submit()
function test_form_submit($form, &$form_state) {
// Redirect the user to http://example.com/test/<Postcode> upon submit
$form_state['redirect'] = 'test/' . check_plain($form_state['values']['postcode']);
}
// Implementation of hook_menu(): used to create a page for the form
function test_menu() {
// Create a menu item for http://example.com/test that displays the form
$items['test'] = array(
'title' => 'Postcode form',
'page callback' => 'drupal_get_form',
'page arguments' => array('test_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
// Implementation of hook_block(): used to create a movable block for the form
function test_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list': // Show block info on Site Building -> Blocks
$block['postcode']['info'] = t('Postcode form');
break;
case 'view':
switch ($delta) {
case 'postcode':
$block['subject'] = t('Postcode');
$block['content'] = drupal_get_form('test_form');
break;
}
break;
}
return $block;
}
More info:
Creating modules - a tutorial: Drupal 6.x
Form API Quickstart Guide
Form API Reference
hook_menu() API reference
hook_block() API reference
Example of hook_block in Drupal 6
The Drupal Form API -is- dead simple and its something you need to learn as a developer eventually. Might as well jump in and do it through the API since its not too hard, what you are trying to do.
Creating forms in Drupal is fairly easy once you get the hang of it. I would recommend reading the following link. http://drupal.org/node/751826 It gives a good overview of how to create the form.
In the _submit hook, you can then redirect to the appropriate page by setting $form_state['redirect'].
This is assuming of course that you already have the hang of creating custom modules. If you need more info on that, go here.
Related
I am trying to learn drupal so right now my website resides on my localhost. I am using DRUPAL 7. I have created a contact us page in my drupal site using drupal's contact module. I want to add one more fields(Phone Number) to the existing contact form and need to sent the value along with the email. How is it possible to do something like this.
I have used the following code in my contact module page
function mymodulename_form_contact_site_form_alter(&$form, &$form_state) {
$form['phone'] = array(
'#title' => t('Phone'),
'#type' => 'textfield',
'#required' => TRUE,
);
$order = array(
'name',
'mail',
'phone',
'subject',
'cid',
'message',
'copy',
'submit'
);
foreach ($order as $key => $field) {
// Set/Reset the field's
// weight to the array key value
// from our order array.
$form[$field]['#weight'] = $key;
}
}
But the field is not showing up on the website page. Please help.
Thanks
Now its all fine. I had placed the code in the existing contact module file. That was the issue. Now I create a custom module and placed the similar code there. Now its all fine.
Only change needed to make it working is instead of using this
function mymodulename_form_contact_site_form_alter(&$form, &$form_state) {
use the below line
function mymodulename_form_contact_site_form_alter(&$form, &$form_state, &$form_id) {
Hope this helps someone.
A node loads a profile of a user (external database + views). All of this works when I visit: node/123/profile/id/3. Now I have implemented hook_menu() in order to load any profile page and have nicer URLs.
When I load it myself for some reason $left in page.tpl.php is suddenly empty and many more variables seem not to be loading. I have tried many different functions to render and create the correct $output but realized that node_show() seems to be the function of choice.
Testing has shown now that for some reason hook_nodeapi() calls are ignored.
My code:
/**
* Implementation of hook_menu
*/
function modulename_menu() {
$items = array();
$items['my/nice/url/profile'] = array(
'description' => 'This page holds a view that shows profiles based on the %',
'page callback' => 'website_profile_load',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu path wildcard callback
*/
function website_profile_load() {
$output = node_show(node_load(1221), false, true);
return $output;
}
So what is the correct way to do this and get Panels (see comment below) to load correctly?
UPDATE:
I am using table wizard and Views 2 to connect to another database with information about people that aren't users of the system. This is an alumni page, the page is administered externally and showed internally (nothing I can do about, have to make this work :)
Just discovered that Panels aren't loaded at all. So even if the node I am trying to load is uses panels for some reason none of that is loaded.
/**
* Menu path wildcard callback
*/
function website_profile_load($uid = null) {
if (!$uid) {
global $user; // if no user passed in argument, show current user profile
$uid = $user->uid;
}
$output = drupal_render(content_profile_show_profiles($uid));
}
There are many reasons why rendering the result of a node_load is different from going to the stock Drupal path /node. It is way too much to go over here honestly but the short answer is that you have to define a template/theme and blocks etc for each page you create. Just because you make a new path and do a node_load in the callback for that path doesn't mean Drupal can automagically know how you want to display that content. It simply loads data from the node and it is available to do whatever you please with it after that. This is why you get a blankish looking page instead of what you'd expect from going through /node.
However I will offer this simple solution since it sounds like you want the exact same page you'd get when you go to 'node/123/profile/id/3' but accessible through a link you define yourself. You just need to setup a redirect in your hook_menu like so:
$items['my/nice/url/profile'] = array(
'description' => 'This page holds a view that shows profiles based on the %',
'page callback' => 'drupal_goto',
'page arguments' => 'node/123/profile/id/3',
'access callback' => TRUE,
'type' => MENU_CALLBACK);
This is essentially saying that when you navigate to 'my/nicer/url/profile' it runs: drupal_goto('node/123/profile/id/3');
I found the answer to be that apparently somewhere in the pipeline of creating a node Drupal uses $path (that is originally set by $_GET['q']) and sometimes also $_GET['q'] to determine how to render the page. NOTE that I am using Panels and Ctools Page Manager modules in order to get my things working correctly.
It turns out Panels, if you search the code, looks at $_GET['q'] for quite an amount of things.
Here is what I ended up with:
/**
* Implementation of hook_menu
*/
function modulename_menu() {
$items = array();
// For department and having nice URL's for their profile pages.
$items['my/nice/url/profile/%'] = array(
'description' => 'This page holds a view that shows profiles based on the %',
'page callback' => 'website_profile_load',
'page arguments' => arg(4),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu path callback
*/
function website_profile_load($id = NULL) {
// Rename the query internally since other functions base the design
// on the way the query is structured and not simply by the node which
// is currently loading.
if(!empty($id)) {
$path = $_GET['q'] = 'node/1221/profile/id/' . $id;
}
// Use ctools function to correctly display node view since
// this site heavily uses ctools rendering for panels and
// web parts / web pages.
drupal_load('module', 'page_manager');
ctools_include('node_view', 'page_manager', 'plugins/tasks');
if(function_exists('page_manager_node_view')) {
$output = page_manager_node_view(node_load(1221));
} else {
// Will display incorrectly but still load the UI
$output = node_page_view(node_load(1221));
}
return $output;
}
And it works :)
I'm having a problem with Drupal forms. Can't think of a way to solve it and I was wondering if any brain out there has the solution to my problem.
I have this form:
<?php
function mymodule_myform(){
$form['#action'] = url('search/cards');
$form['whatwhere']['what'] = array(
'#type' => 'textfield',
'#title' => t('What?'),
'#maxlength' => 80,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
}
?>
Which, as you can see, is supposed to submit all info to www.example.com/search/cards.
This will indeed submit my form to the desired URL. However, without the mymodule_menu() hook defining the path, it will end in a 404.
So I add:
<?php
function mymodule_menu(){
$items['search/%'] = array(
'title' => t('Search Results'),
'page callback' => 'mymodule_main',
'access arguments' => array('access content'),
'file' => 'mymodule.inc',
);
}
?>
And, at mymodule.inc file I'll create my function mymodule_main():
<?php
function mymodule_main(){
print_r($_POST);
die(); // ### Note the die(); ###
return 'Just searched: '. $_POST['what'];
}
?>
If I leave it exactly like it is, of course PHP will end the script execution right after printing my form info on the screen. All good!
However, if I remove the die(), it seems the function is called twice, and the second call does not have $_POST filled up anymore.. Seems mymodule_menu() overrides in some way whatever the form submit handler is doing...
The question is: How can I submit my form to any other internal page without having the 404 and keep my form info?
Thanks in advance.
I think you need to use the method described in http://api.drupal.org/api/function/search_box_form_submit/6
function MODULE_block_form_submit($form, &$form_state)
{
if (isset($_REQUEST['destination'])) {
unset($_REQUEST['destination']);
}
if (isset($_REQUEST['edit']['destination'])) {
unset($_REQUEST['edit']['destination']);
}
$form_state['redirect'] = 'search/cards/'. trim($form_state['values']['whatwhere']);
}
I don't know exactly, but maybe your code conflicts with drupal's builtin search module?
After some debate # drupal forums (which can be followed here) conclusion is that: it is not possible to have a menu callback handling form results. You can instead, use a form submit handler (as always) and then redirect to your menu callback.
It's not a POST to the menu callback but it's as good as it gets.
Thanks for your help.
I am modifying an already contributed drupal module (Inline Ajax Search) to handle searching of a specific content type with some search filters (i.e. when searching for help documentation, you filter out your search results by selecting for which product and version of the product you want help with).
I have modified the module some what to handle all the search filters.
I also added in similar functionality from the standard core search module to handle the presenting of the search form and search results on the actual search page ( not the block form ).
The problem is that when i submit the form, i discovered that I'd lose all my post data on that submit because somewhere, and i don't know where, drupal is either redirecting me or something else is happening that is causing me to lose everything in the $_POST array.
here's the hook_menu() implementation:
<?php
function inline_ajax_search_menu() {
$items = array();
$items['search/inline_ajax_search'] = array(
'title' => t('Learning Center Search'),
'description' => t(''),
'page callback' => 'inline_ajax_search_view',
'access arguments' => array('search with inline_ajax_search'),
'type' => MENU_LOCAL_TASK,
'file' => 'inline_ajax_search.pages.inc',
);
}
?>
the page callback is defined as such (very similar to the core search module's search_view function):
<?php
function inline_ajax_search_view() {
drupal_add_css(drupal_get_path('module', 'inline_ajax_search') . '/css/inline_ajax_search.css', 'module', 'all', FALSE );
if (isset($_POST['form_id'])) {
$keys = $_POST['keys'];
// Only perform search if there is non-whitespace search term:
$results = '';
if(trim($keys)) {
require_once( drupal_get_path( 'module', 'inline_ajax_search' ) . '/includes/inline_ajax_search.inc' );
// Collect the search results:
$results = _inline_ajax_search($keys, inline_ajax_search_get_filters(), "page" );
if ($results) {
$results = theme('box', t('Search results'), $results);
}
else {
$results = theme('box', t('Your search yielded no results'), inline_ajax_search_help('inline_ajax_search#noresults', drupal_help_arg()));
}
}
// Construct the search form.
$output = drupal_get_form('inline_ajax_search_search_form', inline_ajax_search_build_filters( variable_get( 'inline_ajax_search_filters', array() ) ) );
$output .= $results;
return $output;
}
return drupal_get_form('inline_ajax_search_search_form', inline_ajax_search_build_filters( variable_get( 'inline_ajax_search_filters', array() ) ) );
}
?>
from my understanding, things should work like this: A user goes to www.mysite.com/search/inline_ajax_search and drupal will process the path given in my url and provide me with a page that holds the themed form for my search module. When i submit the form, whose action is the same url (www.mysite.com/search/inline_ajax_search), then we go thru the same function calls, but we now have data in the $_POST array and one of them is indeed $_POST['form_id'] which is the name of the form "inline_ajax_search_search_form". so we should be able to enter into that if block and put out the search results.
but that's not what happens...somewhere from when i submit the form and get my results and theme it all up, i get redirected some how and lose all my post data.
if anybody can help me, it'd make me so happy lol.
drupal_get_form actually wipes out the $_POST array and so that's why I lose all my post data.
according to this: http://drupal.org/node/748830 $_POST should really be ignored when doing things in drupal. It's better to find a way around using it. One way is the way described in the link, making ur form data persist using the $_SESSION array. I'm sure there are various other and better ways to do this, but yeah, drupal_get_form was the culprit here...
OK so this is my hook form alter function.It is causing all the registration forms on site to be over written which I do not want as I just want it on this page.
function special_registration_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_register') {
drupal_set_title(t('Custom registration'));
$form['firstname'] = array('#type' => 'textfield',
'#title' => t('First Name: *'),
'#required' => TRUE,
'#size' => 45,
'#weight' => - 100,);
$form['lastname'] = array('#type' => 'textfield',
'#title' => t('Last Name: *'),
'#required' => TRUE,
'#size' => 45,
'#weight' => - 99,);
}
I only first name and last name to be captured and stored in a different table just on this page.
On other pages I just want the good old fashioned form. Do I still need to change the weight? I know I am missing something elementary.
You just need a check for the current page, using either arg or $_GET['q'].
eg:
function special_registration_form_alter(&$form, $form_state, $form_id) {
if ($_GET['q'] !== 'whatever/path' ) { return false; }
..rest of code..
}
If you want to restrict your form alterations to a specific page, you can simply add a check for the page to your form id check, e.g.:
function special_registration_form_alter(&$form, $form_state, $form_id) {
// Alter the registration form, but only on 'user/register' pages
if ($form_id == 'user_register' && 'user' == arg(0) && 'register' == arg(1)) {
// snipped alteration code
}
}
You could make use of the Profile module in the Core list of modules as well. It will solve this without any programming, fyi.
Implement hook_user(); the function allow to alter the form presented to the users when they register on a site.
hook_user() is used by user.module, and it is independent from the profile module.
Defining the hook as hook_user($op, &$edit, &$account, $category = NULL), the parameter $op will contain the value 'register' when the registration form is being presented to the user. In that case, the module returns the form fields it wants to add to the registration form.
If you don't really need to create user accounts, like for a simple event registration. If instead you're collecting just names, you could use the webform module instead.