R – Pass variable from module to theme template when using drupal_render

ajaxdrupal

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:

  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.

Related Topic