开发者

Drupal get parameters from url in edit form and populate the form

i have an employee table with fields like id, name, age and salary.

Iam showing the list of employee names in my custom module and when i click on an employee name, i have to show the edit form of that employee.

The employee names are given a link, as like:

<a href='".$base_url."/my_module/edit/".$employee->id."'>

and the corresponding menu path is configured as:

$items['my_module/edit/%'] = array(
        'title' => t('My form'),开发者_如何转开发
        'description' => t('Edit employee'),
        'page callback' => 'my_module_edit',
        'access arguments' => array('access content'),
        'access callback' => TRUE,        
        'type' => MENU_CALLBACK,
    );

And when i click on the 5th employee name, the url would be: ..../my_module/edit/5

So now my questions are:

  1. How can i get the id (5) of the employee from the url in my my_module_edit function ?
  2. How can i show the edit form of the employee, populating the fields (by querying with id) ?


For question 2: You have got it mixed up a bit, you don't create a custom 'page callback' menu item for forms, you should use 'drupal_get_form' as the 'page callback' with the name of your form() function (my_module_employee_form) as the 'page arguments'.

function my_module_menu() {
  $items['my_module/employee/edit/%'] = array(
     'title' => t('Edit employee'),
     'page callback' => 'drupal_get_form',
     'page arguments' => array('my_module_employee_form', 3),
     'access arguments' => array('administer my_module'),
     'type' => MENU_CALLBACK,
   );
 }

function my_module_employee_form() {
  ... read database and generate form..
}

function my_module_employee_form_submit($form_id, &$form) {
  ... read form and update database ...
}

Then in the function my_module_employee_form() you generate the form and populate it, and in my_module_employee_form_submit() you read the values back and store them in the database.


$request_parts = explode('/', $_SERVER['REQUEST_URI');
$employee_id = intval(array_pop($request_parts));

That's for your question #1. Not sure I understand #2


For your first question, you can get the arguments passed to a callback by using arg(0), arg(1), arg(2) etc...

<a href='".$base_url."/my_module/edit/".$employee->id."'>

For this href arg(0) == 'my_module', arg(1) == 'edit', arg(2) == '5', take a look at arg()'s documentation.

You can also parse the query string your self by accessing the $_GET['q'] variable.

For your second question, if your employee table is a Drupal table you can just make the link be the edit link to the Drupal node, like href="node/5/edit". If it is a custom table you will have to create your own form by implementing hook_form()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜