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 @ c2ac6d1d

1 85ad3d82 Assos Assos
<?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 c304a780 Assos Assos
    // This is a 'page' task and will fall under the page admin UI.
19 85ad3d82 Assos Assos
    '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 c304a780 Assos Assos
    // Automatically disable this task if it cannot be enabled.
64 85ad3d82 Assos Assos
    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 c304a780 Assos Assos
  // Load my task plugin.
82 85ad3d82 Assos Assos
  $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 7e72b748 Assos Assos
  $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 c304a780 Assos Assos
      'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE))),
101 7e72b748 Assos Assos
    );
102
    drupal_add_html_head_link($meta_short, TRUE);
103
  }
104
105 e4c061ad Assos Assos
  // Load all contexts.
106 85ad3d82 Assos Assos
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
107
108 e4c061ad Assos Assos
  // Build the full output using the configured CTools plugin.
109 85ad3d82 Assos Assos
  $output = ctools_context_handler_render($task, '', $contexts, array($node->nid));
110 e4c061ad Assos Assos
  if ($output !== FALSE) {
111 85ad3d82 Assos Assos
    node_tag_new($node);
112
    return $output;
113
  }
114
115 e4c061ad Assos Assos
  // Try loading an override plugin.
116 85ad3d82 Assos Assos
  foreach (module_implements('page_manager_override') as $module) {
117
    $call = $module . '_page_manager_override';
118
    if (($rc = $call('node_view')) && function_exists($rc)) {
119 e4c061ad Assos Assos
      return $rc($node);
120 85ad3d82 Assos Assos
    }
121
  }
122
123 560c3060 Julien Enselme
  // Prepare the node to be displayed so all of the regular hooks are triggered.
124
  $default_output = node_page_view($node);
125
126 e4c061ad Assos Assos
  // Otherwise, fall back to the default output generated by node_page_view().
127
  return $default_output;
128 85ad3d82 Assos Assos
}
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 c304a780 Assos Assos
 *
178 85ad3d82 Assos Assos
 * @return
179
 *   TRUE if the current user can access the page.
180
 */
181
function page_manager_node_view_access_check($task, $subtask_id, $contexts) {
182
  $context = reset($contexts);
183
  return node_access('view', $context->data);
184
}