So I need to AJAX-reload part of a page, and so I'm building a little module to just return drupal_render($node->content['the_field']). Which seems to work fine, it's returning the same output as what was originally on the page.
The problem is that I need to send a parameter from the module to the theme layer to change some imagecache preset names depending on some states on the page.
What would be the best way to move data from the URL of my AJAX request through the module into the field's theme template?
Best Answer
Assuming that your module registers a path for the callback via
hook_menu()
, I see two ways, depending on how 'big' the returned chunk of Markup needs to be:If you only need the themed image for your AJAX operation, it might be easiest to drop the
drupal_render($node->content['the_field'])
in favor of callingtheme_imagecache()
directly:Obviously, you'd need to fetch the image/field information yourself before this, and set $presetname according to your URL argument. In your frontend js, you'd need to adjust the replacement logic to act only on the image, as opposed to swapping the whole field markup.
If you need the whole field markup as suggested by your question, I'd implement an override of
theme_imagecache()
, adding the logic to change it's $preset variable based on direct inspection of the path:This is the 'quick&dirty' way. If you want to heed the 'avoid usage' warning given in the arg() function documentation (and also avoid using a global variable), you could 'pass' the preset name to the theme override by means of a static variable in a helper function:
You would call this first from your AJAX menu callback, passing the preset override determined from the URL, so it gets stored in the static variable. In your
theme_imagecache()
override, you'd call it again without any parameter. If it returns NULL, you just proceed as normal, using the 'standard' preset. If it returns something, you'll use that instead of the default, as now you know that this is a request made to your AJAX callback.