Pages

3 May 2013

Drupal behaviors: A quick how to


If you’re adding JavaScript to your custom module it is very easy and tempting to simply add it like this:
jQuery(document).ready(function($){ alert(‘hot dog flavored water’); }); 
Now this code works perfectly fine but what if your JavaScript needs to be executed on page load and after an AJAX request? Imagine you have a view that uses “Views Infinite Scroll” and you want add a CSS class to every result like this:
jQuery(document).ready(function($){ $('.view-display-id-page .views-row').addClass('fancy-pants'); }); 
This will work for the results that are displayed initially but for all the results that are loaded by Infinite Scroll's AJAX call the class is not added. That’s where Drupal behaviors come in handy.  The behaviors will be executed on every request including AJAX requests, so let's do the equivalent of the code above but this time using this method:
Drupal.behaviors.infiniteScrollAddClass = { attach: function (context, settings) { $('.view-display-id-page .views-row').addClass('fancy-pants'); } }; 
I admit that was quick - so here are some explanations:
  • infiniteScrollAddClass: This is your namespace and should be unique. For example, this is typically the name of your module, but it isn't mandatory.
  • context: This is actually really really cool, on page load the context will contain the entire document and after an AJAX request will have all the newly loaded elements. This way you can treat content that is loaded in via AJAX differently than others.
  • settings: This contains information passed on to JavaScript via PHP, it is similar to accessing it via Drupal.settings. For further comprehension I recommend this source.
There obviously are cases where some functionality should not be executed on every request. In such a case its great to use jQuery's .once() method. So let's say we want to give all the initially loaded results in our view an additional class, for something like this we would proceed like so:
Drupal.behaviors.infiniteScrollAddClass = { attach: function (context, settings) { // these are the elements loaded in first $('.view-display-id-page').once(function(){ $(this).find('.views-row').addClass('i-was-here-first'); });  // everybody $('.view-display-id-page .views-row').addClass('fancy-pants'); } }; 
This will add the class “i-was-here-first” to all the view results present on page load, everybody else joining in via AJAX will just get the “fancy-pants” class.
So that’s a quick look at Drupal behaviors, if you haven’t used it do use it!
If you are looking for additional theoretical insight into this topic I can recommend these two sources for further reading:


5 Drupal Security Tips


Drupal is one of the most secure content management platforms around. Despite its high level of security, it is still vulnerable to certain types of security problems. Many of the potential security problems are preventable if you know what to look for. Here are five security tips for Drupal.
1. Security Updates
Some security problems are actually just loopholes that are found in certain developmental versions of the platform. Security loopholes can actually open to door to malware, hacking, and other attempts to compromise the security of the website. One easy solution is to stay with the recommended distributions until the developmental versions have been stabilized. Be sure to keep everything up to date whenever new releases are available because they will usually have the security solutions that you need to keep your site secure.
2. Be Selective About 3rd Party Modules
Third party modules can help your website run faster and offer some helpful features, but you should use them with a grain of salt. Unfortunately, there are some third party modules may actually create some security issues that may not have been present otherwise. In order to prevent this, it is recommended that you carefully review each one before installation. Try to stick with the ones that are recommended by other users and be sure to look through it yourself to see if there are any problems.
3. Regularly Change Passwords
You should always use tough passwords to ensure the security of your website, but another thing you should be doing is changing your passwords regularly. Hackers can penetrate your system if you use the same passwords continuously, which is common as many people do that because it can be difficult to remember a lot of passwords. It is vital that you change your passwords at least every 3 months for all of your entire system.
4. Monitor Other User Accounts
 Although it is perfectly fine to have other users, their privileges should be carefully monitored and restricted. Sometimes security issues can occur when other users inadvertently let in malicious code or even invite other users. Make sure that they must have permission to make posts, change settings, or add code to your website.
5. Backup, Backup, Backup
As always, backing up yours system is essential. If you do not backup your system, then it will be much more difficult to recover from a security issue that has significantly altered your website. The best solution to this problem is to perform regular backups. Go through all of the steps to make sure that your information is still there.
Securing your Drupal website is relatively easy to do as long as you follow these general guidelines. Be careful about what you allow other users to do with your site, backup your information, change your passwords, update your modules, and be selective about using third party modules. If you do all of this your site will more than likely stay safe.