Skip to content

Instantly share code, notes, and snippets.

@khalib
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save khalib/9476040 to your computer and use it in GitHub Desktop.

Select an option

Save khalib/9476040 to your computer and use it in GitHub Desktop.
How to communicate from the backend to javascript in Drupal 7.
(function($) {
Drupal.behaviors.drinks = {
attach: function(context, settings) {
var drinkType = settings.drinks.page_type;
alert('This page is of type: ' + drinkType);
}
}
})(jQuery);
<?php
// assume you have access to a $node object.
switch($node->type) {
case 'beer':
$type = 'beer';
break;
case 'soda':
$type = 'soda';
break;
default:
$type = 'water';
}
drupal_add_js(array('drinks' => array('page_type' => $type)), 'setting');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment