Variable JS

Component ID

1899988

Component name

Variable JS

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Component created

Component changed

Component body

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


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.