| Commit | Line | Data |
|---|---|---|
| dede8450 | 1 | <?php |
| dede8450 EM |
2 | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * Page wizard that can create a variant on the node_view to take over a node | |
| 6 | * for a particular type. | |
| 7 | * | |
| 8 | * This wizard does a lot that's cut and pasted from exports. We can get away | |
| 9 | * with this because we know exports tend to remain relatively backward | |
| 10 | * compatible, and because we know that our context IDs are locked in the | |
| 11 | * node_view page. | |
| 12 | */ | |
| 13 | $plugin = array( | |
| 14 | 'title' => t('Node template'), | |
| 15 | 'page title' => t('Node template wizard'), | |
| dede8450 EM |
16 | 'description' => t('The node page wizard can help you override the node page for a type of node.'), |
| 17 | ||
| 18 | 'type' => 'panels', | |
| 19 | ||
| 20 | 'form info' => array( | |
| 21 | 'order' => array( | |
| 22 | 'type' => t('Select node type'), | |
| 23 | 'content' => t('Content'), | |
| 24 | ), | |
| 25 | ||
| 26 | 'forms' => array( | |
| 27 | 'type' => array( | |
| 28 | 'form id' => 'panels_node_override_basic', | |
| 29 | ), | |
| 30 | 'content' => array( | |
| 31 | 'form id' => 'panels_node_override_content', | |
| 32 | ), | |
| 33 | ), | |
| 34 | ), | |
| 35 | ||
| 36 | 'default cache' => 'panels_node_override_new_page', | |
| 37 | ||
| 38 | 'start' => 'panels_node_override_start', | |
| 39 | 'finish' => 'panels_node_override_finish', | |
| 40 | ); | |
| 41 | ||
| 42 | /** | |
| 43 | * Provide defaults for a new cache. | |
| 44 | * | |
| 45 | * The cache will store all our temporary data; it isn't really a page | |
| 46 | * in itself, but it does contain everything we need to make one at the end. | |
| 47 | */ | |
| 48 | function panels_node_override_new_page(&$cache) { | |
| 49 | $cache->type = ''; | |
| 50 | $cache->display = panels_new_display(); | |
| 51 | $cache->display->layout = 'flexible'; | |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * Callback called prior to the wizard starting up on every page | |
| 56 | * load. | |
| 57 | */ | |
| 58 | function panels_node_override_start($form_info, $step, &$form_state) { | |
| 59 | $form_state['page'] = page_manager_get_page_cache('node_view'); | |
| 60 | if (!empty($form_state['page']->locked)) { | |
| 61 | $account = user_load($form_state['page']->locked->uid); | |
| 62 | $name = theme('username', $account); | |
| 63 | $lock_age = format_interval(time() - $form_state['page']->locked->updated); | |
| 64 | $break = url(page_manager_edit_url($form_state['page']->task_name, array('actions', 'break-lock'))); | |
| 65 | ||
| 66 | drupal_set_message(t('WARNING! The node_view is being edited by user !user, and is therefore locked from editing by others. This wizard cannot create a new node override while this page is locked. This lock is !age old. Click here to <a href="!break">break this lock</a>.', array('!user' => $name, '!age' => $lock_age, '!break' => $break)), 'warning'); | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * First page of our page creator wizard. | |
| 72 | */ | |
| 73 | function panels_node_override_basic(&$form, &$form_state) { | |
| 74 | $types = node_get_types(); | |
| 75 | $form_state['types'] = $types; | |
| 76 | ||
| 77 | $already_done = array(); | |
| 78 | // Figure out which types already have variants assigned to them. | |
| 79 | foreach ($form_state['page']->handlers as $name => $handler) { | |
| 80 | if ($handler->handler == 'panel_context' && !empty($handler->conf['access']['plugins'])) { | |
| 81 | foreach ($handler->conf['access']['plugins'] as $plugin) { | |
| 82 | if ($plugin['name'] == 'node_type') { | |
| 83 | foreach ($plugin['settings']['type'] as $type) { | |
| 84 | $already_done[$type] = $name; | |
| 85 | } | |
| 86 | } | |
| 87 | } | |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | if ($already_done) { | |
| 92 | $items = array(); | |
| 93 | foreach ($already_done as $type => $handler_id) { | |
| 94 | $items[] = check_plain($types[$type]->name) . ' ' . l(t('[Edit]'), page_manager_edit_url($form_state['page']->task_name, array('handlers', $handler_id, 'content'))); | |
| 95 | } | |
| 96 | ||
| 97 | $form['already_done'] = array( | |
| 98 | '#type' => 'item', | |
| 99 | '#title' => t('Existing node templates'), | |
| 100 | '#value' => theme('item_list', $items), | |
| 101 | ); | |
| 102 | } | |
| 103 | ||
| 104 | $options = array(); | |
| 105 | foreach ($types as $name => $type) { | |
| 106 | if (empty($already_done[$name])) { | |
| 107 | $options[$name] = $type->name; | |
| 108 | } | |
| 109 | } | |
| 110 | ||
| 111 | $form['type'] = array( | |
| 112 | '#type' => 'select', | |
| 113 | '#title' => t('Node type'), | |
| 114 | '#options' => $options, | |
| 115 | '#default_value' => $form_state['cache']->type, | |
| 116 | ); | |
| 117 | ||
| 118 | ctools_include('page-wizard', 'panels'); | |
| 119 | panels_page_wizard_add_layout($form, $form_state); | |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Submit function to store the form data in our cache. | |
| 124 | */ | |
| 125 | function panels_node_override_basic_submit(&$form, &$form_state) { | |
| 126 | $cache = &$form_state['cache']; | |
| 127 | $cache->display->layout = $form_state['values']['layout']; | |
| 128 | $cache->type = $form_state['values']['type']; | |
| 129 | ||
| 130 | // Create a new handler object and cache it; this way we can use the | |
| 131 | // handler object for retrieving contexts properly. | |
| 132 | // Create the the panel context variant configured with our display | |
| 133 | $plugin = page_manager_get_task_handler('panel_context'); | |
| 134 | ||
| 135 | // Create a new handler. | |
| 136 | $cache->handler = page_manager_new_task_handler($plugin); | |
| 137 | $cache->handler->conf['title'] = $form_state['types'][$cache->type]->name; | |
| 138 | $cache->handler->conf['pipeline'] = 'ipe'; | |
| 139 | $cache->handler->conf['access'] = array( | |
| 140 | 'plugins' => array( | |
| 141 | 0 => array( | |
| 142 | 'name' => 'node_type', | |
| 143 | 'settings' => array( | |
| 144 | 'type' => array( | |
| 145 | $cache->type => $cache->type, | |
| 146 | ), | |
| 147 | ), | |
| 148 | 'context' => 'argument_nid_1', | |
| 149 | 'not' => FALSE, | |
| 150 | ), | |
| 151 | ), | |
| 152 | 'logic' => 'and', | |
| 153 | ); | |
| 154 | ||
| 155 | // Find a region by trying some basic main content region IDs. | |
| 156 | $layout = panels_get_layout($form_state['values']['layout']); | |
| 157 | $regions = panels_get_regions($layout, $cache->display); | |
| 158 | foreach (array('center', 'middle', 'content', 'main') as $candidate) { | |
| 159 | if (!empty($regions[$candidate])) { | |
| 160 | $region = $candidate; | |
| 161 | break; | |
| 162 | } | |
| 163 | } | |
| 164 | ||
| 165 | // If all of the above failed, use the first region. | |
| 166 | if (empty($region)) { | |
| 167 | $keys = array_keys($regions); | |
| 168 | $region = reset($keys); | |
| 169 | } | |
| 170 | ||
| 171 | // Populate the layout with content. This is from an export, with minor | |
| 172 | // changes to ensure defaults are correct and to add stuff to the proper region. | |
| 173 | $pane = new stdClass; | |
| 174 | $pane->pid = 'new-1'; | |
| 175 | $pane->panel = $region; | |
| 176 | $pane->type = 'node_content'; | |
| 177 | $pane->subtype = 'node_content'; | |
| 178 | $pane->shown = TRUE; | |
| 179 | $pane->access = array(); | |
| 180 | $pane->configuration = array( | |
| 181 | 'links' => 1, | |
| 182 | 'page' => 1, | |
| 183 | 'no_extras' => 0, | |
| 184 | 'override_title' => 0, | |
| 185 | 'override_title_text' => '', | |
| 186 | 'identifier' => '', | |
| 187 | 'link' => 0, | |
| 188 | 'leave_node_title' => 0, | |
| 189 | 'context' => 'argument_nid_1', | |
| 190 | 'build_mode' => 'full', | |
| 191 | ); | |
| 192 | $pane->cache = array(); | |
| 193 | $pane->style = array( | |
| 194 | 'settings' => NULL, | |
| 195 | ); | |
| 196 | $pane->css = array(); | |
| 197 | $pane->extras = array(); | |
| 198 | $pane->position = 0; | |
| 199 | $cache->display->content['new-1'] = $pane; | |
| 200 | $cache->display->panels[$region][0] = 'new-1'; | |
| 201 | $pane = new stdClass; | |
| 202 | $pane->pid = 'new-2'; | |
| 203 | $pane->panel = $region; | |
| 204 | $pane->type = 'node_comments'; | |
| 205 | $pane->subtype = 'node_comments'; | |
| 206 | $pane->shown = TRUE; | |
| 207 | $pane->access = array(); | |
| 208 | $pane->configuration = array( | |
| 209 | 'mode' => variable_get('comment_default_mode', COMMENT_MODE_THREADED_EXPANDED), | |
| 210 | 'order' => variable_get('comment_default_order', COMMENT_ORDER_NEWEST_FIRST), | |
| 211 | 'comments_per_page' => variable_get('comment_default_per_page', '50'), | |
| 212 | 'context' => 'argument_nid_1', | |
| 213 | 'override_title' => 0, | |
| 214 | 'override_title_text' => '', | |
| 215 | ); | |
| 216 | $pane->cache = array(); | |
| 217 | $pane->style = array( | |
| 218 | 'settings' => NULL, | |
| 219 | ); | |
| 220 | $pane->css = array(); | |
| 221 | $pane->extras = array(); | |
| 222 | $pane->position = 1; | |
| 223 | $cache->display->content['new-2'] = $pane; | |
| 224 | $cache->display->panels[$region][1] = 'new-2'; | |
| 225 | $pane = new stdClass; | |
| 226 | $pane->pid = 'new-3'; | |
| 227 | $pane->panel = $region; | |
| 228 | $pane->type = 'node_comment_form'; | |
| 229 | $pane->subtype = 'node_comment_form'; | |
| 230 | $pane->shown = TRUE; | |
| 231 | $pane->access = array(); | |
| 232 | $pane->configuration = array( | |
| 233 | 'anon_links' => 1, | |
| 234 | 'context' => 'argument_nid_1', | |
| 235 | 'override_title' => 0, | |
| 236 | 'override_title_text' => '', | |
| 237 | ); | |
| 238 | $pane->cache = array(); | |
| 239 | $pane->style = array( | |
| 240 | 'settings' => NULL, | |
| 241 | ); | |
| 242 | $pane->css = array(); | |
| 243 | $pane->extras = array(); | |
| 244 | $pane->position = 2; | |
| 245 | $cache->display->content['new-3'] = $pane; | |
| 246 | $cache->display->panels[$region][2] = 'new-3'; | |
| 247 | ||
| 248 | $task = page_manager_get_task('node_view'); | |
| 249 | ctools_include('context'); | |
| 250 | ctools_include('context-task-handler'); | |
| 251 | $cache->context = ctools_context_handler_get_all_contexts($task, NULL, $cache->handler); | |
| 252 | ||
| 253 | } | |
| 254 | ||
| 255 | /** | |
| 256 | * Second page of our wizard. This one provides a layout and lets the | |
| 257 | * user add content. | |
| 258 | */ | |
| 259 | function panels_node_override_content(&$form, &$form_state) { | |
| 260 | ctools_include('page-wizard', 'panels'); | |
| 261 | panels_page_wizard_add_content($form, $form_state); | |
| 262 | } | |
| 263 | ||
| 264 | /** | |
| 265 | * Store changes to the display. | |
| 266 | */ | |
| 267 | function panels_node_override_content_submit(&$form, &$form_state) { | |
| 268 | panels_page_wizard_add_content_submit($form, $form_state); | |
| 269 | } | |
| 270 | ||
| 271 | /** | |
| 272 | * Complete the wizard, create a new variant, and send them to the | |
| 273 | * edit screen of that variant. | |
| 274 | */ | |
| 275 | function panels_node_override_finish(&$form_state) { | |
| 276 | $page = &$form_state['page']; | |
| 277 | $cache = &$form_state['cache']; | |
| 278 | ||
| 279 | // Add the new handler to the page | |
| 280 | $cache->handler->conf['display'] = $cache->display; | |
| 281 | page_manager_handler_add_to_page($page, $cache->handler); | |
| 282 | ||
| 283 | // Save it | |
| 284 | page_manager_save_page_cache($page); | |
| 285 | ||
| 286 | // Send us to the page manager edit form for this. | |
| 287 | $form_state['redirect'] = url(page_manager_edit_url('node_view', array('handlers', $cache->handler->name, 'content'))); | |
| 288 | drupal_set_message(t('Your node template has been created.')); | |
| 289 | } |