Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jlbellido/drupalFormacion
Browse files Browse the repository at this point in the history
  • Loading branch information
keilovecraft committed Oct 15, 2013
2 parents dc26b91 + a3d8ef0 commit fd6204b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
69 changes: 69 additions & 0 deletions sites/all/modules/main/custom_module/custom_module.forms.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* Define a custom form
*/
function custom_module_my_custom_form(){
$form = array();

//Get the current data stored in DB:
$data = variable_get('custom_module_my_custom_data', array());

// Fieldset:
$form['fieldset'] = array(
'#type' => 'fieldset',
'#title' => 'Mi fieldset',
'#collapsible' => TRUE,
);
// Textfield requiered:
$form['fieldset']['subject'] = array(
'#required' => TRUE,
'#prefix' => '<div class="subject-wrapper">',
'#type' => 'textfield',
'#default_value' => array_key_exists('subject', $data) ? $data['subject'] : '',
'#suffix' => '</div>',
);

// Textarea
$form['body'] = array(
'#type' => 'textarea',
'#default_value' => array_key_exists('body', $data) ? $data['body'] : '',
'#rows' => 4,
'#cols' => 3,
);

// Submit button:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
);

return $form;
}

/**
* Implements FORM_ID_validate().
*/
function custom_module_my_custom_form_validate(&$form, &$form_state) {
if (is_numeric($form_state['values']['subject'])) {
// Show error :
form_set_error('subject', t('Enter a string'));
}
}

/**
* Implements FORM_ID_submit().
*/
function custom_module_my_custom_form_submit($form, $form_state) {
// Create data for to be strored in DB with the submitted data.
$data = array(
'subject' => check_plain($form_state['values']['subject']), // Santize
'body' => check_plain($form_state['values']['body']), // Santize
);
// Store the submitted data into a Drupal variable:
// This is saved as new row in varibale table in DB.
variable_set('custom_module_my_custom_data', $data);

// Success message:
drupal_set_message(t('Your form data has been saved correctly'));
}
5 changes: 5 additions & 0 deletions sites/all/modules/main/custom_module/custom_module.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "Custom module"
description = "Custom module for Drupal class"
core = "7.x"
files[] = custom_module.module
files[] = custom_module.forms.inc.module
29 changes: 29 additions & 0 deletions sites/all/modules/main/custom_module/custom_module.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Implements hook_menu().
*/
function custom_module_menu(){
$items = array(
'my-custom-form' => array(
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_module_my_custom_form'),
'access callback' => 'user_access',
'access arguments' => array('access to my custom form'),
'file' => 'custom_module.forms.inc',
),
);
return $items;
}

/**
* Implements hook_permission().
*/
function custom_module_permission() {
return array(
'access to my custom form' => array(
'title' => t('Access to my custom form'),
'description' => t('Determine access to submit the custom form'),
),
);
}

0 comments on commit fd6204b

Please sign in to comment.