Projet

Général

Profil

Paste
Télécharger (6,34 ko) Statistiques
| Branche: | Révision:

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

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
  // Load my task plugin
82
  $task = page_manager_get_task('node_view');
83

    
84
  // Load the node into a context.
85
  ctools_include('context');
86
  ctools_include('context-task-handler');
87

    
88
  $uri = entity_uri('node', $node);
89
  if (isset($uri['path'])) {
90
    // Set the node path as the canonical URL to prevent duplicate content.
91
    $meta_canon = array(
92
      'rel' => 'canonical',
93
      'href' => url($uri['path'], $uri['options']),
94
    );
95
    drupal_add_html_head_link($meta_canon, TRUE);
96

    
97
    // Set the non-aliased path as a default shortlink.
98
    $meta_short = array(
99
      'rel' => 'shortlink',
100
      'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))
101
    );
102
    drupal_add_html_head_link($meta_short, TRUE);
103
  }
104

    
105
  // Load all contexts.
106
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
107

    
108
  // Build the full output using the configured CTools plugin.
109
  $output = ctools_context_handler_render($task, '', $contexts, array($node->nid));
110
  if ($output !== FALSE) {
111
    node_tag_new($node);
112
    return $output;
113
  }
114

    
115
  // Try loading an override plugin.
116
  foreach (module_implements('page_manager_override') as $module) {
117
    $call = $module . '_page_manager_override';
118
    if (($rc = $call('node_view')) && function_exists($rc)) {
119
      return $rc($node);
120
    }
121
  }
122

    
123
  // Prepare the node to be displayed so all of the regular hooks are triggered.
124
  $default_output = node_page_view($node);
125

    
126
  // Otherwise, fall back to the default output generated by node_page_view().
127
  return $default_output;
128
}
129

    
130
/**
131
 * Callback to get arguments provided by this task handler.
132
 *
133
 * Since this is the node view and there is no UI on the arguments, we
134
 * create dummy arguments that contain the needed data.
135
 */
136
function page_manager_node_view_get_arguments($task, $subtask_id) {
137
  return array(
138
    array(
139
      'keyword' => 'node',
140
      'identifier' => t('Node being viewed'),
141
      'id' => 1,
142
      'name' => 'entity_id:node',
143
      'settings' => array(),
144
    ),
145
  );
146
}
147

    
148
/**
149
 * Callback to get context placeholders provided by this handler.
150
 */
151
function page_manager_node_view_get_contexts($task, $subtask_id) {
152
  return ctools_context_get_placeholders_from_argument(page_manager_node_view_get_arguments($task, $subtask_id));
153
}
154

    
155
/**
156
 * Callback to enable/disable the page from the UI.
157
 */
158
function page_manager_node_view_enable($cache, $status) {
159
  variable_set('page_manager_node_view_disabled', $status);
160

    
161
  // Set a global flag so that the menu routine knows it needs
162
  // to set a message if enabling cannot be done.
163
  if (!$status) {
164
    $GLOBALS['page_manager_enabling_node_view'] = TRUE;
165
  }
166
}
167

    
168
/**
169
 * Callback to determine if a page is accessible.
170
 *
171
 * @param $task
172
 *   The task plugin.
173
 * @param $subtask_id
174
 *   The subtask id
175
 * @param $contexts
176
 *   The contexts loaded for the task.
177
 * @return
178
 *   TRUE if the current user can access the page.
179
 */
180
function page_manager_node_view_access_check($task, $subtask_id, $contexts) {
181
  $context = reset($contexts);
182
  return node_access('view', $context->data);
183
}