Projet

Général

Profil

Paste
Télécharger (5,84 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / page_manager / plugins / tasks / node_view.inc @ e4c061ad

1
<?php
2

    
3
/**
4
 * @file
5
 * Handle the 'node view' override task.
6
 *
7
 * This plugin overrides node/%node and reroutes it to the page manager, where
8
 * a list of tasks can be used to service this request based upon criteria
9
 * supplied by access plugins.
10
 */
11

    
12
/**
13
 * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
14
 * more information.
15
 */
16
function page_manager_node_view_page_manager_tasks() {
17
  return array(
18
    // This is a 'page' task and will fall under the page admin UI
19
    'task type' => 'page',
20

    
21
    'title' => t('Node template'),
22

    
23
    'admin title' => t('Node template'),
24
    'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying nodes at <em>node/%node</em>. If you add variants, you may use selection criteria such as node type or language or user access to provide different views of nodes. If no variant is selected, the default Drupal node view will be used. This page only affects nodes viewed as pages, it will not affect nodes viewed in lists or at other locations. Also please note that if you are using pathauto, aliases may make a node to be somewhere else, but as far as Drupal is concerned, they are still at node/%node.'),
25
    'admin path' => 'node/%node',
26

    
27
    // Menu hooks so that we can alter the node/%node menu entry to point to us.
28
    'hook menu' => 'page_manager_node_view_menu',
29
    'hook menu alter' => 'page_manager_node_view_menu_alter',
30

    
31
    // This is task uses 'context' handlers and must implement these to give the
32
    // handler data it needs.
33
    'handler type' => 'context',
34
    'get arguments' => 'page_manager_node_view_get_arguments',
35
    'get context placeholders' => 'page_manager_node_view_get_contexts',
36

    
37
    // Allow this to be enabled or disabled:
38
    'disabled' => variable_get('page_manager_node_view_disabled', TRUE),
39
    'enable callback' => 'page_manager_node_view_enable',
40
    'access callback' => 'page_manager_node_view_access_check',
41
  );
42
}
43

    
44
/**
45
 * Callback defined by page_manager_node_view_page_manager_tasks().
46
 *
47
 * Alter the node view input so that node view comes to us rather than the
48
 * normal node view process.
49
 */
50
function page_manager_node_view_menu_alter(&$items, $task) {
51
  if (variable_get('page_manager_node_view_disabled', TRUE)) {
52
    return;
53
  }
54

    
55
  // Override the node view handler for our purpose.
56
  $callback = $items['node/%node']['page callback'];
57
  if ($callback == 'node_page_view' || variable_get('page_manager_override_anyway', FALSE)) {
58
    $items['node/%node']['page callback'] = 'page_manager_node_view_page';
59
    $items['node/%node']['file path'] = $task['path'];
60
    $items['node/%node']['file'] = $task['file'];
61
  }
62
  else {
63
    // automatically disable this task if it cannot be enabled.
64
    variable_set('page_manager_node_view_disabled', TRUE);
65
    if (!empty($GLOBALS['page_manager_enabling_node_view'])) {
66
      drupal_set_message(t('Page manager module is unable to enable node/%node because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error');
67
    }
68
  }
69

    
70
  // @todo override node revision handler as well?
71
}
72

    
73
/**
74
 * Entry point for our overridden node view.
75
 *
76
 * This function asks its assigned handlers who, if anyone, would like
77
 * to run with it. If no one does, it passes through to Drupal core's
78
 * node view, which is node_page_view().
79
 */
80
function page_manager_node_view_page($node) {
81
  // Prep the node to be displayed so all of the regular hooks are triggered.
82
  // Also save the output for later, in case it is needed.
83
  $default_output = node_page_view($node);
84

    
85
  // Load my task plugin
86
  $task = page_manager_get_task('node_view');
87

    
88
  // Load the node into a context.
89
  ctools_include('context');
90
  ctools_include('context-task-handler');
91

    
92
  // Load all contexts.
93
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
94

    
95
  // Build the full output using the configured CTools plugin.
96
  $output = ctools_context_handler_render($task, '', $contexts, array($node->nid));
97
  if ($output !== FALSE) {
98
    node_tag_new($node);
99
    return $output;
100
  }
101

    
102
  // Try loading an override plugin.
103
  foreach (module_implements('page_manager_override') as $module) {
104
    $call = $module . '_page_manager_override';
105
    if (($rc = $call('node_view')) && function_exists($rc)) {
106
      return $rc($node);
107
    }
108
  }
109

    
110
  // Otherwise, fall back to the default output generated by node_page_view().
111
  return $default_output;
112
}
113

    
114
/**
115
 * Callback to get arguments provided by this task handler.
116
 *
117
 * Since this is the node view and there is no UI on the arguments, we
118
 * create dummy arguments that contain the needed data.
119
 */
120
function page_manager_node_view_get_arguments($task, $subtask_id) {
121
  return array(
122
    array(
123
      'keyword' => 'node',
124
      'identifier' => t('Node being viewed'),
125
      'id' => 1,
126
      'name' => 'entity_id:node',
127
      'settings' => array(),
128
    ),
129
  );
130
}
131

    
132
/**
133
 * Callback to get context placeholders provided by this handler.
134
 */
135
function page_manager_node_view_get_contexts($task, $subtask_id) {
136
  return ctools_context_get_placeholders_from_argument(page_manager_node_view_get_arguments($task, $subtask_id));
137
}
138

    
139
/**
140
 * Callback to enable/disable the page from the UI.
141
 */
142
function page_manager_node_view_enable($cache, $status) {
143
  variable_set('page_manager_node_view_disabled', $status);
144

    
145
  // Set a global flag so that the menu routine knows it needs
146
  // to set a message if enabling cannot be done.
147
  if (!$status) {
148
    $GLOBALS['page_manager_enabling_node_view'] = TRUE;
149
  }
150
}
151

    
152
/**
153
 * Callback to determine if a page is accessible.
154
 *
155
 * @param $task
156
 *   The task plugin.
157
 * @param $subtask_id
158
 *   The subtask id
159
 * @param $contexts
160
 *   The contexts loaded for the task.
161
 * @return
162
 *   TRUE if the current user can access the page.
163
 */
164
function page_manager_node_view_access_check($task, $subtask_id, $contexts) {
165
  $context = reset($contexts);
166
  return node_access('view', $context->data);
167
}