Last active
August 29, 2015 13:57
-
-
Save khalib/9476040 to your computer and use it in GitHub Desktop.
How to communicate from the backend to javascript in Drupal 7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function($) { | |
| Drupal.behaviors.drinks = { | |
| attach: function(context, settings) { | |
| var drinkType = settings.drinks.page_type; | |
| alert('This page is of type: ' + drinkType); | |
| } | |
| } | |
| })(jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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