Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In /views_datasource/views/theme/views-views-json-style-simple.tpl.php the following code:
if (isset($_GET[$jsonp_prefix]) && $jsonp_prefix) {
$json = check_plain($_GET[$jsonp_prefix]) . '(' . $json . ')';
}
guarantees that the prefix saved in the views settings here is never used:
It's easy enough to save a copy of views-views-json-style-simple.tpl.php in my theme to override the template, but shouldn't the code itself look like this instead?
if ($jsonp_prefix) {
$json = $jsonp_prefix . '(' . $json . ')';
}
Or maybe I'm just missing the point here? Under what conditions would you ever want a $_GET value here instead of the value set in the view?
Comment | File | Size | Author |
---|---|---|---|
#2 | views_datasource-2706493-2.patch | 2.69 KB | renat |
Screen Shot 2016-04-14 at 12.02.09 PM.png | 14.85 KB | codevoice |
Comments
Comment #2
renat CreditAttribution: renat as a volunteer commentedCode itself is more or less OK, but user interface is extremely misleading - to the point where it's not possible to use JSONP format unless you're already comfortable with it, and can read module's code.
"JSONP Prefix" input is actually to set URL parameter NAME, and with that name we can get prefix VALUE from the URL. For example, we should query Views Datasource-generated page from our client JS code like this:
https://yoursite.com/path/to/api?prefixName=myJsFunction
So you should put "prefixName" to "JSONP Prefix" input, and we'll get
myJsFunction({"nodes":[{"node":{"title":"Some text"}}]})
in response. So the module behaves in line with JSONP standard, client app should be able to specify JSONP prefix - as it's a name of the function, that will be called back on the client upon server response.
Here is the patch, that fixes "JSONP Prefix" input name and description, and adds JSONP-related instructions to README file.
Comment #3
NicholasSUsing isset() seems to be the number one bug I always see in Drupal modules.
I don't see why people don't always just use !empty() instead.
I maybe would also phrase it as....
'#description' => t('If used this will identify the callback URL parameter. This parameters value will be used to prefix the JSON into a JSONP format.'),