drupal questions - how to print a form from another module? - forms

I'm trying to include a module form in a panel and I've tried using drupal_get_form(), but not sure I'm using it correctly.
In the organic groups module, there's a function to render an og_broadcast_form. It's called within a page_callback in og.module:
// Broadcast tab on group node.
$items['node/%node/broadcast'] = array(
'title' => 'Broadcast',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_broadcast_form', 1),
'access callback' => 'og_broadcast_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'file' => 'og.pages.inc',
'weight' => 7
);
And in og.pages.inc, the function is:
function og_broadcast_form($form_state, $node) {
drupal_set_title(t('Send message to %group', array('%group' => $node->title)));
if (!empty($form_state['post'])) {
drupal_set_message(t('Your message will be sent to all members of this group.'));
}
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 70,
'#maxlength' => 250,
'#description' => t('Enter a subject for your message.'),
'#required' => TRUE,
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#rows' => 5,
'#cols' => 90,
'#description' => t('Enter a body for your message.'),
'#required' => TRUE
);
$form['send'] = array('#type' => 'submit', '#value' => t('Send message'));
$form['gid'] = array('#type' => 'value', '#value' => $node->nid);
return $form;
}
I've tried a number of variations of drupal_get_form:
print drupal_get_form('og_broadcast_form', NULL, arg(1)); //where arg 1 is the node id from the url
print drupal_get_form('og_broadcast_form');
print drupal_get_form('og_broadcast_form', &$form_state, arg(1));
print drupal_get_form('og_broadcast_form', $n); //where $n is node_load(arg(1));
print drupal_get_form('og_broadcast_form', &$form_state, $n);
etc., etc... Is there a way to accomplish what I'm trying to do here?

FYI.. your problem is that you're attempting to load a form located in another modules include file.
The function is located in og.pages.inc and you'll need to make a call to:
module_load_include('inc', 'og', 'og.pages');
This must be done before you can grab the form.

If drupal_get_form is given the name of a function as it's first argument, that will be both the form_id and a function to be called to generate the $form array.
On line 3 of the function code, we have $args = func_get_args();, this is used by drupal_get_form to collect any or all additional arguments you may want to pass to your form-building function.
You should be using drupal_get_form('og_broadcast_form', node_load(arg(1)));.
Are you sure you should be using print and not return? I have recently learned they do very different things in the theming system. I have used drupal_get_form in this way to populate the contents of a block successfully, but at no point did I print to the screen myself.
EDIT: The full node object and not the nid because %node in a menu path uses a wildcard loader to pass the node_load(arg(1)) on to whatever function is being called.

drupal_get_form gets the form for you. have you tried print drupal_render(drupal_get_form('whatever'))) ?

drupal_get_form() only accepts one argument, the $form_id.
http://api.drupal.org/api/function/drupal_get_form/6
Run a hook_form_alter() and var_dump($form_id). That will give you the $form_id, and when you pass that to drupal_get_form(), it should return the rendered form.

Related

Drupal custom form and autocomplete issue

Some how I managed to get it work. But still the result is not coming along with the autocomplete.
Posting my latest code now,
the textfield code
$form['town'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#autocomplete_path' => 'hfind/town/autocomplete',
);
menu function code
function hfind_menu() {
$items = array();
$items['hfind/town/autocomplete'] = array (
'title' => 'Autocomplete for cities',
'page callback' => 'hfind_town_autocomplete',
'access arguments' => array('use autocomplete'),
'type' => MENU_CALLBACK
);
return $items;
}
the callback function code
function hfind_town_autocomplete($string){
$matches = array();
$result = db_select('towns', 't')
->fields('t', array('town_name'))
->condition('town_name', '%' . db_like($string) . '%', 'LIKE')
->execute();
foreach ($result as $row) {
$matches[$row->city] = check_plain($row->city);
}
drupal_json_output($matches);
}
I hope this may the final edit.
The current situation is, autocomplete is working
The url is hfind/town/autocomplete/mtw
but it is not able to find any data from the database. I found why and unable to fix it.
It is because in the last function I've added above the $string needs to be the 'search query' but it is always querying the database as 'autocomplete'. I mean the $string variable always having the value 'autocomplete' instead of user typed value.
One more problem is, even after providing the permission to all types of user to access search autocomplete on the forms, guests users are not able to use the feature.
Please please someone help me..
`drupal_json_output()` instead of `drupal_to_js` and remove `print` .
<code>
hook_menu() {
$items['cnetusers/autocomplete'] = array(
'title' => 'Auto complete path',
'page callback' => 'cnetusers_employees_autocomplete',
'page arguments' => array(2, 3, 4, 5),
'access arguments' => array('access user profiles'),
'type' => MENU_CALLBACK,
);
return $item;
}
// my autocomplete function is like this
function cnetusers_employees_autocomplete() {
// write your sql query
$matches["$record->ename $record->elname [id: $record->uid]"] = $value;
}
if (empty($matches)) {
$matches[''] = t('No matching records found.');
}
drupal_json_output($matches);
}
$form['disc_info']['approval'] = array(
'#type' => 'textfield',
'#title' => t('Approval By'),
'#autocomplete_path' => 'cnetusers/autocomplete',
);
</code>

Passing URL parameters to a Drupal form created in a menu item

I am using Drupal 7 and have a custom module where I am creating a menu item which will display a Drupal form. The code is setup like this:
$items['job-seekers/update-resume/%'] = array(
'title' => 'Update Resume',
'page callback' => 'drupal_get_form',
'page arguments' => 'oa_onboard_update_resume_form',
'access arguments' => array('access content'),
'page arguments' => array(2),
);
So my menu item will display a form, but I need to take the parameter passed in the URL and have it stored in a hidden form field that will end up getting passed to the form submission method for processing. My form code looks like this:
/**
* Form callback for updating resume
*/
function oa_onboard_update_resume_form($form_state) {
$form['resume_file'] = array(
'#type' => 'file',
'#title' => t('Resume Upload'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
So it's a very simple form that is just a file upload field, however I can't figure out how to gain access to the ID that is passed in the URL. Originally I was thinking I needed to account for it in the form method like so:
function oa_onboard_update_resume_form($candidateId) {
But I get an error when I try to do that:
Notice: Undefined index: 20249278 in drupal_retrieve_form() (line 763 of C:\xampp\htdocs\mysite\includes\form.inc).
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function '20249278' not found or invalid function name in drupal_retrieve_form() (line 798 of C:\xampp\htdocs\mysite\includes\form.inc).
I think I might be going about this the wrong way. The bottom line is I just need to create a URL that I can pass an ID to that will display a form and pass that ID to the submit method along with the file upload.
Thanks in advance.
EDIT
Here is the new callback function that renders the form after fixing the menu item per the suggestion below:
function oa_onboard_update_resume_form($candidateId) {
$form['candidateId'] = array(
'#type' => 'hidden',
'#value' => $candidateId,
);
$form['resume_file'] = array(
'#type' => 'file',
'#title' => t('Resume Upload'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
In the hook_menu function you have twice set 'page arguments' and you should use the 'page callback' with the function oa_onboard_update_resume_form.
$items['job-seekers/update-resume/%'] = array(
'title' => 'Update Resume',
'page callback' => 'oa_onboard_update_resume_form',
'access arguments' => array('access content'),
'page arguments' => array(2),
);
OK so this is what ended up working:
$items['job-seekers/update-resume/%'] = array(
'title' => 'Update Resume',
'page callback' => 'drupal_get_form',
'page arguments' => array('oa_onboard_update_resume_form', 2),
'access arguments' => array('access content'),
);
function oa_onboard_update_resume_form($form, &$form_state, $candidateId) {
// I can use candidateId here now
}

sending form values $form_state['redirect'] Form API Drupal

I want to send my form values using form API drupal. I have following value
$form['billing']["cardholders_name"] = array(
'#type' => 'textfield',
'#title' => t("Cardholder's Name"),
'#required' => TRUE,
'#prefix' => '<div class="field-wrapper-w1 card-name">'
);
I am writing following code in my form submit function
function test_form_submit($form, &$form_state) {
$form_state['redirect'] = 'www.test.com/page' . '?cname=' . $form_state['values']['billing[cardholders_name]'];
}
But it seems like it is not working. Please help
The following example appears in
http://api.drupal.org/api/drupal/includes%21form.inc/function/drupal_redirect_form/7
$form_state['redirect'] = array(
'node/123',
array(
'query' => array(
'foo' => 'bar',
),
'fragment' => 'baz',
),
);
Any value for form element will be in $form_state under values so in your function test_form_submit you can access cardholders_name
$form_state['values']['cardholders_name']
also you can do it in this way using drupal_goto()
drupal_goto('www.test.com/page' . '?cname=' . $form_state['values']['cardholders_name]);

zend form float and integer validation

I am using zend form. I want to validate a field and want to allow only flat and integer values in the field. It means user can either enter any floating value like 2.0 or 3.56, etc OR 4 or 7, etc. But I dont want to receive alpha numeric or alphabet input.
I have used digit validator but it only allows digits not floating number.
Can any body tell me how to put both validations together?
My code is as follows
$parent_affiliate_commission = new Zend_Form_Element_Text('parent_affiliate_commission');
$parent_affiliate_commission->setRequired(true)
->addFilter('StringTrim')
->addFilter('StripTags')
->addValidator('Digits')
->setAttrib('class', 'small')
->addValidator('StringLength', false, array(2, 100))
->setDecorators(array('ViewHelper', 'errors'))
It would be really easy to create and use a custom validator. You could validate simply by using PHP is_int and is_float functions and still do in the 'Zend way'.
By using the callback method I have validated the value in numeric.Because it will not allow the characters.
$inputFilter->add(
$factory->createInput(
array(
'name' => 'proximity',
'required' => true,
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
'messages' => array(
\Zend\Validator\Callback::INVALID_VALUE => 'The proximity value should be numbers'
),
'callback' => function (
$value,
$context = array())
{
$isValid = is_numeric(
$value);
return $isValid;
}
)
)
)
)));

In Drupal 7, how do I show a new form that can edit data selected from a different form?

I have a tableselect form that lists several items. When a user selects one or more items, and clicks the edit button, I want a new form to show up that lets them edit the items.
I have the new form structure being generated, but I can't get it to show up after the edit button is clicked.
Currently, nothing seems to happen. I know that the tableselect form is being submitted correctly, and the function to create the edit term form is working correctly. I tested it with drupal_set_message and var export.
So, how do I get the new form to show?
Here is my relevant code:
/**
* Generate form for listing terms
*/
function markit_form_terms_list()
{
$form = array();
$form['terms'] = array(
'#type' => 'fieldset',
'#title' => t('List of Terms'),
);
$header = array(t('Name'), t('ID'), t('SetID'));
$form['terms']['items'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#title' => t('Terms'),
'#options' => markit_get_array_terms(),//drupal_map_assoc($header, 'markit_get_array_terms'),
'#tree' => TRUE,
);
$form['terms']['submit'] = array(
'#type' => 'submit',
'#value' => t('Edit Term'),
'#submit' => array('markit_form_terms_list_submit'),
);
/*$form['terms']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete Term'),
'#submit' => 'markit_form_terms_delete'
);*/
return $form;
}
/**
* Generate form to edit the terms.
* #param <type> $form
* #param <type> $form_state
* #return string
*/
function markit_form_term_edit($form, $form_state)
{
$newform = array();
$newform['termstoedit'] = array(
'#type' => 'fieldset',
'#title' => t('Edit Term/s'),
);
foreach($form_state['values']['items'] as $row)
{
if($row!=0)//if a row is not selected, it will be 0. So don't select rows equal to 0.
{
$terminfo = markit_get_markterms($row);
drupal_set_message(var_export($terminfo,true));//['term_name']);
drupal_set_message($terminfo[0]['term_name']);
$newform['termstoedit'][$terminfo[0]['term_id']] = array(
'#type' => 'textfield',
'#title' => t('Term:'),
'#default_value' => $terminfo[0]['term_name'],
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
);
}
}
$newform['termstoedit']['submit'] = array(
'#type' => 'submit',
'#value' => t('Edit Term'),
'#submit' => array('markit_form_term_edit_submit'),
);
drupal_set_message(var_export($newform,true));
return $newform;
}
function markit_form_terms_list_submit($form,$form_state)
{
drupal_set_page_content(drupal_build_form('markit_form_term_edit', $form_state));
}
I believe I am not using the correct code in the markit_form_terms_list_submit function. I've tried several different things, but it hasn't worked yet. And the Google searches I've done haven't helped either. I also have searched the Drupal API and Drupal Form API sites extensively.
Anyway, I think that's all the info you might need in order to help me. Thanks in advance!
I believe you may have better luck if you have one form function with differing fields being shown depending on the output.
If you do this you will even be able to use the form ajax methods to auto update your form.
Have a look at this howto to see if you think the approach will work for you.