I have created custom module called 'mymodule'.

In this URL localhost/es/mymodule/name/1/2/3, I have this field:

$form['name'] = array(
  '#title' => t('Enter name'),
  '#ajax' => array(
    'event' => 'blur',
    'callback' => 'mymodule_ajax_callback',
    'wrapper' => 'my-wrapper',
    'method' => 'replace',

My ajax callback is successfully called:

function mymodule_ajax_callback($form, &$form_state) {
  return ...

However, inside 'mymodule_ajax_callback', I need to get the current URL arguments of the page such that arg(1) = 'name', arg(2) = 1, arg(3) = 2, and so on.

By nature, Drupal uses the URL system/ajax so calling arg(N) does not work as I need it to be.

How to get the current URL arguments using arg() inside the ajax callback?


I thought of using $_SESSION[...] = arg(1); $_SESSION[...] = arg(2); ... on my form and calling the session variables on my ajax callback. But I am looking for a better way without using session but only using drupal methods if available.

  • If the current URL arguments were applied by form submition (by applying some exposed filters for example) you can easy get all of them from $_GET as well.
    – Jack-PL
    Sep 27, 2013 at 13:25

3 Answers 3


The arguments passed to the page callback are saved in $form_state['build_info']['args'], with the exception of the form ID.

If you use the following definition for the menu item, you would get them there. (Replace 'form ID' with the form builder ID.)

$item['name/%/%/%'] = array(
  // …

  'page callback' => 'drupal_get_form',
  'page arguments' => array('form ID', 1, 2, 3),

  // …



Create the hidden field in the form & store the current url as below:

$current_url = arg(1);
$form['current_url'] = array('#type' => 'hidden', '#value' => $current_url);

And that than you can access the above hidden field in the ajax callback as $form_state['values']['current_url']

The above method worked for me.

  • There is no need to use a hidden field, since the value doesn't need to be passed to the browser.
    – apaderno
    Sep 27, 2013 at 15:38
  • 1
    Yeah right, and thanks for providing the better solution!! Sep 27, 2013 at 16:04

This is the trick I use for the OpenLayers module:

function openlayers_ui_admin_settings($form, &$form_state) { $form['openlayers_default_map'] = array( '#type' => 'select', '#title' => t('OpenLayers Default Map'), '#description' => t('This is the default map that will be used in cases where one is not defined. It will also be used as the default when creating new maps.'), '#options' => $options, '#default_value' => variable_get('openlayers_default_map', 'map1'), '#ajax' => array( 'callback' => '_ajax_reload_default_map', 'method' => 'replace', 'wrapper' => 'default_map_ajax', 'effect' => 'fade' ); $form['current_path'] = array('#type' => 'value', '#value' => current_path()); return system_settings_form($form); } function _ajax_reload_default_map($form, &$form_state) { $_SESSION['current_path'] = $form_state['input']['current_path']; $map = openlayers_object_load('map', $form_state['values']['openlayers_default_map']); $form['default_map']['map'] = array( '#markup' => openlayers_render_map($map) );; return $form['default_map']; }

Instead of using $form_state['values'], I use $form_state['input'], it doesn't get updated through the AJAX calls and the current path remains correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.