1

I have a path alias like below

$node_alias = '/node/add/page?id=1';

what i need is have to append query parameter to a destination url with above url,i have tried below but doenn't work

$node_alias = '/node/add/page?id=1&destination=/admin/content?id=1

Any solution?

EricLavault
  • 12,130
  • 3
  • 23
  • 45
Moby M
  • 910
  • 2
  • 7
  • 26
  • Slashes need to be 'encoded' in URL query strings see: https://www.geeksforgeeks.org/php-urlencode-function/ for more details on how to do this in PHP – Ben Winding Mar 26 '20 at 11:48

1 Answers1

0

You need to url-encode each component in the query string.

The first parameter id=1 is fine, but the destination parameter contains special characters used for delimiting uri components : /, ?, =.

You can use urlencode() or rawurlencode() :

$node_alias = '/node/add/page?id=1&destination=' . rawurlencode('/admin/content?id=1')

There is a drupal way of doing this, using drupal_http_build_query() (d7):

$path = current_path();
$params = drupal_get_query_parameters() + array('destination' => '/admin/content?id=1');
$query = drupal_http_build_query($params);

$node_alias = $path . '?' . $query;

See also urlencode vs rawurlencode?

EricLavault
  • 12,130
  • 3
  • 23
  • 45