4

I'm using the flot module to create some graphs in Drupal. Generally, it is working pretty well. However, I have come across an issue which is more general.

I am defining a settings array, which works for most things, but some flot settings allow for anonymous functions:

  xaxis: {
    transform: function (v) { return Math.log(v); },
    inverseTransform: function (v) { return Math.exp(v); }
  }

How could I encode this in PHP so that it will be output correctly? The flot module uses drupal_to_js(), and I can't put it outside of quotes as PHP will try to interpret it. If I put it inside quotes it will be treated as a string.

From my googleing it looks like json_encode() also doesn't support this.

A little more explanation: drupal_to_js() takes a PHP structure and transposes it to JavaScript.

  $top = array(
    'a_string' => 'A string will convert fine',
    'a_number' => 123 // as will a number,
    'an_array' => array('a' => 'nested arrays work fine too'),
  );

This will be transposed to something like

 [ a_string: 'A string will convert fine', a_number: 123 ...

I want to transpose a function; the following code will give me the string 'function (v) { return Math.log(v); }'.

  $top = array(
    'a_string' => 'function (v) { return Math.log(v); }',
  );

The following code will give me a PHP parse error (as it should be).

  $top = array(
    'a_string' => function (v) { return Math.log(v); },
  );

I want the text in the string to be used as a JavaScript literal, but I don't think it can be done.

Has anyone found a good way round this? I could output some raw JavaScript, but then I will lose a lot of functionality.

2
  • What are you trying to achieve, and why cannot you use drupal_to_js()?
    – avpaderno
    Commented Mar 22, 2011 at 18:16
  • This doesn't solve your problem, but you're confusing JSON and Javascript. The snippet in your question isn't JSON, which is why the encoder doesn't work. You can verify this yourself by running the following in the webkit console: JSON.parse('function (v) { return Math.log(v); }'); the result will be SyntaxError: JSON Parse error: Unexpected identifier "function".
    – Dylan Tack
    Commented Nov 30, 2011 at 1:24

2 Answers 2

2

This guide can be used as a work around http://echodittolabs.org/blog/2011/04/setting-js-callbacks-drupal-json-and-some-jquery-sauce-jcarousel-controls

We are looking for a generalized solution to this here http://drupal.org/node/1148204

1
  • Thanks for the links. I can see how this would work. Will probably look at the advagg module too. Commented Jun 8, 2011 at 14:05
1

If I understand the problem correctly, you have two options here.

The first is to use drupal_add_js with the inline option set to true. drupal_to_js() deals specifically with transforming PHP variables into their JavaScript equivalent; in this case, you aren't dealing with variables, but rather with functions, so it's not a proper fit.

The second option (which I probably favor) is to not use anonymous functions at all. Instead, create your own JS file which looks something like this:

// Give yourself a namespace to play nice with other JS.
if ( !ProjectName.flot ) { ProjectName.flot = {}; }

ProjectName.flot.transform = function (v) {
  // …
}

ProjectName.flot.inverseTransform = function (v) {
  // …
}

From there, your code to call these functions would look more like:

xaxis: {
  transform: ProjectName.flot.transform(v),
  inverseTransform: ProjectName.flot.transform(v)
}

Which would still be added via drupal_add_js(). I prefer this method because maintaining your JS is a lot easier/more logical if it's in its own file. Either methods should work, though.

1
  • I'll have to rephrase my question. This dosn't quite answer it, Commented Mar 23, 2011 at 10:14

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.