You are here

function esi_handle_component in ESI: Edge Side Includes 7.3

Menu callback to handle an ESI component.

See also

esi_component_load().

1 string reference to 'esi_handle_component'
esi_menu in ./esi.module
Implements hook_menu().

File

./esi.pages.inc, line 12
Delivery handlers for the ESI module.

Code

function esi_handle_component($component) {

  // Prevent ESI page from being cached.
  drupal_page_is_cacheable(FALSE);

  // The menu wildcard loader will return NULL for invalid components, so that
  // the menu-handler will delegate 404 delivery here.
  if (empty($component)) {
    esi_fast_404();
  }

  //store original context so we can replace it later
  $snippet_context = array();
  $snippet_context['REQUEST_URI'] = $_SERVER['REQUEST_URI'];
  $snippet_context['q'] = $_GET['q'];
  esi_snippet_context($snippet_context);

  // Remove the component from the arguments.
  $args = array_slice(func_get_args(), 1);

  // Load in the include file if provided.
  if (!empty($component['file'])) {
    $filepath = $component['filepath'] . '/' . $component['file'];
    if (file_exists($filepath)) {
      include_once $filepath;
    }
  }

  // Allow modules to preproccess the request, set up context, etc.
  // Any arguments returned by the preprocess handler are passed to the render
  // handler.
  if (isset($component['preprocess'])) {
    $result = call_user_func_array($component['preprocess'], $args);
    $args = is_array($result) ? $result : array(
      $result,
    );
  }

  // Get the renderable content of the component.
  $content = call_user_func_array($component['render'], $args);

  // Ensure the content is a renderable array (even if a string was returned).
  $content_block = is_array($content) ? $content : array(
    '#markup' => $content,
  );
  return $content_block;
}