snippet

Generate UUID in Drupal 8

Drupal 8 is full of UUIDs. Sometimes it's useful to generate one for use ourselves.

Keep a record of important events on your Drupal site

Quite often a site needs to log important events from custom code. Use the watchdog. I'm getting slightly less descriptive in my introductions.

Saving a single field of a node/entity

Sometimes doing a full node_save/entity_save on an entity is overkill, like when updating a single field's value and not needing anything else to change.

This can be achieved by doing part of the complete entity_save function - prepare, update, and flush caches.

Set a Drupal module's weight on install using db_update

The weight of a module determines the ordering in which it will be called during callback hooks. eg Given 2 modules which implement hook_menu_alter(), the one with weight 0 will run before the one with weight 10.

If you know the weight that a module needs to be, and you can be very sure that no other module might get in there and change it, it can just be set statically.

Views set filter programatically

Code which is meant to set it (works in most cases), but which ended up not working for me in one instance which is kinda annoying. Ended up using a contextual filter instead which with programming, is a much easier way to go.

Default values on form input elements that disappear when clicked on

Having an example or description value inside the form element for input elements that are missing a title or description. With this small code snippet as an example, this can be done in Drupal forms quite easily by adding these attributes to the form input item array.

Drupal 6 themed images with ImageCache and Thickbox or Lightbox

This is mostly because I can't remember how to do this all the time.

The ImageCache preset is 'vpthumb' in this case.

Theme images in Drupal 7 - The new imagecache is image styles

ImageCache was the Drupal 6 way of doing things when you wanted a modified version of an already existing image on the site. Instead of manually manipulating the image and keeping a copy of it manually, you could setup an ImageCache preset which would be used to manipulate the image automatically.

In Drupal 7, this functionality is now in the core and it has also gone through some subtle changes to keep up with changes in the core.

Set and Get Drupal cache data

Drupal has an inbuilt caching system, but it needs to be used effectively to get any benefit from it. For data accessed many times during the same request, data should be statically cached. If it's consistent over a longer period of time, it should also be added to the core cache.

The following is my template code that I used any time I need to pull something from the cache. I modify it based on what I expect to happen to the data.

Separating out form_alter functions in Drupal 7 modules

Separate out files ... reduce code loaded for most pages since main module files are loaded for every page load. Moving all code that isn't needed on every page load to different files speeds things up. Maybe. It sounds good in theory.

To make Drupal search for the default form_alter, and any other custom form, the module needs to hook_hook_info_alter().

Subscribe to RSS - snippet