I am coding a custom task_handler for a node_view task.

My render callback looks currently like the code below (the most of my code was copied from http_response task_handler).

Now I want to access the node object in the $contexts: array(..., [argument_entity_id:node_1] => ctools_context Object, ...); I guess I should not write $node = $contexts['argument_entity_id:node_1']->data, should I? Because the array key looks very generic. I think there should be a function to retrieve the node context. May somebody help me to find a good way to get the node object out of the contexts array?


  // Go through arguments and see if they match.
  ctools_include('context');
  ctools_include('context-task-handler');

  // Add my contexts
  $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler);
  
  // Test.
  if ($test && !ctools_context_handler_select($handler, $contexts)) {
    return;
  }
  
  if (isset($handler->handler)) {
    ctools_context_handler_pre_render($handler, $contexts, $args);
  }

  // Get node object from context.
  // This works but does not look good, because argument_entity_id:node_1 looks very generic.
  $node = $contexts['argument_entity_id:node_1']->data;

Comments

merlinofchaos’s picture

Status: Active » Fixed

I *just* wrote code similar to this, and it's in the very latest Panelizer 2.x -- go see the panelizer_node task there. I think it should answer all your questions by virtue of being actual working code, but if you have further questions please activate this issue again and I will try to elucidate if you need it.

osopolar’s picture

Thank you merlinofchaos for the quick and helpful response.

To resume: Panelizer uses a function which get the correct context from the configuration if set, otherwise it takes just the first context available:

/**
 * Figure out the correct context to use for this panelizer.
 */
function _panelizer_panelizer_task_get_context($handler, $contexts) {
  if (isset($handler->conf['context']) && !empty($contexts[$handler->conf['context']])) {
    return $contexts[$handler->conf['context']];
  }

  // If one was not set up, we could be using old, saved data. Assume that
  // this means we just want the first context available.
  if (!empty($contexts)) {
    return reset($contexts);
  }

  // Fail!
  return stdClass();
}

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.