Projet

Général

Profil

Paste
Télécharger (7 ko) Statistiques
| Branche: | Révision:

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

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
  // Set the default title for the node add/edit form. If the page has a custom
99
  // title it'll override this.
100
  $types = node_type_get_types();
101
  $context = reset($contexts);
102
  if (empty($context->data->nid)) {
103
    drupal_set_title(t('Create @name', array(
104
      '@name' => $types[$context->data->type]->name
105
    )), PASS_THROUGH);
106
  }
107
  else {
108
    drupal_set_title(t('<em>Edit @type</em> @title', array(
109
      '@type' => $types[$context->node_type]->name,
110
      '@title' => $context->data->title
111
    )), PASS_THROUGH);
112
  }
113

    
114
  $arg = array(isset($node->nid) ? $node->nid : $node->type);
115
  $output = ctools_context_handler_render($task, '', $contexts, $arg);
116
  if ($output === FALSE) {
117
    // Fall back!
118
    // We've already built the form with the context, so we can't build it again, or
119
    // form_clean_id will mess up our ids. But we don't really need to, either:
120
    $output = $context->form;
121
  }
122

    
123
  return $output;
124
}
125

    
126
/**
127
 * Callback to handle the process of adding a node.
128
 *
129
 * This creates a basic $node and passes that off to page_manager_node_edit().
130
 * It is modelled after Drupal's node_add() function.
131
 *
132
 * Unlike node_add() we do not need to check node_access because that was
133
 * already checked by the menu system.
134
 */
135
function page_manager_node_add($type) {
136
  global $user;
137

    
138
  $types = node_type_get_types();
139

    
140
  // Initialize settings:
141
  $node = (object) array(
142
    'uid' => $user->uid,
143
    'name' => (isset($user->name) ? $user->name : ''),
144
    'type' => $type,
145
    'language' => LANGUAGE_NONE,
146
  );
147

    
148
  drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
149
  return page_manager_node_edit($node);
150
}
151

    
152
/**
153
 * Callback to get arguments provided by this task handler.
154
 *
155
 * Since this is the node edit and there is no UI on the arguments, we
156
 * create dummy arguments that contain the needed data.
157
 */
158
function page_manager_node_edit_get_arguments($task, $subtask_id) {
159
  return array(
160
    array(
161
      'keyword' => 'node',
162
      'identifier' => t('Node being edited'),
163
      'id' => 1,
164
      'name' => 'node_edit',
165
      'settings' => array(),
166
    ),
167
  );
168
}
169

    
170
/**
171
 * Callback to get context placeholders provided by this handler.
172
 */
173
function page_manager_node_edit_get_contexts($task, $subtask_id) {
174
  return ctools_context_get_placeholders_from_argument(page_manager_node_edit_get_arguments($task, $subtask_id));
175
}
176

    
177
/**
178
 * Callback to enable/disable the page from the UI.
179
 */
180
function page_manager_node_edit_enable($cache, $status) {
181
  variable_set('page_manager_node_edit_disabled', $status);
182
  // Set a global flag so that the menu routine knows it needs
183
  // to set a message if enabling cannot be done.
184
  if (!$status) {
185
    $GLOBALS['page_manager_enabling_node_edit'] = TRUE;
186
  }
187
}
188

    
189
/**
190
 * Callback to determine if a page is accessible.
191
 *
192
 * @param $task
193
 *   The task plugin.
194
 * @param $subtask_id
195
 *   The subtask id
196
 * @param $contexts
197
 *   The contexts loaded for the task.
198
 *
199
 * @return
200
 *   TRUE if the current user can access the page.
201
 */
202
function page_manager_node_edit_access_check($task, $subtask_id, $contexts) {
203
  $context = reset($contexts);
204
  return node_access('update', $context->data);
205
}