I am a newbie to drupal. I am working to create a custom module in the name "course". I have read about the list of hooks to create a custom module. So, what's my problem is that i cant able to see the edit form for the module configure but i can do add management and its working fine.
I've used Existing contact modules as a reference.
Below are my code:
In course.module
function course_menu() {
$items['admin/structure/course'] = array(
'title' => 'Academy\'s courses',
'description' => 'Create a system contact form and set up categories for the form to use.',
'page callback' => 'course_list',
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
$items['admin/structure/course/add'] = array(
'title' => 'Add Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form'),
'access arguments' => array('administer contact forms'),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
'file' => 'course.admin.inc',
);
$items['admin/structure/course/edit/%course'] = array(
'title' => 'Edit Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form',4),
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
return $items;
}
and in course.admin.inc:
function course_edit_form($form, &$form_state, array $course = array()) {
// If this is a new course, add the default values.
$course += array(
'name' => '',
'course_id' => NULL,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Course Name'),
'#maxlength' => 255,
'#default_value' => $course['name'],
'#description' => t("Example: 'Available Course Names'."),
'#required' => TRUE,
);
$form['course_id'] = array(
'#type' => 'value',
'#value' => $course['course_id'],
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function course_edit_form_submit($form, &$form_state) {
if (empty($form_state['values']['course_id'])) {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->execute();
//drupal_write_record('courses', $form_state['values']);
}
else {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->condition('course_id', $form_state['values']['course_id'])
->execute();
//drupal_write_record('courses', $form_state['values'], array('course_id'));
}
drupal_set_message(t('Course %name has been saved.', array('%name' => $form_state['values']['name'])));
watchdog('contact', 'Course %name has been saved.', array('%course' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/course/edit/' . $form_state['values']['course_id']));
$form_state['redirect'] = 'admin/structure/course';
}
I can't able to find the exact problem, why add is working but not edit & delete? Help me on this.
Below image tells you what exactly i am asking for(Red mark).
If anything additional needed in this please let me know.
At A guess I'd say the edit route is not matching because of an issue with the course load function (not listed). Try dumping the result of that method. Is the course entity defined in your module? If so, then the convention is to prefix the variable name with the module name, and implement the appropriately names hook.
There's a better description of how this should work here: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7
Jump to the "Auto-Loader Wildcards" section.
Try to clear cache /admin/config/development/performance
Related
I am having three fields: First name, Last name and Year of birth. In code I am easily inserting into database when I press submit button, so I want to update these fields when I want to click on Edit button. Please anyone help me to code this update to put the inserted data in a database or please post code.
<?php
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => 'My form',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_my_form'),
'access arguments' => array('access content'),
'description' => 'My form',
'type' => MENU_CALLBACK,
);
$items['my_module/edit'] = array(
'title' => t('Edit Name'),
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_edit'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function my_module_my_form($form_state) {
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE, // Added
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE, // Added
);
$form['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => ('Year of birth'),
'#description' => 'Format is "YYYY"',
);
// Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
function my_module_edit($form, &$form_submit){
$form['Edit'] = array(
'#type' => 'Edit',
'#value' => 'Edit',
);
return $form;
}
function my_module_my_form_submit($form, &$form_state){
$firstName = $form_state['values']['fname'];
$lastName=$form_state['value']['lname'];
$yearofbirth = $form_state['values']['yearOfbirth'];
$query ="INSERT INTO `slideshow`.`mymoduledb`(`first_name`,`last_name`,`year_of_birth`) VALUES ('{$firstName}','{$lastName}','{$yearofbirth}')";
//$result=db_query($query);
if ($success = db_query($query)) {
// Tell the user that the employee has been saved.
drupal_set_message(t(' has been saved.'));
}else{ // If there's an error, $success will evaluate to FALSE, and the following code will execute.
drupal_set_message(t('There was an error saving your data. Please try again.'));
}
}
}
Inorder to edit form field you can reuse the form and set the default value field '#default_value'
storing the form submitted data fetched from database using the respective uniques ids.Refer the link for sample code.
https://www.drupal.org/node/2069383
I'm trying to add a class or ID to a link generated from my hook_menu code included below:
<?php
function mymodule_menu() {
$items['mypage_redirect'] = array(
'page callback' => 'mypage_redirect_callback',
'type' => MENU_CALLBACK,
'access arguments' => array("access content"),
'title' => 'My module generated page',
'options' => array(
'attributes' => array(
'class' => array(
'test-class'
)
)
),
);
return $items;
}
All I'm trying to accomplish a simple link with a class of 'test-class' so I can style it but the instructions I followed to get the above code has not worked.
function commons_menu() {
$items = array();
$items['test'] = array(
'title' => t("test"),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'options' => array(
'attributes' => array(
'class' => 'test-menu'
)
)
);
return $items;
}
Have cheked other forums too!!.. the attibutes doesn't work with MENU_LOCAL_ACTION or MENU_LOCAL_TASK
Your code looks fine.. did you try clearing cache after the changes?
I am developing a custom module by name 'form' in drupal 7.
I have placed my module folder in sites\all\modules\form.
And i have 2 files in 'form' folder. 'form.info' and 'form.module'.
'form.info'
name = Form
description = Form creation.
core = 7.x
And 'form.module' contains
<?php
function form_menu()
{
$items['form/examples'] = array
(
'title' => 'Form API Examples',
'description' => 'Examples of using the Form API',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_simple_form'),
'access callback' => TRUE
);
return $items;
}
function form_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Favorite Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('What is the favorite color?'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
)
return $form;
}
I am getting only blank white page when i click on the link.
Thanks in advance.
I works for me try this code for mymodule.module
<?php
// $Id$
function form_menu()
{
$items['form/examples'] = array
(
'title' => 'Form API Examples',
'description' => 'Examples of using the Form API',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_simple_form'),
'access callback' => TRUE
);
return $items;
}
function form_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Favorite Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('What is the favorite color?'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
When you create mymodule.info file
name = Form
description = Form creation.
core = 7.x
package = Form
Add package to it so you have your module in his own box. It is easier to find it betwen all of those modules.
Rename your module to form1, as it is conflicting with Drupal internal form module, located in /includes.
You forgot a semicolon here:
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
); //<- semicolon missing
I'd like to make a drupal page with a form. Something like the following, which doesn't render:
function score_table_menu() {
$items['table'] = array(
'title' => t('name'),
'page arguments' => array('table_page'),
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
return $items;
}
function table_page(){
$output .= t('Complicated Hello');
$header = stuff;
$rows = stuff;
$output .= theme_table($header, $rows);
return $output;
}
function table_form(){
$stuff_array = array (values);
$form['choice']= array(
'#type' => 'select',
'#title' => t('Select Stuff'),
'#options' => $stuff_array,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function table_form_validate(){}
function table_form_submit(){
drupal_set_message(t('Submitted'));
}
Is it possible to have half the module render a page and the other half a form? I've written out two page arguments. I don't see any difference between page argument and callback. Also might need to use drupal_get_form() to render the form on the page.
page callback
is the function that you want this URI to trigger.
page arguments
are any arguments that you want to sent to that function when the callback is called.
Your menu item should look like this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'table_page',
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
Or this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'drupal_get_form',
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
In your table_form, you can use other form types to display markup,
$form['table'] = array(
'#type' => 'markup',
'#markup' => theme_table($header, $rows),
);
Take a look in the API for a better understanding of hook_menu: http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_menu/6
I defined in this my menu in D6. I want to enter text field value on clicking on checkbox.
function mymodule_menu(){
return array(
'assignJob/js'=>array(
'page callback' => 'assignJob_js',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
)
}
function assignJob_assignee() {
$output='xxxxxxx';
drupal_json(array('status' => TRUE, 'data' => $output));
}
While making form, there is checkbox called assignJob
$form[$group]['assignJob'] = array(
'#type' => 'checkbox',
'#title' => 'Assign Job',
'#ahah' => array(
'event' => 'change',
'path' => 'assignJob/js',
'wrapper' => 'edit-name',
'method' => 'append',
'effect' => 'none',
'progress' => array(
'type' => 'throbber',
),
)
Wrapper is the id of text field
When i click on this chcekbox i am getting
An HTTP error 404 occurred.
/assignJob/js
Can anybody help me what am i doing wrong here?
it seems like the path /assignJob/js doesn't exist.
make sure you don't have a typo when you declared it in hook_menu
and make sure you flushed all the caches when you change hook_menu