Projet

Général

Profil

Paste
Télécharger (16,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panelizer / plugins / task_handlers / panelizer_node.inc @ a2bb1a14

1
<?php
2
/**
3
 * @file
4
 *
5
 * This is the task handler plugin to handle an entity view.
6
 * NOTE: This is named panelizer_node for historical reasons. It is too much
7
 * of a pain to change the name of a task handler. This panelizes any entity
8
 * not just a node.
9
 */
10

    
11
// Plugin definition
12
$plugin = array(
13
  // is a 'context' handler type, meaning it supports the API of the
14
  // context handlers provided by ctools context plugins.
15
  'handler type' => 'context',
16
  // Administrative fields.
17
  'title' => t('Panelizer'),
18
  'forms' => array(),
19
  'admin title' => 'panelizer_panelizer_task_title',
20
  'admin summary' => 'panelizer_panelizer_task_admin_summary',
21
  'operations' => array(),
22
  // Callback to render the data.
23
  'render' => 'panelizer_panelizer_task_render',
24
  // Callback to return addressable data
25
  'addressable callback' => 'panelizer_panelizer_task_get_addressable',
26

    
27
  'test' => 'panelizer_panelizer_task_test',
28
  'visible' => TRUE,
29

    
30
  // Provide custom panelizer specific contextual links.
31
  'contextual link' => 'panelizer_panelizer_task_contextual_link',
32

    
33
  'operations' => array(
34
    'settings' => array(
35
      'title' => t('General'),
36
      'description' => t('Change general settings for this variant.'),
37
      'form' => 'panelizer_panelizer_task_edit_settings',
38
    ),
39
    'criteria' => array(
40
      'title' => t('Selection rules'),
41
      'description' => t('Control the criteria used to decide whether or not this variant is used.'),
42
      'ajax' => FALSE,
43
      'form' => array(
44
        'order' => array(
45
          'form' => t('Selection rules'),
46
        ),
47
        'forms' => array(
48
          'form' => array(
49
            'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
50
            'form id' => 'ctools_context_handler_edit_criteria',
51
          ),
52
        ),
53
      ),
54
    ),
55
    'context' => array(
56
      'title' => t('Contexts'),
57
      'ajax' => FALSE,
58
      'description' => t('Add additional context objects to this variant that can be used by the content.'),
59
      'form' => array(
60
        'order' => array(
61
          'form' => t('Context'),
62
        ),
63
        'forms' => array(
64
          'form' => array(
65
            'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
66
            'form id' => 'ctools_context_handler_edit_context',
67
          ),
68
        ),
69
      ),
70
    ),
71
  ),
72
  'default conf' => array(
73
    'title' => t('Panelizer'),
74
    'contexts' => array(),
75
    'relationships' => array(),
76
    'context' => array(),
77
  ),
78

    
79
);
80

    
81
/**
82
 * Figure out the correct context to use for this panelizer.
83
 */
84
function _panelizer_panelizer_task_get_context($handler, $contexts) {
85
  $contexts = ctools_context_handler_get_handler_contexts($contexts, $handler);
86

    
87
  if (isset($handler->conf['context']) && is_string($handler->conf['context']) && !empty($contexts[$handler->conf['context']])) {
88
    return $contexts[$handler->conf['context']];
89
  }
90

    
91
  // If one was not set up, we could be using old, saved data. Assume that
92
  // this means we just want the first context available.
93
  if (!empty($contexts)) {
94
    return reset($contexts);
95
  }
96

    
97
  // Fail!
98
  return new stdClass();
99
}
100

    
101
/**
102
 * Get extra contexts to pass from page manager to panelizer.
103
 *
104
 * @param $handler
105
 *   The handler object.
106
 * @param $base_contexts
107
 *   The base contexts from page manager.
108
 *
109
 * @return array
110
 *   All contexts defined in page manager other than the panelized entity
111
 *   context.
112
 */
113
function _panelizer_panelizer_task_get_extra_contexts($handler, $base_contexts) {
114
  $extra_contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler);
115

    
116
  // Remove the panelized context to prevent duplication in the extra contexts.
117
  if (isset($handler->conf['context']) && !empty($extra_contexts[$handler->conf['context']])) {
118
    unset($extra_contexts[$handler->conf['context']]);
119
  }
120
  elseif (!empty($extra_contexts)) {
121
    array_shift($extra_contexts);
122
  }
123

    
124
  return $extra_contexts;
125
}
126

    
127
/**
128
 * Provide appropriate contextual links for Panelizer controlled entities.
129
 */
130
function panelizer_panelizer_task_contextual_link($handler, $plugin, $contexts, $args) {
131
  $links = array();
132

    
133
  $context = _panelizer_panelizer_task_get_context($handler, $contexts);
134
  if (empty($context->data)) {
135
    return;
136
  }
137

    
138
  $entity = &$context->data;
139

    
140
  // Only show links for this view mode.
141
  $view_mode = 'page_manager';
142

    
143
  if (empty($entity->panelizer[$view_mode])) {
144
    return FALSE;
145
  }
146

    
147
  $panelizer = $entity->panelizer[$view_mode];
148
  // One of these two will always be set.
149
  $entity_type = !empty($panelizer->entity_type) ? $panelizer->entity_type : $panelizer->panelizer_type;
150

    
151
  if ($entity_handler = panelizer_entity_plugin_get_handler($entity_type)) {
152
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
153

    
154
    // @todo there's code on the entity handler to do this path thing for us.
155
    $bits = explode('/', $entity_handler->plugin['entity path']);
156
    foreach ($bits as $count => $bit) {
157
      if (strpos($bit, '%') === 0) {
158
        $bits[$count] = $entity_id;
159
      }
160
    }
161

    
162
    $bits[] = 'panelizer';
163
    $bits[] = $view_mode;
164

    
165
    $base_path = implode('/', $bits);
166
    if ($entity_handler->panelizer_access('settings', $entity, $view_mode)) {
167
      $links['settings'] = array(
168
        'title' => t("Edit custom display settings"),
169
        'href' => "$base_path/settings",
170
      );
171
    }
172
    if ($entity_handler->panelizer_access('context', $entity, $view_mode)) {
173
      $links['context'] = array(
174
        'title' => t('Edit custom display contexts'),
175
        'href' => "$base_path/context",
176
      );
177
    }
178
    if ($entity_handler->panelizer_access('layout', $entity, $view_mode)) {
179
      $links['layout'] = array(
180
        'title' => t('Edit custom display layout'),
181
        'href' => "$base_path/layout",
182
      );
183
    }
184
    if ($entity_handler->panelizer_access('content', $entity, $view_mode)) {
185
      $links['content'] = array(
186
        'title' => t('Edit custom display content'),
187
        'href' => "$base_path/content",
188
      );
189
    }
190

    
191
    // Work out what the default display is called.
192
    $default_display_name = $entity_handler->get_default_display_default_name($bundle, $view_mode);
193
    // If the default display is defined, add links to edit it.
194
    if (!empty($default_display_name)) {
195
      $template_base_path = "admin/structure/types/manage/{$bundle}/panelizer/{$view_mode}/{$default_display_name}";
196
      if ($entity_handler->panelizer_access('settings', $entity, $view_mode)) {
197
        $links['default_settings'] = array(
198
          'title' => t('Edit default display settings'),
199
          'href' => "$template_base_path/settings",
200
        );
201
      }
202
      if ($entity_handler->panelizer_access('context', $entity, $view_mode)) {
203
        $links['default_context'] = array(
204
          'title' => t('Edit default display contexts'),
205
          'href' => "$template_base_path/context",
206
        );
207
      }
208
      if ($entity_handler->panelizer_access('layout', $entity, $view_mode)) {
209
        $links['default_layout'] = array(
210
          'title' => t('Edit default display layout'),
211
          'href' => "$template_base_path/layout",
212
        );
213
      }
214
      if ($entity_handler->panelizer_access('content', $entity, $view_mode)) {
215
        $links['default_content'] = array(
216
          'title' => t('Edit default display content'),
217
          'href' => "$template_base_path/content",
218
        );
219
      }
220
    }
221
  }
222

    
223
  return $links;
224
}
225

    
226
/**
227
 * Callback to provide administrative summary of the task handler.
228
 */
229
function panelizer_panelizer_task_admin_summary($handler, $task, $subtask, $page, $show_title = TRUE) {
230
  ctools_include('context');
231
  ctools_include('context-task-handler');
232

    
233
  $output = '';
234

    
235
  $output .= '<div class="clear-block">';
236
  if ($show_title) {
237
    // Get the operations
238
    $operations = page_manager_get_operations($page);
239

    
240
    // Get operations for just this handler.
241
    $operations = $operations['handlers']['children'][$handler->name]['children']['actions']['children'];
242
    $args = array('handlers', $handler->name, 'actions');
243
    $rendered_operations = page_manager_render_operations($page, $operations, array(), array('class' => array('actions')), 'actions', $args);
244

    
245
    $output .= '<div class="handler-title clear-block">';
246
    $output .= '<div class="actions handler-actions">' . $rendered_operations['actions'] . '</div>';
247
    $output .= '<span class="title-label">' . t('Panelizer') . '</span>';
248
    $output .= '</div>';
249
  }
250

    
251
  $plugin = page_manager_get_task_handler($handler->handler);
252

    
253
  $object = ctools_context_handler_get_task_object($task, $subtask, $handler);
254
  $context = ctools_context_load_contexts($object, TRUE);
255

    
256
  $access = ctools_access_group_summary(!empty($handler->conf['access']) ? $handler->conf['access'] : array(), $context);
257
  if ($access) {
258
    $access = t('This variant will be selected if the entity being viewed is panelized AND @conditions. This variant must be enabled and selected for panelizer to work!', array('@conditions' => $access));
259
  }
260
  else {
261
    $access = t('This variant will be selected if the entity being viewed is panelized. This variant must be enabled and selected for panelizer to work!');
262
  }
263

    
264
  $rows[] = array(
265
    array(
266
      'class' => array('page-summary-label'),
267
      'data' => t('Selection rule'),
268
    ),
269
    array(
270
      'class' => array('page-summary-data'),
271
      'data' => $access,
272
    ),
273
    array('class' => array('page-summary-operation'), ''),
274
  );
275

    
276
  $output .= theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => array('page-manager-handler-summary'))));
277
  $output .= '</div>';
278

    
279
  return $output;
280
}
281

    
282
/**
283
 * Render a entity that has been panelized.
284
 */
285
function panelizer_panelizer_task_render($handler, $base_contexts, $args, $test = TRUE) {
286
  // Get the context this is viewing; figure out what entity it is and load
287
  // the right plugin.
288
  ctools_include('context');
289
  $context = _panelizer_panelizer_task_get_context($handler, $base_contexts);
290
  if (!$context) {
291
    return FALSE;
292
  }
293

    
294
  if (empty($context->data->panelizer['page_manager'])) {
295
    return FALSE;
296
  }
297

    
298
  $panelizer = $context->data->panelizer['page_manager'];
299
  // One of these two will always be set.
300
  $entity_type = !empty($panelizer->entity_type) ? $panelizer->entity_type : $panelizer->panelizer_type;
301

    
302
  $address = implode('::', array('page_manager', $handler->task, $handler->subtask, $handler->name, implode('..', $args)));
303

    
304
  if ($entity_handler = panelizer_entity_plugin_get_handler($entity_type)) {
305
    $extra_contexts = _panelizer_panelizer_task_get_extra_contexts($handler, $base_contexts);
306
    $info = $entity_handler->render_entity($context->data, 'page_manager', NULL, $args, $address, $extra_contexts);
307

    
308
    // Add body element classes, appending to the existing value.
309
    $panel_body_css = &drupal_static('panel_body_css', array());
310
    if (!empty($info['classes_array'])){
311
      $panel_body_css += array('body_classes_to_add' => '');
312
      $panel_body_css['body_classes_to_add'] = trim($panel_body_css['body_classes_to_add'] . ' ' . implode(' ', $info['classes_array']));
313
    }
314

    
315
    return $info;
316
  }
317
}
318

    
319
/**
320
 * Determine if the panelizer task handler should fire.
321
 *
322
 * This returns true if the configured entity is panelized and has
323
 * a display.
324
 */
325
function panelizer_panelizer_task_test($handler, $base_contexts) {
326
  ctools_include('context');
327

    
328
  if (!ctools_context_handler_select($handler, $base_contexts)) {
329
    return;
330
  }
331

    
332
  $context = _panelizer_panelizer_task_get_context($handler, $base_contexts);
333
  if (empty($context->data)) {
334
    return;
335
  }
336

    
337
  $entity = &$context->data;
338
  // Fail if there isn't a panelizer object on the entity.
339
  if (empty($entity->panelizer['page_manager']->display)) {
340
    return;
341
  }
342

    
343
  $panelizer = $entity->panelizer['page_manager'];
344
  $entity_type = !empty($panelizer->entity_type) ? $panelizer->entity_type : $panelizer->panelizer_type;
345

    
346
  $panelizer_handler = panelizer_entity_plugin_get_handler($entity_type);
347
  if (!$panelizer_handler) {
348
    return;
349
  }
350

    
351
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
352

    
353
  // Make sure the bundle + view mode is actually panelized!
354
  return $panelizer_handler->is_panelized($bundle . '.page_manager');
355
}
356

    
357
function panelizer_panelizer_task_get_addressable($task, $subtask_name, $handler, $address, $contexts, $arguments, $type) {
358
  ctools_include('plugins', 'panels');
359
  if (empty($contexts)) {
360
    return;
361
  }
362

    
363
  $context = _panelizer_panelizer_task_get_context($handler, $contexts);
364
  if (empty($context->data)) {
365
    return;
366
  }
367

    
368
  // Extract the entity from the context so we can load the panelizer.
369
  $entity = &$context->data;
370

    
371
  if (empty($entity->panelizer['page_manager']) || empty($entity->panelizer['page_manager']->display)) {
372
    return;
373
  }
374

    
375
  // Load the panelizer info.
376
  $panelizer = $entity->panelizer['page_manager'];
377
  // Load the display.
378
  $display = $panelizer->display;
379

    
380
  // One of these two will always be set.
381
  $entity_type = !empty($panelizer->entity_type) ? $panelizer->entity_type : $panelizer->panelizer_type;
382
  $handler = panelizer_entity_plugin_get_handler($entity_type);
383
  if (!$handler) {
384
    return;
385
  }
386

    
387
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
388

    
389
  $display->context = $handler->get_contexts($panelizer, $entity);
390
  $display->args = $arguments;
391
  $display->css_id = $panelizer->css_id;
392

    
393
  $display->cache_key = implode(':', array('panelizer', $entity_type, $entity_id));
394

    
395
  $renderer = panels_get_renderer($panelizer->pipeline, $display);
396
  $renderer->prepare();
397

    
398
  $pid = array_shift($address);
399
  if (!empty($renderer->prepared['panes'][$pid])) {
400
    if ($type == 'content') {
401
      return $renderer->render_pane($renderer->prepared['panes'][$pid]);
402
    }
403
    elseif ($type == 'pane') {
404
      return $renderer->prepared['panes'][$pid];
405
    }
406
  }
407
}
408

    
409
/**
410
 * General settings for the panel
411
 */
412
function panelizer_panelizer_task_edit_settings($form, &$form_state) {
413
  ctools_include('context');
414
  ctools_include('context-task-handler');
415
  $conf = $form_state['handler']->conf;
416

    
417
  $form['title'] = array(
418
    '#type' => 'textfield',
419
    '#default_value' => !empty($conf['title']) ? $conf['title'] : t('Panelizer'),
420
    '#title' => t('Administrative title'),
421
    '#description' => t('Administrative title of this variant.'),
422
  );
423
  $name = isset($conf['name']) ? $conf['name'] : FALSE;
424
  $form['name'] = array(
425
    '#type' => 'machine_name',
426
    '#title' => t('Machine name'),
427
    '#required' => FALSE,
428
    '#default_value' => $name,
429
    '#description' => t("A unique machine-readable name for this variant. It must only contain lowercase letters, numbers, and underscores. This name will be used when exporting the variant. If left empty the variant's name will be used instead."),
430
    '#size' => 32,
431
    '#maxlength' => 32,
432
    '#machine_name' => array(
433
      'exists' => 'page_manager_handler_check_machine_name',
434
      'source' => array('title'),
435
    ),
436
    '#field_prefix' => '<span dir="ltr">' . $form_state['task_name'] . '__',
437
    '#field_suffix' => '</span>&lrm;',
438
  );
439

    
440
  $contexts = ctools_context_handler_get_all_contexts($form_state['task'], $form_state['subtask'], $form_state['handler']);
441

    
442
  $required_context = new ctools_context_required(t('Panelized entity'), 'entity');
443
  $form['context'] = ctools_context_selector($contexts, $required_context, isset($conf['context']) ? $conf['context'] : '');
444

    
445
  return $form;
446
}
447

    
448
function panelizer_panelizer_task_edit_settings_submit($form, &$form_state) {
449
  $machine_name = $form_state['handler']->name;
450
  $name = $form_state['task_id'] . '__' . $form_state['values']['name'];
451

    
452
  // If new name doesn't equal machine name, we need to update and redirect.
453
  if ($machine_name !== $name) {
454
    $form_state['new trail'] = $form_state['trail'];
455
    $delta = array_search($machine_name, $form_state['new trail']);
456
    $form_state['new trail'][$delta] = $name;
457
    $form_state['handler']->name = $name;
458
  }
459

    
460
  $form_state['handler']->conf['title'] = $form_state['values']['title'];
461
  $form_state['handler']->conf['name'] = $form_state['values']['name'];
462
  $form_state['handler']->conf['context'] = $form_state['values']['context'];
463
}
464

    
465
/**
466
 * Set up a title for the display based upon the selection rules.
467
 */
468
function panelizer_panelizer_task_title($handler, $task, $subtask) {
469
  if (isset($handler->conf['title'])) {
470
    return check_plain($handler->conf['title']);
471
  }
472
  else {
473
    return t('Panelizer');
474
  }
475
}