2

Okay, I am afraid this might be a 'duh!' question, but I am just having one of those days! Well, the problem is, I am getting the following error every time I have a query string in my url (https://www.google.com/?q=hello):

Recoverable fatal error: Argument 1 passed to drupal_http_build_query() must be an array, string given

I am modifying a custom module that someone else wrote and this is the code that is problematic.

$pieces = parse_url($href);
$pieces['html'] = TRUE;
$pieces['path'] = isset($pieces['path']) ? $pieces['path'] : '';
if (isset($pieces['scheme'])) {
  $pieces['path'] = $pieces['scheme'] . '://' . $pieces['host'] . $pieces['path'];
}

return l($image, $pieces['path'], $pieces);

This is what $pieces looks like:

scheme (String, 5 characters ) https
host (String, 13 characters ) www.google.com
path (String, 22 characters ) https://www.google.com/
query (String, 6 characters ) q=hello
html (Boolean) TRUE

Obviously, the problem is due to $query not being an array. I am wondering how best to rewrite the above code so that I can properly call the l() function with the query string and any other attributes that a user might put in there. Any and all help would be greatly appreciated. Thanks.

3 Answers 3

2

You need to use an associative array, e.g.

$pieces['query'] = array('q' => 'hello');

The arguments for l() are in no way related to, or compatible with, the return from parse_url(). To avoid confusion it'd be better to build up a separate options array to pass as the 3rd argument to l().

3
  • Yeah, I know, but the problem is, the url will not always be the same. The user can basically type anything in there. What this url does is, it basically links an image. So, the user can enter an image of their choice and also the link to the image. So, it might have a couple of queries or more, in there like: yahoo.com/?t='hello'&j='hi'. My head is just not getting around the idea of getting the queries and separating out the key value pairs into an associative array. Hmm..maybe the explode function?
    – SN_26
    Aug 26, 2013 at 11:16
  • 1
    That's just a basic PHP question, out of scope for this site I'm afraid. Search google for "php convert query string to array" and you'll find the answer
    – Clive
    Aug 26, 2013 at 11:22
  • Nevermind, I think I've got it. I thought there was some Drupal way of doing this. Thanks.
    – SN_26
    Aug 26, 2013 at 11:31
1

This is the re-worked drupally way to turn the query string into an array. The function drupal_get_query_array does the work for us:

$pieces = parse_url($href);
$pieces['html'] = TRUE;
$pieces['path'] = isset($pieces['path']) ? $pieces['path'] : '';
if (isset($pieces['scheme'])) {
  $pieces['path'] = $pieces['scheme'] . '://' . $pieces['host'] . $pieces['path'];
}

// We need to convert the query to an associative array before we pass it
// to the l() function.
if (isset($pieces['query'])) {
  $pieces['query'] = drupal_get_query_array($pieces['query']);
}   

return l($image, $pieces['path'], $pieces);
0

I would rather use the following code.

  $query = array();
  $url = parse_url($href);

  if (!empty($url[query])) {
    foreach(explode('&', html_entity_decode($url[query])) as $val) { 
      $token = explode('=', $val); 
      $query[$token[0]] = $token[1]; 
    }
  }

  $options = array(
    'fragment' => !empty($url['fragment']) ? $url['fragment'],
    'query' => $query,
    'absolute' => TRUE,
    'html' => TRUE,
  );

  return l($image, $url['path'], $options);

I assume the URL contained in $href is for the site running Drupal. Differently, the following code should be used.

  $query = array();
  $url = parse_url($href);

  $path = !empty($url['path']) ? $url['path'] : '';

  if (!empty($url['scheme'])) {
    $path = $url['scheme'] . '://' . (!empty($url['host']) ? $url['host'] : '') . $path;
  }

  if (!empty($url[query])) {
    foreach(explode('&', html_entity_decode($url[query])) as $val) { 
      $token = explode('=', $val); 
      $query[$token[0]] = $token[1]; 
    }
  }

  $options = array(
    'fragment' => !empty($url['fragment']) ? $url['fragment'],
    'query' => $query,
    'absolute' => TRUE,
    'html' => TRUE,
  );

  return l($image, $url['path'], $options);

I would avoid using the array returned from parse_url() since it could contain array elements that conflict with the ones used by modules that implement hook_url_outbound_alter().

3
  • 1
    Thanks for the re-worked code. Really appreciate it. After I made the post, I also used explode to split the queries. But someone finally gave me the Drupally way to do this. drupal_get_query_array() actually accomplishes this same task for us.
    – SN_26
    Aug 28, 2013 at 18:21
  • You should add that as answer, showing the code you used, and accept it. If you do that, I will upvote it, since it deserves to be the first answer visible.
    – apaderno
    Aug 28, 2013 at 18:54
  • Have added the drupal way to get this done. Sorry for the delay in posting this, was away for a few days.
    – SN_26
    Sep 5, 2013 at 7:35

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.