tags:

views:

326

answers:

2

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?

+1  A: 

you can use one of the preprocess hook in your module ie.

function modulename_preprocess_page(&$vars) {
  $vars['myvar'] = $myvalue;
}

but i suspect you'd better override some imagecache themeable function in your theme directly.

gpilotino
+2  A: 

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:

  1. 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 calling theme_imagecache() directly:

    return theme('image_cache', $presetname, $path, $alt, $title, $attributes, $getsize);
    

    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.

  2. 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:

    function theme_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
      // NOTE: Assuming an AJAX callback path of 'your/ajax/callback/preset_name'
      // Get all path elements
      $path_elements = arg();
      // Pop the last element, as it will be the preset, if the rest matches below
      $preset_name = array_pop($path_elements);
      // Prepare the rest for comparison below
      $leading_path = implode('/', $path_elements);
      // Do we have a preset, while being on the right path?
      if (isset($preset_name) && 'your/ajax/callback' == $leading_path) {
        // Yes, adjust the preset
        $preset = $preset_name // might need conversion from path element to preset name first, if not the same
      }
      // Add copy of original theme_imagecache function here ...
    }
    

    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:

    function yourModule_imagecache_preset_override($override = NULL) {
      static $preset;
      if (isset($override)) {
        $preset = $override;
      }
      return $preset;
    }
    

    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.

Henrik Opel
Well I'm changing the imagecache presets, but I'm returning way more than just the image, I'm returning a whole CCK multigroup.The static variable sounds interesting, I'll have to see if I could integrate that somehow.
Karl
Should you end up using the static variable version, you can make it a bit more 'intuitive' to use by adding an explicit 'setter' function, turning the above example into a 'getter'. The setter version would be the only one calling the getter version with a parameter. You can find this 'pattern' applied regularly within Drupal core - for example with the `menu_set_item()`/`menu_get_item()` couple.
Henrik Opel
This worked like a charm! Thanks much!
Karl