Hello all,
I am working on a module. I display some data along with some option in the end columns. One of the options are edit. when the user clicks on edit, it takes him to the edit form. I am making the edit string link as below.

$edit_string   = l('Edit', 'admin/settings/split_test/edit/'.$data->stid);<code>. 

The menu code for this edit is given as below.

<code>
$items['admin/settings/split_test/edit'] = array(
	'title' => 'Split_Testing edit',
	'description' => 'Edit the split test.',
	'page callback' => 'split_test_edit',
	'page arguments' => array('split_test_edit_settings'),
	'access arguments' => array('administer site configuration'),
	'type' => MENU_CALLBACK,
	);

on this path, i have a function split_test_edit as below

function split_test_edit($stid) {
  $output = t('Edit your split test information below.');
  drupal_set_message($stid);
  return $output;
}

I want to use this $stid, which was there on the link, but i am not getting it here in the edit fucntion therefore, i am displaying its value here to see, if i got it, but i am not able to display this $stid, which means that i am not passing it correctly. obviously i am not doing it correctly, but you might have gotten the idea of what i am chasing here. kindly help me

Comments

marcvangend’s picture

I'm not 100% sure if I completely understand your question, but I think you should start reading some documentation about arguments in the menu system. IMO it's best explained here: http://api.drupal.org/api/function/page_example_menu/6.

shanto’s picture

Try this


function split_test_edit() {
	$stid = arg(4);
	$output = t('Edit your split test information below.');
	drupal_set_message($stid);
	return $output;
}

Syed_Shah’s picture

Wow......... it worked, but could you explain it please. what is this arg(4)?.

Thanks a lot

shanto’s picture

arg() is a drupal function. Drupal make parameter from URL. see API details http://api.drupal.org/api/function/arg

Suppose, your url is domain.com/admin/settings/split_test/edit/

arg(0) = admin
arg(1) = setting
arg(2) = split_test
arg(3) = edit
arg(4) = your parameter

I hope you got it ;).

marcvangend’s picture

I'm sorry, but that is not the recommended method.
Using arg(4) in you function will make your code hard to read. Even more important: in case you need to re-use your function, the variable might not be available as arg(4) at all. It's better to do it like this:

In your hook_menu:

$items['admin/settings/split_test/edit/%'] = array(
  'title' => 'Split_Testing edit',
  'description' => 'Edit the split test.',
  'page callback' => 'split_test_edit',
  'page arguments' => array(4),
  'access arguments' => array('administer site configuration'),
  'type' => MENU_CALLBACK,
);

Notice how the 'page arguments' property passes on arg(4) to the split_test_edit() function.
By the way, instead of outputting variables with drupal_set_message, I recommend installing the devel module and use dsm($stid);.

malks’s picture

I realise that it is not recommend for readability, but is anyone aware of how to avoid arg() if you're not sure how many arguments you're going to get?

Anthony.

marcvangend’s picture

Good question, I had the same issue a couple of weeks ago. I haven't done hours of research, but indeed I ended up using arg() instead of passing variables in hook_menu.