You are here

function scald_render in Scald: Media Management made easy 6

Same name and namespace in other branches
  1. 7 scald.module \scald_render()

Render a Scald Atom

"Rendering" takes an Atom and generates output (typically XHTML) based on the "Context" which is specified. The Context is essentially a template.

NOTE: scald_render() gets called recursively until a viable output is generated. It may return FALSE on a given call, but it will ultimately fall back to the 'title' Context which is provided by Scald Core and *always* generates *something*. Even if a

Parameters

$sid: A Scald ID *OR* a Scald Atom.

$context: A valid Scald Context slug (a text string)

$options: An optional text string specifying additional Scald Context-specific options that get passed to the Scald Context for rendering (e.g. a mechanism for specifying the size of an image).

$rebuild: Set to true to force a rebuild of the rendering.

Return value

The rendered Scald Atom (usually an XHTML string ready for display) FALSE if the rendering failed for whatever reason

9 calls to scald_render()
atom_reference_textfield_process in atom_reference/atom_reference.module
Process an individual element, building the actual form api components needed for this widget.
mee_textarea_process in mee/mee.module
Process an individual element.
scald_debug_console in ./scald.debug.inc
Callback for Scald Debugging Console
scald_dnd_library_add_item in scald_dnd_library/scald_dnd_library.module
Adds an item in the library array.
scald_views_handler_field_representation::render in includes/scald_views_handler_field_representation.inc
Renders the atom according in the context specified in the option form.

... See full list

File

./scald.module, line 2221

Code

function scald_render($sid, $context, $options = NULL, $rebuild = FALSE) {

  // Argument validation
  if (!is_numeric($sid)) {
    if (!empty($sid->sid)) {
      $atom = $sid;
      $sid = $atom->sid;
    }
    else {
      return FALSE;
    }
  }

  // Load the Atom Object in order to use it for determining the current Actions
  //  bitstring and for recursive scald_render() calls.
  $atom_reg = scald_is_registered($sid, $rebuild);
  if (empty($atom)) {
    $atom = $atom_reg;
  }

  // SID doesn't correspond to a registered Atom; drop through to render the
  //  "Invalid ID" message.
  if (empty($atom_reg)) {
    return scald_scald_render($atom_reg, 'invalid-id');
  }

  // Drop through to no-access rendering if current user can't view.
  if (!scald_action_permitted($atom, 'view')) {
    return scald_scald_render($atom, 'no-access');
  }
  $scald_config = variable_get('scald_config', 0);

  // In the event of an invalid Context, initiate fallbacks.  Immediate return
  //  avoids caching a different rendering as the specified Context.
  if (empty($scald_config->contexts[$context])) {
    return scald_render($atom, _scald_context_fallback($type, $context), $options, $rebuild);
  }

  // Check the cache unless explicitly rebuilding the rendering of the Atom
  $cache_id = $sid . ':' . $context . ':' . scald_actions($atom) . (is_null($options) ? '' : ':' . $options);
  if (!$rebuild && !variable_get('scald_always_rebuild', FALSE)) {
    $cache = cache_get($cache_id, 'cache_scald');
    if (!empty($cache)) {
      return $cache->data;
    }
  }

  // Either a cache miss or an explicit rebuild.  Pull in the rest of the Atom.
  if (!scald_is_fetched($atom)) {
    $atom = scald_fetch($sid);
  }
  scald_prerender($atom, $context, $options);

  // Context Provider does the final rendering
  $rendered = module_invoke($scald_config->contexts[$context]['provider'], 'scald_render', $atom, $context, $options);

  // The Context exists but rendering is failing hard for some reason.  Output
  //  still needs to be ensured, however.  Also note that an empty string is
  //  valid output.  However, hook_scald_render() should return FALSE upon
  //  failure and module_invoke() will return NULL if the function does not
  //  exist.
  if ($rendered === FALSE || is_null($rendered)) {
    return scald_render($atom, _scald_context_fallback($atom->type, $context), $options, $rebuild);
  }

  // For so-called "parsable" Contexts, ensure a standard format for the
  //  enclosing comments.
  if ($scald_config->contexts[$context]['parseable']) {
    $rendered = '<!-- scald=' . $atom->sid . ':' . $context . (!empty($options) ? ' ' . $options : '') . ' -->' . $rendered . '<!-- END scald=' . $atom->sid . ' -->';
  }

  // Only cache the Atom if Contexts, etc. have not indicated that the rendering
  //  is not cacheable (e.g. it contains the current user's username).
  if (!$atom->rendered->nocache) {

    // Note that cached renderings of an Atom will stick around until
    //  scald_update_atom() is called on the Atom.  This is usually the
    //  responsibility of the Atom Provider as it has the best idea when the base
    //  entity is changing.
    cache_set($cache_id, $rendered, 'cache_scald', CACHE_PERMANENT);
  }
  return $rendered;
}