Entries in the ‘Web development’ Category:

Specializing Theme Hooks in Custom Drupal 7 Modules

An important feature of Drupal 7 is the clean separation between the structure of elements created by modules and the rendering of those elements by themes. One cool thing is that a module can provide suggestions for the theme hook which should render an element. This gives theme writers an opportunity to override the theme for very specific elements in a module, and provides for more generic fallback themes otherwise.

The theme hook suggestions are lists of increasingly specific keys separated by a double underscore, for example node__mymodule__block5. The first entry, node here, is a top level hook defined by a hook_theme function. When the theme function is called, the theme system will try the most specific hook first, in this example node__mymodule__block5. If that doesn’t exist, it will try the next hook, node__mymodule, and so on until it gets to the top level hook. Themes that implement these specific hooks with template files will replace the underscores with hyphens, so node__mymodule__block5 would be rendered by node--mymodule--block5.tpl.php.

Module developers can set a theme hook suggestion for an element by using the #theme key of a render array. The keys of the render array will have to include any parameters that the top level hook requires. These parameters are defined when the hook is declared. Hooks in Drupal core have their parameters in the Drupal API, and hooks from custom Drupal modules should be documented in the module. Here is a nice list of default theme hooks in Drupal core

Here is an example that creates a list using a theme suggestion derived from the Drupal core hook item_list, getting the elements from some function get_items.


function mymodule_block_view($delta = "")
{
    $block = array();
    $block["subject"] = t("My Awesome List");
    $block["content"] = array(
        // item_list is the Drupal core theme hook for unordered or ordered lists
        "#theme" => "item_list__mymodule__awesome_list",
        // #type and #items are parameters for the item_list theme
        "#type" => "ul",
        "#items" => get_items(),
    );

    return $block;
}
 

Tags: , ,

Leave a Comment


Inside Drupal are All the Stars in the Sky

I have drilled deep into Drupal and come out the other side, dripping with offal and frustration. It is an incredibly powerful framework, with an easy, standard way to modify literally anything in the system at any point during the page flow. Of course, finding the easy way is the hard part, because there are also ten wrong ways to do anything you want to do, and the API reference is written as more of a gentle reminder to the API authors that relegates the uninitiated to swapping war stories in the comments on each page. I still love it, though, especially now that I know the secrets, which I will be sharing here soon.

Tags:

Leave a Comment


Flash Game Monstrosi Stacks

I released a new Flash game, Monstrosi Stacks. It’s a take on the match-3 genre where you drag whole columns instead of swapping pieces. It features cute little monster-things I’m calling Monstrosi. There are some more games in the works that will feature the Monstrosi, so I hope to build them into something of a brand that will support secondary products.

Read the rest…

Tags: ,

Leave a Comment


Double Pendulum Simulation

I’ve been playing around with some of the new features in HTML5, particularly to see how the canvas stacks up to Flash. One of the things I wanted to test was javascript performance, so I ported this Flash toy I wrote a few years ago. It’s a physical simulation of a double pendulum system. It’s interactive, and it can export the line drawing it produces as a PNG.

How did canvas+JS do? The export was a lot easier: I had to write a PNG encoder in Actionscript for the original version! Pretty much everything else was harder. Canvas has features similar to Flash 5, and I missed modern Flash’s rich standard library. CSS layout is still somewhat inferior to Flex for GUI design, as the layout options are less flexible.

One of the appeals of canvas is mobile support, but I was disappointed by the performance on my Motorola Droid. Just clearing the background on a canvas larger than 500×200 took the frame rate to single digits, and I couldn’t find a reliable way to make the canvas fill the screen if other elements were present (I didn’t look too hard, since a canvas that large was unusable). The javascript performance was fine, it was just the drawing that caused problems. Let me know if you get better results on different hardware, I’d love to know that this can work better.

Overall, canvas shows promise, but I don’t think it’s ready to replace Flash for complex graphical applications.

Want to embed this on your own site? I’ve put an embeddable double pendulum simulation at Clockwork Magpie Studios.

Tags: , , , ,

Leave a Comment


Spin: The Game

Spin is a new Flash game I’ve been noodling on for a little while. It’s a Puzzle Bobble type game, except that the balls can freely spin like a pinwheel. The rotation is physically accurate, so as balls fall off of the board it changes speed and direction. When a ball hits the board the ball’s momentum is transferred to it, so if the board is turning slowly you can speed it up or make it turn the other way by firing the balls at it.

The feature list was miles long, but I’m getting too busy to work on it. Look forward to a second version when I can get back to it.

Tags: ,

Leave a Comment