How to locate the file who handles submit button in Drupal 7 - drupal

I'm new to Drupal 7, it's weird to me compared to traditional programming like native PHP, MVC frameworks, Node.js, .Net, etc. Please help me locate the file and/or function that handles the submit button. I have this tpl file already
<form id="com_id" method="post" action="">
...
<button type="submit" id="saveBtn" class="hidden btn-save" name="btnSave"><?php echo t('SAVE'); ?></button>
As you can see there's no value in form's action. In Laravel MVC, it's easy,
Route::get('/the_path', 'RoutinesController#theMethod');

1.Create a custom module
2.Create a form in it example:
function mymodule_example_form($form, &$form_state) {
//build your form using the form api documentation, linked example and your needs
}
3.Add validation for the form:
function mymodule_example_form_validate($form, &$form_state) {
//your validation goes here
}
4.Add submit function to it
function mymodule_example_form_submit($form, &$form_state) {
//do stuff on submit here
}
Not sure where you want to print it but you can always put it in a custom block and render it from there or attach it to a node using hooks (mymodule_node_view) there are many ways but if you need more details be specific and we will help you out and don't forget to turn the module on once you created it :)
UPDATE: If the form already exists use hook form alter:
function your_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'your_form_ID') {
$form['#submit'][] = 'your_module_custom_form_submit';
}
function your_module_custom_form_submit(&$form, &$form_state, $form_id) {
//do stuff here on submit
}

Related

How can I redirect a Drupal user after they create new content

I want to redirect my users after they create a piece of new content, instead of directing them to the 'view' page for the content.
I have seen mention of using hook_alter or something, but I'm really new to drupal and not even sure what that means?
Thanks!
As you mention that you are new to Drupal, I'd suggest to take a look at the Rules module. You can add a trigger on for content has been saved/updated and add an action, to redirect the user to a specific page.
You can however do the same in a light weight custom module using a form_alter hook.
First, find the form ID of the form. For node forms, it's the [node-type]_node_form.
Then, you can add a new submit function to be executed when the form is submitted. In this submit handler, set the redirect path.
See this guide on a basic how-to on creating a module.
Your module code would be something like belows:
<?php
function mymodule_mytype_node_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_node_submit_do_redirect';
}
function mymodule_node_submit_do_redirect($form, &$form_state) {
$form_state['redirect'] = 'my_custom_destination';
}
A much much simpler approach is to set the destination in the node form's URL.
For example, if you opened http://example.com/node/add/mytype?destination=my_custom_destination, you will be redirected to that URL instead of the node view page.
This works for me using Drupal 7, after creating a new module!
Put into the .module file the following PHP code:
<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == '[node-type]_node_form') {
$form['actions']['submit']['#submit'][]= 'my_custom_submit_handler';
}
}
function my_custom_submit_handler($form, &$form_state) {
$form_state['redirect'] = 'http://www.expamle.eu/?q=node/2';
}
You just need to change [node-type]_node_form with your node type name (e.g.: example_node_form) and the http://www.expamle.eu/?q=node/2 with the correct URL.

Display a thanks page in Drupal quiz module

I have the Quiz module installed in my drupal website. My client want to see a thanks page rather than a Quiz Result page, once the Quiz is completed.
I haven't really used Quiz module soecifically, but I surmise can pass it a form state redirect using hook_form_alter. Something like:
function MODULE_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == 'form_id')
{
$form['actions']['submit']['#submit'][] = 'MODULE_form_id_submit_handler';
}
}
Then catch that with a custom submit handler:
function MODULE_form_id_submit_handler($form, &$form_state)
{
$form_state['redirect'] = 'path/to/thanks/page';

Drupal: run custom code when a form is submitted

how can I run specific code when a form is submitted in Drupal ?
I'm using hook_form_alter to edit the form, but I actually need to collect the data inserted by user and run code when the user click on "Save / Register" etc
thanks
You can add callbacks to the submit array. It goes something like this:
function myform_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'some_form') {
$form['#submit'][] = 'mycallback';
}
}
function mycallback(&$form, &$form_state) {
// do stuff
}
Try adding the following function:
function myform_form_submit($form_id, $form_values){
print_r($form_values);
// custom code
}
worked for me. Hope it helps :)

drupal6 error ajax inline

How can i set a custom inline error messages to form in a node (include cck and all the stuff) ?
I saw several modules, but none of them give a 100% solution, because there is no CCK-support, upload-support, error messages etc.
I'm guessing you are looking to do custom validation on a CCK field?
In that case you can add your function by creating a module and implementing hook_form_alter() and creating your own validation function.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'story_node_form': // Or whatever content type you're checking
// Simply add an additional link validate handler.
$form['#validate'][] = 'mymodule_field_validate';
break;
}
}
function mymodule_field_validate($form, &$form_state) {
if (!somevalidatorfunctionorother($form_state['values']['my_custom_field']) {
form_set_error('my_custom_field', t('Text of your error message.'));
}
}
I adapted the code above from this post: http://fleetthought.com/adding-additional-cck-validation-function-field-using-hookformalter

drupal form alter

I'm developing a module which alter display of add/edit node forms. I'm a beginner in module development.
I have written following code, it is not working properly. Please tell me what's wrong with this?
function hook_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_form') {
drupal_set_message(t('some message.'));
}
}
This is for drupal 6.
In addition, the node add/edit forms have content-type specific IDs. so story nodes would be:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'story_node_form') {
drupal_set_message(t('Editing a story node!'));
}
}
If you're looking to catch every node edit form, regardless of type, try this:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
drupal_set_message(t('Editing a node!'));
}
}
you should not call your function "hook_form_alter" but rather "yourmodule_form_alter". yourmodule should be the name of your module file, so if your module is called "hello" the function name should be "hello_form_alter".
You need to understand the hook system that Drupal uses if you want to do module development. When a hook is implemented somewhere in Drupal, it's called hook_something. The way this works is that every time a module wants to implement the hook, you need to replace the 'hook' with the 'modelname'. Drupal will know it's an instance of that hook and call it as such. Drupal also uses the same system in the theming layer, with a twist. As it will look at the different theming functions and prioritize them based on a ranking system, that enabled themes to override the way modules output data.
function hook_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_form') {
drupal_set_message(t('some message.'));
}
}
First you must "hook" keyword to your module keyword.. lets suppose your module name is "contact_us" then
function contact_us_form_alter(&$form, $form_state, $form_id) {
Now this function has three variable
$form
$form_id
$form_state
Most important variable during form alter is $form_id which basically note down which form is load in a page.
function contact_us_form_alter(&$form, $form_state, $form_id) {
print_r($form_id);exit; // Used to find the form id
}
after you find the form_id
function contact_us_form_alter(&$form, $form_state, $form_id) {
if($form_id=='contact_us_form')
// Do your stuff
}
Note:check you have properly write custom module and enable it.. also clear cache

Resources