Experimental Project

This is a sandbox project, which contains experimental code for developer use only.

This module exposes all variables defined with Variable API to JavaScript API.

Usage

  1. Checkout and enable module
  2. In hook_variable_info() define your variables to be exportable to JavaScript
  3. Access your variables from Javascript with code like:
    Drupal.settings.variable_js.variableName (note that changes will not persist).

Example handler

<?php
function mymodule_variable_info() {

 
$variables = array();
 
$variables['mymodule_info'] = array(
   
'title' => t('Info text'),
   
'description' => t('Sample variable with information'),
   
'type' => 'string',
   
'token' => TRUE,
   
'multidomain' => TRUE,
   
'js' => TRUE,
  );
return
$variables;
}
?>

Example JavaScript usage

function($) {
  Drupal.behaviors.mymodule_test = {
attach : function(context) {
                        alert(Drupal.settings.variable_js.mymodule_info);
}
}
})(jQuery);

Note: examples were not tested on animals ;-).

Dependencies

Requires Variable.

To do

Some ideas for improvement:

  1. Allow exporting to JS of variables from GUI (add a checkbox in variable GUI that enables any variable to be exportable to JS)
  2. Allow saving of variable modifications made in JavaScript (some AJAX magic will be required)

Currently I don't have time / motivation / need for these, but if you find any of them interesting - let me know / create a feature request issue.

Maintainers for Variable JS

  • lklimek - 3 commits
    last: 16 weeks ago, first: 16 weeks ago

Issues for Variable JS

To avoid duplicates, please search before submitting a new issue.
All issues
Bug reports
Statistics (2 years)
New issues
Open bugs
Participants