Drupal

Vertical Tabs in render arrays without the Form API in Drupal 7

This is a quick reminder to myself so I don't forget in future. Using Vertical Tabs within forms is fairly easy as it's part of the form API. It was a little harder for me to work out how to do it without using the Form API and using only a render array. The example below shows how to split up content using Vertical Tabs from within a render array:

Caching Render Arrays in Drupal 7

Render arrays in Drupal are great. If you do not know what a render array is then take a look here http://drupal.org/node/930760, but basically they are arrays that convert into HTML. Render arrays are used to allow other modules to override output before going through the theme layer. What happens though if you have a render array that does a lot of processing? You do not want that array, or part of that array, running each time the page is called.

Here I describe how to cache a render array. Firstly, we need a render array:

Themeing a form into a table with draggable rows in Drupal 7

What I want is a form themed into a table, but I also want to to make the rows draggable so I can change the weight of each form entry. Below is an example of one way this can be done in Drupal 7. As an example, lets say we have an array of the main characters in the BBC series Red Dwarf (not including Holly). We want to display those characters into a form so our user can change those details, if they so wish.

Applying Javascript behaviours once in Drupal

Drupal uses Drupal.behaviours as a replacement for jQuery.ready, but unlike jQuery.ready it can run multiple times during page execution or whenever new DOM elements are inserted into the document. This can be a problem when we want code inside the Behavior to run only once. This is important if you want to create a new instance of an object, for example. What I used to do in the past (for Drupal 6) was add a class to an element to know if the code had been processed or not:

Override the theme list item function to add odd/even zebra striping

I think list items should, or have the option to, include zebra striping. For now, it easy enough to just override theme_item_list() and add that ourselves. Place the following code in your themes template.php file, remembering to change the hook to the name of your theme.

Describe field properties to make custom fields work with Entity Metadata Wrappers

I've created a custom field using the Field API and it works great. However, I want to use the Entity module and its Entity Metadata Wrapper to quickly access Entity data, including data from the new field I've created. If you try to access data from the new field you will see an error message similar to the below.

EntityMetadataWrapperException: Unknown data property field_example_rgb. in EntityStructureWrapper->getPropertyInfo() (line 339 of mysite\sites\all\modules\entity\includes\entity.wrapper.inc).

Merging two forms together in Drupal

Sometimes in Drupal I want to join together two forms, or in other words, prepend a form to another. For example, let say I have three user forms, all different but all contain the same fields to obtain the user address. One day I want to add an address lookup button in the address part of the form, but I'm forced to do the work on all three forms. What if I could create a separate form for just the address, then merge/add/prepend it to other forms. This way, I just need to change the address form and the changes would be reflected in all three of my forms. Here is an example:

Drupal is Sexy

If Drupal was a woman, you definitely would!

menu_get_object() vs node_load() when getting the node object in Drupal 7

Sometimes, we need to get node information about the current node we are viewing. In Drupal 6, it was always recommended to use menu_get_object() rather than node_load() to get the node object - the node object has all the information about the node. The reason being that during the inital page request to the node, the node object is cached statically in the function menu_get_item(), a function used by menu_get_object(). So the next time we call menu_get_object(), we are in fact getting a cached version of the node object, making it much faster than node_load().

How to debug AJAX in Drupal using FirePHP

I've been using the AJAX Framework in Drupal quite a lot lately, and it's always been fairly steady going, but recently my code has become more and more complicated. I thought it was time to find a good way viewing all those PHP arrays and objects during AJAX calls, so I can see how they are changing and if my code was doing what it was supposed to be doing. FirePHP is what I ended up using and below describes how I got it working with Drupal.