function example_menu() {
$items['admin/config/example'] = array(
'title' => 'Example',
'description' => 'example configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('example_admin_settings'),
'access arguments' => array('administer example'),
'file' => 'example.admin.inc',
'file path' => drupal_get_path('module', 'example'),
);
return $items;
}
In the above code I am confused how it works. The page callback is drupal_get_form and the page arguments is example_admin_settings. My question is how exactly does this work?
I know that drupal_get_form probably ends up calling example_admin_settings which return system_settings_form. Could someone point me to the right docs?
You're on the right path.
When you access admin/config/example, drupal_get_form('example_admin_settings') is executed. example_admin_settings() itself will return an array which has its contents based on the form api, something like:
$form['name'] = array(
'#type' => 'input',
'#title' => 'Insert your name here: ',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
Drupal will render and output the form automatically for you.
Also, consider posting your next questions about Drupal at https://drupal.stackexchange.com/.
this is example is like making something like this
function example_menu() {
$items['admin/config/example'] = array(
'title' => 'Example',
'description' => 'example configuration',
'page callback' => '_my_page_content_function',
'access arguments' => array('administer example'),
'file' => 'example.admin.inc',
'file path' => drupal_get_path('module', 'example'),
);
return $items;
}
function _my_page_content_function(){
return drupal_get_form('example_admin_settings') ;
}
function example_admin_settings($form, $form_state) {
... return my form
}
in your example you asking drupal to create page and the content of this page is the content that returned by the function drupal_get_form when we pass the argument example_admin_settings to it.
SUMMARY:
page callback is the name of the function that return the content of our page when we pass the argument page aruguments to
sorry for my english
mmmm need more details ?
Related
I have created a custom Drupal7 form module , but I want to show the form to the authenticated users only . I want to create the module likewise that it could have check-box options in the people->permission section from where I could set the permission for this module for all type of users .Here is the menu_hook
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access arguments' => array('access administration pages'),
'access callback' => TRUE
);
return $items;
}
I am new in drupal so any help regarding this would be appreciable.If someone could write down the hook_permission rather than giving examples then it would be a great help.
Here is the implementation of hook_permission
/**
* Implements hook_permission().
*/
function form_example_permission() {
return array(
'administer your module' => array(
'title' => t('Administer permission for your module'),
'description' => t('Some description that would appear on the permission page..'),
),
);
}
And you have to give the key of the returned array (administer your module) to access arguments in the implementation of hook_menu
So, your hook_menu implementation would become:
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access arguments' => array('administer your module'),
);
return $items;
}
Note that you'll have to flush the cache after you change anything in hook_menu. You can do it from admin/config/development/performace/
Try this. After adding this clear your cache and go to the people->permission and from there you could set permission for users .
function form_example_permission() {
return array(
'administer my module' => array(
'title' => t('Administer my module'),
'description' => t('Perform administration tasks for my module.'),
),
);
}
I am completely new to Drupal am trying to create a single page that contains 2 forms (that come from a custom module). I also want this page to be a dedicated file in my theme directory.
In the custom module called "vocabt" I have vocabt.module that contains 3 needed functions:
function vocabt_menu() {
$items['scores/admin'] = array(
'title' => 'Check Your Student\'s Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_admin_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['scores/student'] = array(
'title' => 'Check My Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_student_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
}
function vocabt_admin_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} else {
$values = array();
}
$schools = vocabt_get_school_options();
$form['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_option' => 'Select',
);
$form['username'] = array(
'#type' => 'textfield',
'#title' => 'User Name',
'#required' => TRUE,
);
$form['password'] = array(
'#type' => 'password',
'#title' => 'Password',
'#required' => TRUE,
);
$form['actions'] = array();
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Log In',
'#attributes' => array('class' => array('button blue')),
);
return $form;
}
function vocabt_student_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} elseif(isset($_GET['id']) && $info = vocabt_decode_id($_GET['id'])) {
$values = $info;
} else {
$values = array();
}
$form['left'] = array(
'#prefix' => '<div class="lookup">',
'#suffix' => '</div>',
);
$form['right'] = array(
'#prefix' => '<div class="results">',
'#suffix' => '</div>',
);
$schools = vocabt_get_school_options();
$form['left']['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_value' => 'Select',
);
$form['left']['id'] = array(
'#type' => 'textfield',
'#title' => 'Enter your student ID',
'#required' => TRUE,
'#default_value' => !empty($values['id']) ? $values['id'] : '',
);
$form['left']['submit'] = array(
'#type' => 'submit',
'#value' => 'Check',
'#attributes' => array('class' => array('button blue')),
);
if(!empty($form_state['storage']['error'])) {
$results = '<div class="blackbox">';
$results .= '<div style="padding: 20px 0px; text-align: center;">'.$form_state['storage']['error'].'</div>';
$results .= '</div>';
} elseif(empty($values['id'])) {
$results = '<div class="blackbox">';
$results .= '<div class="instructions">Your score will appear here once you’ve entered your information on the left.</div>';
$results .= '</div>';
} else {
$results = vocabt_get_student_results($values['school'], $values['id']);
}
$form['right']['results'] = array('#markup' => $results);
return $form;
}
My question is I want to hit a URL such as "/scores/admin" that NOT ONLY contains the form within the function of "vocabt_admin_login_form()" but ALSO contains the form within the above function "vocabt_student_login_form()" ....how can I do this?
I also want both forms to appear in page that uses a dedicated PHP file (I can already create custom pages using dedicated PHP files via creating a new page in the CMS), but I do not know how to tie this dedicated PHP file with the 2 forms above. Please let me know! Thank you
It's not clear what you mean by "Dedicated PHP files". In Drupal, you define menu router items using hook_menu(), and then return their data in the specific page callback function. This function can be in any .module file or any included file in normal PHP scope.
Add a new item in your hook_menu() implementation like this:
$items['scores/check'] = array(
'title' => 'Check Scores',
'page callback' => 'vocabt_scores_page', // Note this is the PAGE CALLBACK!
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
)
Now, lets create the page callback function, vocabt_scores_page, to return a render array so Drupal will build both forms in this page - not just the specified one.
function vocabt_scores_page() {
$output = array();
$output['admin'] = drupal_get_form('vocabt_admin_login_form');
$output['student'] = drupal_get_form('vocabt_student_login_form');
return $output;
}
Make sure you clear your site's caches before testing the code. Once saved, go to scores/check page and you will see both forms in one page!
You can move your page callback functions to a different file from the .module file. To do so, add 'files' => 'vocabt.pages.inc' to the menu item and move the functions to a new file named vocabt.pages.inc. Drupal will assume the file is in same folder as the .modulefile. This file name can be anything but we mostly use module.pages.inc, module.admin.inc, likewise.
You could call a template page using the hook_theme function. You could place your html contents in this page.And just call the theme('page_name', $link);.Place your newly created theme file in your corresponding module.
function modulename_theme() {
return array(
'page_name' => array(
'template' => 'page_name',
'arguments' => array('link' => NULL),
),
);
}
You could find more information here
http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7
Hope this helps you... :)
Let's say I have this implementation of hook_menu():
function example_menu(){
$items = array();
$items['admin/recent-completions'] = array(
'title' => 'Recent Completions (Last 100)',
'page callback' => 'example_recent',
'access callback' => user_access('Administer content'),
'type' => MENU_NORMAL_ITEM,
'weight' => -50
);
return $items;
}
How can I make a template for the page callback instead of returning a string?
You would need to implement a hook_theme function and specify a template file.
Then in your page callback, you would have to call your theme function. Something like...
function example_theme($existing, $type, $theme, $path) {
return array(
'recent_completion' => array(
'render element' => 'elements',
'template' => 'recent-completions',
),
...
}
function example_recent() {
// Do some logic and processing here
$render_array = array( /* array with parameters for the template */ );
return theme('recent_completion', $render_array);
}
I had the same question, but wasn't sure how to implement a hook_theme function. This is how it's done (in Drupal 6 at least).
I'm developing a custom module for Drupal 6, that creates a simple form. My problem is that the submit function is not being called/processed!!! Here's my code:
function listgroups_menu(){
$items['user/%/groups-settings'] = array(
'title' => 'Groups Settings',
'page callback' => 'listgroups_groups_list',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function listgroups_groups_list ($uid){
/*
* Couple lines here to access the DB & get the user's $groups.
*/
variable_set('listgroups_database_result', $groups );
$output = drupal_get_form('listgroups_settiongs_form');
return $output;
}
/**
* Form Builder
*/
function listgroups_settiongs_form(){
$groups = variable_get('database_result', array());
//Building the form
$form['display_option'] = array(
'#type' => 'checkbox',
'#title' => t('Show my group.'),
);
$form['groups_selection'] = array(
'#type' => 'radios',
'#title' => 'Please select your group',
'#options' => $groups,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return system_settings_form($form);
}
/**
* Submition
*/
function listgroups_settiongs_form_submit($form, &$form_state){
echo "<pre>I'm heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>";
drupal_set_message('Your settings have been saved! YES!!!');
}
Now, the form rendering & the data retrival of the Db is just perfect. It's when I click the submit button, I get nothing at all!! Only the page refreshes & the messages doesn't appear!!
Any idea why?!!!!
use
return $form;
instead of
return system_settings_form($form);
and also
function xyz_form_submit($form, &$form_state){
//echo "<pre>I'm heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>";
drupal_set_message('<pre>I\'m heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>Your settings have been saved! YES!!!');
}
the problem was if you use system_setting_form then it start behaving as a system setting page that is generally used to store some information in database. So making it normal form you need to return only $form.
Include a submit handler and then assign it a function
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('my_module_function_submit'),
);
my_module_function_submit($form, $form_state){
.
.
.
.
.
}
Refer this link https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit_property
I upgraded a module from 5 to 6. I only have one problem:
I can access the settings page for it, but can not see the contents of it.
This is my code:
function agbnagscreen_menu() {
global $user;
$items = array();
if (agbnagscreen_nag($user)) {
// var_dump($_GET['q']); die();
drupal_goto(sprintf('%s/%s', AGBNAGSCREEN_NAGURL, base64_encode($_GET['q'])));
die();
}
$items['admin/settings/agbnagscreen'] = array(
// 'path' => 'admin/settings/agbnagscreen',
'title' => 'AGB nagscreen',
'access callback' => user_access('Einstellungen von AGB aendern'),
//'access' => user_access('Einstellungen von AGB aendern'),
'page callback' => 'drupal_get_form',
'callback arguments' => array('agbnagscreen_settings_fapi'),
);
$items[AGBNAGSCREEN_NAGURL] = array(
// 'path' => AGBNAGSCREEN_NAGURL,
'title' => 'Allgemeine Geschaeftsbedingungen',
'access' => TRUE,
'callback' => 'drupal_get_form',
'callback arguments' => array('agbnagscreen_fapi'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
I think the problem is cause by this line:
'page callback' => 'drupal_get_form',
Is that correct? How can I write it, that it works?
You might want to read through the Drupal menu system (Drupal 6.x) handbook page to understand the changes to the menu system: you have several problems in your hook_menu implementation.
The conditional at the top will never fire: Drupal 6 only calls hook_menu() when the menu is rebuilt, not on every page load.
There is no callback: use page callback.
The page callback accepts page arguments, not callback arguments.
There is access: use access callback.
access callback always a string containing the function name, not a function, and defaults to "user_access": you need to supply access arguments.
A modified version of your hook_menu implementation might be:
function agbnagscreen_menu() {
$items = array();
$items['admin/settings/agbnagscreen'] = array(
'title' => 'AGB nagscreen',
'access arguments' => array('Einstellungen von AGB aendern'),
'page callback' => 'drupal_get_form',
'page arguments' => array('agbnagscreen_settings_fapi'),
);
$items[AGBNAGSCREEN_NAGURL] = array(
'title' => 'Allgemeine Geschaeftsbedingungen',
'access arguments' => array('access content'),
'page callback' => 'drupal_get_form',
'page arguments' => array('agbnagscreen_fapi'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}