Drupal ships with a powerful method of packaging and redistributing your platform configuration called Install Profiles. It is similar to how Linux distro’s work, where you have the same core operating system, but extensive customization results in a different experience (Fedora, Ubuntu, Debian, etc). This allows us to employ rapid application development (RAD) after finalizing a build of a customized Drupal application.
There are some ‘gotchas’ though if you develop custom Drupal modules like we do. During the install process, Drupal only bootstraps the bare minimum to get the job done. I’ve noticed that if a module’s install routine calls functions outside of the .install file, the expected result tends to fail because files other than .install are not included during this loading process. It seems there is a special case for the file to be included, or you have to specify in your install profile to explicitly include the file (which takes a lot of time to do). To be specific, I have seen this happen with Drupal Boost, Drupal Gallery Assist, and our custom module Billboard, which tipped me off to this problem. The issue has since been fixed in Boost (and Billboard).
For us, the issue was creating custom ImageCache presets during a module’s install routine. This worked perfectly if you were going to the Drupal Modules page and enabling Billboard. What happens was the hook_install() function fired, included a definition file, and created presets based on that file. This fails during a Drupal install profile, where you use a script to install and configure Drupal automatically. This same issue occurs in Gallery Assist, and here is how I got around it:
It was pretty apparent that the code needed to come out of the install function. However, I needed to be sure that where I put the code would be compatible to the module loading process, and I’d need to be sure ImageCache was both installed and loaded before running the following code. Otherwise it would error out, and no ImageCache presets would be created.
Drupal has a crucial hook for modules. hook_init() allows you to perform tasks every time the module is loaded. In this case, I wanted to tell it to run a function and check for two default ImageCache presets. If they didn’t exist, create them. This approach solves three issues:
- Getting around install profile limitations
- Provide default ImageCache presets for Billboard module when it is enabled
- Recreate the ImageCache presets if they are accidentally deleted by the user (or developer
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | function mymodule_init() { mymodule_create_default_imagecache_presets(); } function mymodule_create_default_imagecache_presets() { $default_size = imagecache_preset_by_name('mymodule-default'); $thumb_size = imagecache_preset_by_name('mymodule-thumb'); if (count($default_size) == 0 || count($thumb_size) == 0) { if (count($default_size) == 0) { $mymodule_default_size = "600"; $mymodule_thumbnail_size = "80"; $presets = array(); // Default size. $presets['default'] = array ( 'presetname' => 'mymodule-default', 'actions' => array ( 'weight' => '0', 'module' => 'mymodule', 'action' => 'imagecache_scale', 'data' => array ( 'width' => $mymodule_default_size, 'height' => '', 'upscale' => 0, ), ), ); imagecache_preset_save($presets['default']); $presets['default']['actions']['presetid'] = db_last_insert_id('imagecache_preset', 'presetid'); imagecache_action_save($presets['default']['actions']); imagecache_preset_flush($presets['default']); imagecache_presets(TRUE); } if (count($default_size) == 0) { $presets['thumb'] = array ( 'presetname' => 'mymodule-thumb', 'actions' => array ( 'weight' => '0', 'module' => 'mymodule', 'action' => 'imagecache_resize', 'data' => array ( 'width' => $mymodule_thumbnail_size, 'height' => $mymodule_thumbnail_size, 'upscale' => 0, ), ), ); imagecache_preset_save($presets['thumb']); $presets['thumb']['actions']['presetid'] = db_last_insert_id('imagecache_preset', 'presetid'); imagecache_action_save($presets['thumb']['actions']); imagecache_preset_flush($presets['thumb']); imagecache_presets(TRUE); } } return; } ?> |
What that says basically is ‘hey, do our default ImageCache presets exist?’ if not, it will create them at runtime.
I have tested this a handful of times with positive results, and, it works with install profiles since hook_init() is called after Drupal loads. Food for thought for all you Drupal developers out there. If there is a better way to achieve this feel free to leave a comment.
