Project

General

Profile

Paste
Download (6.5 KB) Statistics
| Branch: | Revision:

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

1
<?php
2

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

    
12
    'title' => t('Node add/edit form'),
13
    'admin title' => t('Node add/edit form'),
14
    '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.'),
15
    'admin path' => 'node/%node/edit',
16

    
17
    // Menu hooks so that we can alter the node/%node menu entry to point to us.
18
    'hook menu' => 'page_manager_node_edit_menu',
19
    'hook menu alter' => 'page_manager_node_edit_menu_alter',
20

    
21
    // This is task uses 'context' handlers and must implement these to give the
22
    // handler data it needs.
23
    'handler type' => 'context',
24
    'get arguments' => 'page_manager_node_edit_get_arguments',
25
    'get context placeholders' => 'page_manager_node_edit_get_contexts',
26

    
27
    // Allow this to be enabled or disabled:
28
    'disabled' => variable_get('page_manager_node_edit_disabled', TRUE),
29
    'enable callback' => 'page_manager_node_edit_enable',
30
    'access callback' => 'page_manager_node_edit_access_check',
31
  );
32
}
33

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

    
45
  $callback = $items['node/%node/edit']['page callback'];
46
  // Override the node edit handler for our purpose.
47
  if ($callback == 'node_page_edit' || variable_get('page_manager_override_anyway', FALSE)) {
48
    $items['node/%node/edit']['page callback'] = 'page_manager_node_edit';
49
    $items['node/%node/edit']['file path'] = $task['path'];
50
    $items['node/%node/edit']['file'] = $task['file'];
51
  }
52
  else {
53
    variable_set('page_manager_node_edit_disabled', TRUE);
54
    if (!empty($GLOBALS['page_manager_enabling_node_edit'])) {
55
      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');
56
    }
57
    return;
58
  }
59

    
60
  // Also catch node/add handling:
61
  foreach (node_type_get_types() as $type) {
62
    $path = 'node/add/' . str_replace('_', '-', $type->type);
63
    if ($items[$path]['page callback'] != 'node_add') {
64
      if (!empty($GLOBALS['page_manager_enabling_node_edit'])) {
65
        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');
66
      }
67
      continue;
68
    }
69

    
70
    $items[$path]['page callback'] = 'page_manager_node_add';
71
    $items[$path]['file path'] = $task['path'];
72
    $items[$path]['file'] = $task['file'];
73
    // Why str_replace things back?
74
    $items[$path]['page arguments'] = array($type->type);
75
  }
76
}
77

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

    
89
  // Load the node into a context.
90
  ctools_include('context');
91
  ctools_include('context-task-handler');
92
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
93

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

    
104
  return $output;
105
}
106

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

    
119
  $types = node_type_get_types();
120

    
121
  // Initialize settings:
122
  $node = (object) array(
123
    'uid' => $user->uid,
124
    'name' => (isset($user->name) ? $user->name : ''),
125
    'type' => $type,
126
    'language' => LANGUAGE_NONE,
127
  );
128

    
129
  drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
130
  return page_manager_node_edit($node);
131
}
132

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

    
151
/**
152
 * Callback to get context placeholders provided by this handler.
153
 */
154
function page_manager_node_edit_get_contexts($task, $subtask_id) {
155
  return ctools_context_get_placeholders_from_argument(page_manager_node_edit_get_arguments($task, $subtask_id));
156
}
157

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

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