Skip to content
Snippets Groups Projects
Commit 5085b1d7 authored by Earl Miles's avatar Earl Miles
Browse files

Convert node_view to use context instead of the node view. user_view next.

parent 655c3e5c
No related branches found
No related tags found
No related merge requests found
......@@ -285,8 +285,15 @@ function delegator_load_task_handlers($task, $subtask_id = NULL) {
/**
* Load all task handlers for a given task and subtask and sort them.
*/
function delegator_load_sorted_handlers($task, $subtask_id = NULL) {
function delegator_load_sorted_handlers($task, $subtask_id = NULL, $enabled = FALSE) {
$handlers = delegator_load_task_handlers($task, $subtask_id);
if ($enabled) {
foreach ($handlers as $id => $handler) {
if (!empty($handler->disabled)) {
unset($handlers[$id]);
}
}
}
uasort($handlers, '_delegator_sort_task_handlers');
return $handlers;
}
......@@ -604,3 +611,9 @@ function dp_arg_load($value, $subtask, $argument) {
return _dp_arg_load($value, $subtask, $argument);
}
/**
* Get the render function for a handler.
*/
function delegator_get_renderer($handler) {
return ctools_plugin_load_function('delegator', 'task_handlers', $handler->handler, 'render');
}
\ No newline at end of file
......@@ -8,15 +8,25 @@
function delegator_node_view_delegator_tasks() {
return array(
'node_view' => array(
'title' => t('Node view'),
'description' => t('The node view task allows you to control what handler will handle the job of rendering a node view at the path <em>node/%node</em>. If no handler is set or matches the criteria, the default Drupal node renderer will be used.'),
'type' => 'node_view', // handler type -- misnamed
'admin title' => 'Node view', // translated by menu system
'admin description' => 'Overrides for the built in node view handler at <em>node/%node</em>.',
'admin path' => 'node/%node',
'hook menu' => 'delegator_node_view_menu',
'hook menu alter' => 'delegator_node_view_menu_alter',
'task type' => 'page',
// This is a 'page' task and will fall under the page admin UI
'task type' => 'page',
'title' => t('Node view'),
'description' => t('The node view task allows you to control what handler will handle the job of rendering a node view at the path <em>node/%node</em>. If no handler is set or matches the criteria, the default Drupal node renderer will be used.'),
'admin title' => 'Node view', // translated by menu system
'admin description' => 'Overrides for the built in node view handler at <em>node/%node</em>.',
'admin path' => 'node/%node',
// Menu hooks so that we can alter the node/%node menu entry to point to us.
'hook menu' => 'delegator_node_view_menu',
'hook menu alter' => 'delegator_node_view_menu_alter',
// This is task uses 'context' handlers and must implement these to give the
// handler data it needs.
'type' => 'context', // handler type -- misnamed
'get arguments' => 'delegator_node_view_get_arguments',
'get context placeholders' => 'delegator_node_view_get_contexts',
),
);
}
......@@ -46,12 +56,26 @@ function delegator_node_view_menu_alter(&$items, $task) {
function delegator_node_view($node) {
// Load my task plugin:
$task = delegator_get_task('node_view');
$handlers = delegator_load_sorted_handlers($task);
$handlers = delegator_load_sorted_handlers($task, '', TRUE);
// Load the node into a context
ctools_include('context');
$context = ctools_context_create('node', $node);
$context->identifier = t('Node being viewed');
$context->keyword = 'node';
// The ID used here was determined by ctools_context_get_id and is
// used in the contexts returned by the get context placeholders
// callback. It must match or this context will not be the same
// context the UI configured.
$contexts = array(
'argument_nid_0' => $context,
);
// Try each handler.
foreach ($handlers as $handler) {
if ($function = ctools_plugin_load_function('delegator', 'task_handlers', $handler->handler, 'render')) {
$output = $function($handler, $node);
if ($function = delegator_get_renderer($handler)) {
$output = $function($handler, $contexts);
if ($output) {
// Since we're not using node_show() we need to emulate what it used to do.
// Update the history table, stating that this user viewed this node.
......@@ -70,4 +94,29 @@ function delegator_node_view($node) {
// Fall back!
return node_page_view($node);
}
/**
* Callback to get arguments provided by this task handler.
*
* Since this is the node view and there is no UI on the arguments, we
* create dummy arguments that contain the needed data.
*/
function delegator_node_view_get_arguments($task, $subtask_id) {
return array(
array(
'keyword' => 'node',
'identifier' => t('Node being viewed'),
'id' => 0,
'name' => 'nid',
'settings' => array(),
),
);
}
/**
* Callback to get context placeholders provided by this handler.
*/
function delegator_node_view_get_contexts($task, $subtask_id) {
return ctools_context_get_placeholders_from_argument(delegator_node_view_get_arguments($task, $subtask_id));
}
\ No newline at end of file
......@@ -46,7 +46,7 @@ function delegator_page_delegator_tasks() {
// context only items
'type' => 'context',
'get arguments' => 'delegator_page_get_argument_callback',
'get arguments' => 'delegator_page_get_arguments',
'get context placeholders' => 'delegator_page_get_contexts',
),
);
......@@ -295,8 +295,8 @@ function delegator_page_execute($subtask_id) {
// Try each handler.
foreach ($handlers as $handler) {
if ($function = ctools_plugin_load_function('delegator', 'task_handlers', $handler->handler, 'render')) {
$output = $function($handler, $page, $contexts);
if ($function = delegator_get_renderer($handler)) {
$output = $function($handler, $contexts);
if ($output) {
// TRUE is a special return used to let us know that it handled the
// task but does not wish us to render anything, as it already did.
......@@ -318,44 +318,27 @@ function delegator_page_execute($subtask_id) {
/**
* Return a list of arguments used by this task.
*/
function delegator_page_get_argument_callback($task, $subtask_id) {
$page = delegator_page_load($subtask_id);
if ($page) {
return delegator_page_get_arguments($page);
}
}
function delegator_page_get_arguments($task, $subtask_id) {
$arguments = array();
/**
* Get a group of context placeholders for the arguments.
*/
function delegator_page_get_contexts($task, $subtask_id) {
$page = delegator_page_load($subtask_id);
if ($page && $arguments = delegator_page_get_arguments($page)) {
ctools_include('context');
return ctools_context_get_placeholders_from_argument($arguments);
}
}
/**
* Return a list of arguments used by this page.
*
* This provides a list of arguments suitable for using in the context
* system, which is slightly more data than we store in the database.
* This also filters out arguments that have no contexts.
*/
function delegator_page_get_arguments($page) {
if ($page->arguments) {
$arguments = array();
if (!empty($page->arguments)) {
foreach ($page->arguments as $keyword => $argument) {
if (isset($argument['name'])) {
$argument['keyword'] = $keyword;
$arguments[$keyword] = $argument;
}
}
return $arguments;
}
return $arguments;
}
/**
* Get a group of context placeholders for the arguments.
*/
function delegator_page_get_contexts($task, $subtask_id) {
ctools_include('context');
return ctools_context_get_placeholders_from_argument(delegator_page_get_arguments($task, $subtask_id));
}
// --------------------------------------------------------------------------
......
......@@ -18,19 +18,18 @@
/**
* Compare arguments to contexts for selection purposes.
*
* @param $task
* The task plugin definition.
* @param $subtask_id
* The id of the subtask being used.
* @param $handler
* The handler in question.
* @param $contexts
* The context objects provided by the task.
* @return
* TRUE if these contexts match the selection criteria. NULL or FALSE
* otherwise.
*/
function ctools_context_handler_select($task, $subtask_id, $contexts) {
function ctools_context_handler_select($handler, $contexts) {
$task = delegator_get_task($handler->task);
if ($function = ctools_plugin_get_function($task, 'get arguments')) {
$arguments = $function($task, $subtask_id);
$arguments = $function($task, $handler->subtask);
foreach ($arguments as $argument) {
$id = ctools_context_id($argument, 'argument');
if (empty($handler->conf[$id]) || empty($contexts[$id])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment