5 * @file panels_node.module
7 * This module provides the "panel" node type.
8 * Panel nodes are useful to add additional content to the content area
12 // ---------------------------------------------------------------------------
13 // General Drupal hooks
16 * Implementation of hook_perm().
18 function panels_node_perm() {
19 return array('create panel-nodes', 'edit any panel-nodes', 'edit own panel-nodes', 'administer panel-nodes', 'delete any panel-nodes', 'delete own panel-nodes');
23 * Implementation of hook_menu().
25 function panels_node_menu() {
26 // Safety: go away if CTools is not at an appropriate version.
27 if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API
)) {
31 $items['admin/build/panels/settings/panel-node'] = array(
32 'title' => 'Panel nodes',
33 'description' => 'Configure which content is available to add to panel node displays.',
34 'access arguments' => array('administer panel-nodes'),
35 'page callback' => 'panels_node_settings',
36 'type' => MENU_LOCAL_TASK
,
39 // Avoid some repetition on these:
41 'access callback' => 'panels_node_edit_node',
42 'access arguments' => array(1),
43 'page arguments' => array(1),
44 'type' => MENU_LOCAL_TASK
,
47 $items['node/%node/panel_layout'] = array(
48 'title' => 'Panel layout',
49 'page callback' => 'panels_node_edit_layout',
53 $items['node/%node/panel_content'] = array(
54 'title' => 'Panel content',
55 'page callback' => 'panels_node_edit_content',
59 $items['node/add/panel/choose-layout'] = array(
60 'title' => 'Choose layout',
61 'access arguments' => array('create panel-nodes'),
62 'page callback' => 'panels_node_add',
63 'type' => MENU_CALLBACK
,
69 function panels_node_edit_node($node) {
70 if (!isset($node->panels_node
)) {
74 return node_access('update', $node);
77 // ---------------------------------------------------------------------------
81 * Implementation of hook_node_info().
83 function panels_node_node_info() {
84 // Safety: go away if CTools is not at an appropriate version.
85 if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API
)) {
92 'module' => 'panels_node',
93 'body_label' => t('Teaser'),
94 'description' => t("A panel layout broken up into rows and columns."),
100 * Implementation of hook_access().
102 function panels_node_access($op, $node, $account) {
103 if (user_access('administer panel-nodes', $account)) {
107 if ($op == 'create' && user_access('create panel-nodes', $account)) {
111 if ($op == 'update' && (user_access('edit any panel-nodes', $account) || $node->uid
== $account->uid
&& user_access('edit own panel-nodes', $account))) {
116 if ($op == 'delete' && (user_access('delete any panel-nodes') || $node->uid
== $account->uid
&& user_access('delete own panel-nodes'))) {
121 function panels_node_add() {
124 ctools_include('plugins', 'panels');
125 ctools_include('common', 'panels');
127 $layouts = panels_common_get_allowed_layouts('panels_node');
128 return panels_common_print_layout_links($layouts, 'node/add/panel', array('query' => $_GET));
132 * Implementation of hook_form().
134 function panels_node_form(&$node, &$param) {
135 ctools_include('plugins', 'panels');
137 $form['panels_node']['#tree'] = TRUE
;
138 if (empty($node->nid
) && arg(0) == 'node' && arg(1) == 'add') {
139 // Grab our selected layout from the $node, If it doesn't exist, try arg(3)
140 // and if that doesn't work present them with a list to pick from.
141 $panel_layout = isset($node->panel_layout
) ?
$node->panel_layout
: arg(3);
142 if (empty($panel_layout)) {
145 return drupal_goto('node/add/panel/choose-layout', $opts);
148 $layout = panels_get_layout($panel_layout);
149 if (empty($layout)) {
150 return drupal_not_found();
152 $form['panels_node']['layout'] = array(
154 '#value' => $panel_layout,
158 $type = node_get_types('type', $node);
160 $form['title'] = array(
161 '#type' => 'textfield',
162 '#title' => check_plain($type->title_label
),
164 '#default_value' => $node->title
,
167 if (!empty($type->body_label
)) {
168 $form['body_field']['#prefix'] = '<div class="body-field-wrapper">';
169 $form['body_field']['#suffix'] = '</div>';
170 $form['body_field']['body'] = array(
171 '#type' => 'textarea',
172 '#title' => check_plain($type->body_label
),
175 '#description' => t('The teaser is a piece of text to describe when the panel is listed (such as when promoted to front page); the actual content will only be displayed on the full node view.'),
176 '#default_value' => $node->body
,
178 $form['body_field']['format'] = filter_form($node->format
); // Now we can set the format!
181 // drupal_set_message('<pre>' . check_plain(var_export($node, true)) . '</pre>');
183 if (!empty($node->panels_node
['css_id'])) {
184 $css_id = $node->panels_node
['css_id'];
187 $form['panels_node']['css_id'] = array(
188 '#type' => 'textfield',
189 '#title' => t('CSS ID'),
191 '#description' => t('An ID that can be used by CSS to style the panel.'),
192 '#default_value' => $css_id,
195 // Support for different rendering pipelines
196 // Mostly borrowed from panel_context.inc
197 $pipelines = panels_get_renderer_pipelines();
199 // If there are no pipelines, that probably means we're operating in
201 if (empty($pipelines)) {
202 // We retain the original pipeline so we don't wreck things by installing
204 $form['panels_node']['pipeline'] = array(
206 '#value' => $node->panels_node
['pipeline'],
211 foreach ($pipelines as
$name => $pipeline) {
212 $options[$name] = check_plain($pipeline->admin_title
) .
'<div class="description">' .
check_plain($pipeline->admin_description
) .
'</div>';
215 $form['panels_node']['pipeline'] = array(
217 '#options' => $options,
218 '#title' => t('Renderer'),
219 '#default_value' => !empty($node->panels_node
['pipeline']) ?
$node->panels_node
['pipeline'] : 'standard',
227 * Implementation of hook_validate().
229 function panels_node_validate($node) {
230 if (!$node->nid
&& empty($node->panels_node
['layout'])) {
231 form_set_error('', t('Please select a layout.'));
236 * Implementation of hook_load().
238 * Panels does not use revisions for nodes because that would open us up
239 * to have completely separate displays, and we'd have to copy them,
240 * and that's going to be a LOT of data.
242 function panels_node_load($node) {
243 // We shortcut this because only in some really drastic corruption circumstance will this
245 $additions['panels_node'] = db_fetch_array(db_query("SELECT * FROM {panels_node} WHERE nid = %d", $node->nid
));
250 * Implementation of hook_insert().
252 function panels_node_insert(&$node) {
253 // Create a new display and record that.
254 $display = panels_new_display();
255 $display->layout
= $node->panels_node
['layout'];
257 // Special handling for nodes being imported from an export.module data dump.
258 if (!empty($node->export_display
)) {
259 // This works by overriding the $display set above
260 eval($node->export_display
);
261 unset($node->export_display
);
264 panels_save_display($display);
265 $css_id = $node->panels_node
['css_id'];
267 db_query("INSERT INTO {panels_node} (nid, did, css_id, pipeline) VALUES (%d, %d, '%s', '%s')", $node->nid
, $display->did
, $node->panels_node
['css_id'], $node->panels_node
['pipeline']);
269 $node->panels_node
['did'] = $display->did
;
273 * Implementation of hook_delete().
275 function panels_node_delete(&$node) {
276 db_query("DELETE FROM {panels_node} WHERE nid = %d", $node->nid
);
277 if (!empty($node->panels_node
['did'])) {
278 panels_delete_display($node->panels_node
['did']);
283 * Implementation of hook_update().
285 function panels_node_update($node) {
286 db_query("UPDATE {panels_node} SET css_id = '%s', pipeline = '%s' WHERE nid = %d", $node->panels_node
['css_id'], $node->panels_node
['pipeline'], $node->nid
);
290 * Implementation of hook_view().
292 function panels_node_view($node, $teaser = FALSE
, $page = FALSE
) {
293 static
$rendering = array();
295 // Prevent loops if someone foolishly puts the node inside itself:
296 if (!empty($rendering[$node->nid
])) {
300 $rendering[$node->nid
] = TRUE
;
301 ctools_include('plugins', 'panels');
303 // Do the standard view for teaser.
304 $node = node_prepare($node, $teaser);
305 // Because our teasier is never the same as our content, *always* provide
306 // the read more flag.
307 $node->readmore
= TRUE
;
310 if (!empty($node->panels_node
['did'])) {
311 $display = panels_load_display($node->panels_node
['did']);
312 $display->css_id
= $node->panels_node
['css_id'];
313 // TODO: Find a way to make sure this can't node_view.
314 $display->context
= panels_node_get_context($node);
315 $renderer = panels_get_renderer($node->panels_node
['pipeline'], $display);
316 $node->content
['body'] = array(
317 '#value' => panels_render_display($display, $renderer),
323 unset($rendering[$node->nid
]);
327 // ---------------------------------------------------------------------------
328 // Administrative pages
331 * Settings for panel nodes.
333 function panels_node_settings() {
334 ctools_include('common', 'panels');
335 return drupal_get_form('panels_common_settings', 'panels_node');
338 // ---------------------------------------------------------------------------
339 // Meat of the Panels API; almost completely passing through to panels.module
342 * Pass through to the panels layout editor.
344 function panels_node_edit_layout($node) {
345 // ctools_include('plugins', 'panels');
346 ctools_include('context');
347 $display = panels_load_display($node->panels_node
['did']);
348 $display->context
= panels_node_get_context($node);
349 return panels_edit_layout($display, t('Save'), "node/$node->nid/panel_layout", 'panels_node');
353 * Pass through to the panels content editor.
355 function panels_node_edit_content($node) {
356 // ctools_include('plugins', 'panels');
357 ctools_include('context');
358 $display = panels_load_display($node->panels_node
['did']);
359 $display->context
= panels_node_get_context($node);
360 ctools_include('common', 'panels');
361 $content_types = panels_common_get_allowed_types('panels_node', $display->context
);
363 // Print this with theme('page') so that blocks are disabled while editing a display.
364 // This is important because negative margins in common block layouts (i.e, Garland)
365 // messes up the drag & drop.
366 print theme('page', panels_edit($display, "node/$node->nid/panel_content", $content_types), FALSE
);
370 * Build the context to use for a panel node.
372 function panels_node_get_context(&$node) {
373 ctools_include('context');
374 $context = ctools_context_create('node', $node);
375 $context->identifier
= t('This node');
376 $context->keyword
= 'node';
377 return array('panel-node' => $context);
381 * Implementation of hook_export_node_alter()
383 * Integrate with export.module for saving panel_nodes into code.
385 function panels_node_export_node_alter(&$node, $original_node, $method) {
386 if ($method == 'export') {
387 $node_export_omitted = variable_get('node_export_omitted', array());
388 if (variable_get('node_export_method', '') != 'save-edit' && (array_key_exists('panel', $node_export_omitted) && !$node_export_omitted['panel'])) {
389 drupal_set_message(t("NOTE: in order to import panel_nodes you must first set the export.module settings to \"Save as a new node then edit\", otherwise it won't work."));
391 $display = panels_load_display($node->panels_node
['did']);
392 $export = panels_export_display($display);
393 $node->export_display
= $export;
398 * Implementation of hook_panels_dashboard_blocks().
400 * Adds panel nodes information to the Panels dashboard.
402 function panels_node_panels_dashboard_blocks(&$vars) {
403 $vars['links']['panels_node'] = array(
404 'title' => l(t('Panel node'), 'node/add/panel'),
405 'description' => t('Panel nodes are node content and appear in your searches, but are more limited than panel pages.'),