Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / page_manager / plugins / tasks / node_edit.inc @ c304a780

1
<?php
2

    
3
/**
4
 * @file
5
 */
6

    
7
/**
8
 * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
9
 * more information.
10
 */
11
function page_manager_node_edit_page_manager_tasks() {
12
  return array(
13
    // This is a 'page' task and will fall under the page admin UI.
14
    'task type' => 'page',
15

    
16
    'title' => t('Node add/edit form'),
17
    'admin title' => t('Node add/edit form'),
18
    'admin description' => t('When enabled, this overrides the default Drupal behavior for adding or edit nodes at <em>node/%node/edit</em> and <em>node/add/%node_type</em>. If you add variants, you may use selection criteria such as node type or language or user access to provide different edit forms for nodes. If no variant is selected, the default Drupal node edit will be used.'),
19
    'admin path' => 'node/%node/edit',
20

    
21
    // Menu hooks so that we can alter the node/%node menu entry to point to us.
22
    'hook menu' => 'page_manager_node_edit_menu',
23
    'hook menu alter' => 'page_manager_node_edit_menu_alter',
24

    
25
    // This is task uses 'context' handlers and must implement these to give the
26
    // handler data it needs.
27
    'handler type' => 'context',
28
    'get arguments' => 'page_manager_node_edit_get_arguments',
29
    'get context placeholders' => 'page_manager_node_edit_get_contexts',
30

    
31
    // Allow this to be enabled or disabled:
32
    'disabled' => variable_get('page_manager_node_edit_disabled', TRUE),
33
    'enable callback' => 'page_manager_node_edit_enable',
34
    'access callback' => 'page_manager_node_edit_access_check',
35
  );
36
}
37

    
38
/**
39
 * Callback defined by page_manager_node_edit_page_manager_tasks().
40
 *
41
 * Alter the node edit input so that node edit comes to us rather than the
42
 * normal node edit process.
43
 */
44
function page_manager_node_edit_menu_alter(&$items, $task) {
45
  if (variable_get('page_manager_node_edit_disabled', TRUE)) {
46
    return;
47
  }
48

    
49
  $callback = $items['node/%node/edit']['page callback'];
50
  // Override the node edit handler for our purpose.
51
  if ($callback == 'node_page_edit' || variable_get('page_manager_override_anyway', FALSE)) {
52
    $items['node/%node/edit']['page callback'] = 'page_manager_node_edit';
53
    $items['node/%node/edit']['file path'] = $task['path'];
54
    $items['node/%node/edit']['file'] = $task['file'];
55
  }
56
  else {
57
    variable_set('page_manager_node_edit_disabled', TRUE);
58
    if (!empty($GLOBALS['page_manager_enabling_node_edit'])) {
59
      drupal_set_message(t('Page manager module is unable to enable node/%node/edit because some other module already has overridden with %callback.', array('%callback' => $callback)), 'warning');
60
    }
61
    return;
62
  }
63

    
64
  // Also catch node/add handling:
65
  foreach (node_type_get_types() as $type) {
66
    $path = 'node/add/' . str_replace('_', '-', $type->type);
67
    if ($items[$path]['page callback'] != 'node_add') {
68
      if (!empty($GLOBALS['page_manager_enabling_node_edit'])) {
69
        drupal_set_message(t('Page manager module is unable to override @path because some other module already has overridden with %callback. Node edit will be enabled but that edit path will not be overridden.', array('@path' => $path, '%callback' => $items[$path]['page callback'])), 'warning');
70
      }
71
      continue;
72
    }
73

    
74
    $items[$path]['page callback'] = 'page_manager_node_add';
75
    $items[$path]['file path'] = $task['path'];
76
    $items[$path]['file'] = $task['file'];
77
    // Why str_replace things back?
78
    $items[$path]['page arguments'] = array($type->type);
79
  }
80
}
81

    
82
/**
83
 * Entry point for our overridden node edit.
84
 *
85
 * This function asks its assigned handlers who, if anyone, would like
86
 * to run with it. If no one does, it passes through to Drupal core's
87
 * node edit, which is node_page_edit().
88
 */
89
function page_manager_node_edit($node) {
90
  // Load my task plugin.
91
  $task = page_manager_get_task('node_edit');
92

    
93
  // Load the node into a context.
94
  ctools_include('context');
95
  ctools_include('context-task-handler');
96
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
97

    
98
  $arg = array(isset($node->nid) ? $node->nid : $node->type);
99
  $output = ctools_context_handler_render($task, '', $contexts, $arg);
100
  if ($output === FALSE) {
101
    // Fall back!
102
    // We've already built the form with the context, so we can't build it again, or
103
    // form_clean_id will mess up our ids. But we don't really need to, either:
104
    $context = reset($contexts);
105
    $output = $context->form;
106
  }
107

    
108
  return $output;
109
}
110

    
111
/**
112
 * Callback to handle the process of adding a node.
113
 *
114
 * This creates a basic $node and passes that off to page_manager_node_edit().
115
 * It is modelled after Drupal's node_add() function.
116
 *
117
 * Unlike node_add() we do not need to check node_access because that was
118
 * already checked by the menu system.
119
 */
120
function page_manager_node_add($type) {
121
  global $user;
122

    
123
  $types = node_type_get_types();
124

    
125
  // Initialize settings:
126
  $node = (object) array(
127
    'uid' => $user->uid,
128
    'name' => (isset($user->name) ? $user->name : ''),
129
    'type' => $type,
130
    'language' => LANGUAGE_NONE,
131
  );
132

    
133
  drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
134
  return page_manager_node_edit($node);
135
}
136

    
137
/**
138
 * Callback to get arguments provided by this task handler.
139
 *
140
 * Since this is the node edit and there is no UI on the arguments, we
141
 * create dummy arguments that contain the needed data.
142
 */
143
function page_manager_node_edit_get_arguments($task, $subtask_id) {
144
  return array(
145
    array(
146
      'keyword' => 'node',
147
      'identifier' => t('Node being edited'),
148
      'id' => 1,
149
      'name' => 'node_edit',
150
      'settings' => array(),
151
    ),
152
  );
153
}
154

    
155
/**
156
 * Callback to get context placeholders provided by this handler.
157
 */
158
function page_manager_node_edit_get_contexts($task, $subtask_id) {
159
  return ctools_context_get_placeholders_from_argument(page_manager_node_edit_get_arguments($task, $subtask_id));
160
}
161

    
162
/**
163
 * Callback to enable/disable the page from the UI.
164
 */
165
function page_manager_node_edit_enable($cache, $status) {
166
  variable_set('page_manager_node_edit_disabled', $status);
167
  // Set a global flag so that the menu routine knows it needs
168
  // to set a message if enabling cannot be done.
169
  if (!$status) {
170
    $GLOBALS['page_manager_enabling_node_edit'] = TRUE;
171
  }
172
}
173

    
174
/**
175
 * Callback to determine if a page is accessible.
176
 *
177
 * @param $task
178
 *   The task plugin.
179
 * @param $subtask_id
180
 *   The subtask id
181
 * @param $contexts
182
 *   The contexts loaded for the task.
183
 *
184
 * @return
185
 *   TRUE if the current user can access the page.
186
 */
187
function page_manager_node_edit_access_check($task, $subtask_id, $contexts) {
188
  $context = reset($contexts);
189
  return node_access('update', $context->data);
190
}