-
Notifications
You must be signed in to change notification settings - Fork 2
/
panelizer.module
2094 lines (1814 loc) · 65.1 KB
/
panelizer.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* The Panelizer module attaches panels to entities, providing default panels
* and allowing each panel to be configured independently by privileged users.
*/
define('PANELIZER_VERSION', '3.0');
// -----------------------------------------------------------------------
// Drupal core hooks
/**
* Implements hook_help().
*/
function panelizer_help($path, $arg) {
if ($path == 'admin/structure/panelizer') {
return '<p>' . t('Other than "Full page override" or "Default" (when applicable), only view modes enabled through the Custom Display Settings section of the <em>Manage Display</em> settings for that entity or bundle will be available for use.') . '</p>';
}
}
/**
* Implements hook_hook_info().
*/
function panelizer_hook_info() {
$hooks = array(
'panelizer_defaults_override_alter',
'panelizer_entity_plugin_process_alter',
'panelizer_operations_alter',
);
return array_fill_keys($hooks, array('group' => 'panelizer'));
}
/**
* Implements hook_permission().
*/
function panelizer_permission() {
$items = array(
'administer panelizer' => array(
'title' => t('administer panelizer'),
'description' => t('Fully administer panelizer and all panelizer settings.'),
),
);
// Delegate.
foreach (panelizer_get_plugins_with_hook('permission') as $handler) {
$handler->hook_permission($items);
}
return $items;
}
/**
* Implements hook_og_permission().
*/
function panelizer_og_permission() {
$items = array();
// Delegate.
foreach (panelizer_get_plugins_with_hook('permission') as $handler) {
if ($handler->entity_type == 'node') {
$handler->hook_permission($items);
}
}
$final = array(
'administer panelizer og_group defaults' => array(
'title' => t('Group: Administer Panelizer default panels, allowed content and settings.'),
'description' => t('Users with this permission can fully administer panelizer for this entity bundle.'),
),
'administer panelizer og_group overview' => array(
'title' => t('Group: Administer Panelizer overview.'),
'description' => t('Allow access to the panelizer overview page for the entity type/bundle. Note: This permission will be required for panelizer tabs to appear on an entity.'),
),
);
foreach (panelizer_operations() as $path => $operation) {
$final["administer panelizer og_group $path"] = array(
'title' => t('Group: Administer Panelizer @operation', array(
'@operation' => $operation['link title'],
)),
);
}
foreach ($items as $key => $item) {
// Get node bundle.
$words = explode(' ', $key);
$bundle = $words[3];
if (og_is_group_content_type('node', $bundle)) {
$final[$key] = $item;
}
}
return $final;
}
/**
* Implements hook_theme().
*/
function panelizer_theme() {
$items = array();
$items['panelizer_settings_page_table'] = array(
'render element' => 'element',
'file' => 'includes/admin.inc',
);
$items['panelizer_view_mode'] = array(
'render element' => 'element',
'template' => 'panelizer-view-mode',
'path' => drupal_get_path('module', 'panelizer') . '/templates',
);
// Delegate.
foreach (panelizer_get_plugins_with_hook('theme') as $handler) {
$handler->hook_theme($items);
}
return $items;
}
/**
* Implements hook_menu().
*/
function panelizer_menu() {
$items = array();
// Delegate admin menu stuff to admin.inc.
ctools_include('admin', 'panelizer');
panelizer_admin_hook_menu($items);
// Delegate.
foreach (panelizer_get_plugins_with_hook('menu') as $handler) {
$handler->hook_menu($items);
}
return $items;
}
/**
* Implements hook_menu_alter().
*/
function panelizer_menu_alter(&$items) {
// Delegate.
foreach (panelizer_get_plugins_with_hook('menu_alter') as $handler) {
$handler->hook_menu_alter($items);
}
}
/**
* Implements hook_admin_paths().
*/
function panelizer_admin_paths() {
$items = array();
// Delegate.
foreach (panelizer_get_plugins_with_hook('admin_paths') as $handler) {
$handler->hook_admin_paths($items);
}
return $items;
}
/**
* Implements hook_panels_ipe_access().
*/
function panelizer_panels_ipe_access($display) {
// We only care about Panels displays from panelizer.
if (isset($display->context['panelizer'])) {
// The type array contains 3 elements, where the first is the full context
// type (ie. 'entity:ENTITY_TYPE'), and the remaining two are the parts
// seperated by ':', so 'entity' and the entity type name.
$entity_type = $display->context['panelizer']->type[2];
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
// Only allow access to use the IPE if the user has 'update' access to
// the underlying entity.
$entity = $display->context['panelizer']->data;
return $handler->entity_access('update', $entity);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Check whether a given view mode is panelized. Set an error message if there
* are un-hidden fields because they won't be printed anyway.
*/
function panelizer_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
if (!empty($form['#entity_type']) && !empty($form['#bundle']) && !empty($form['#view_mode'])) {
module_load_install('panelizer');
if (panelizer_view_mode_extra_field_displays($form['#entity_type'], $form['#bundle'], $form['#view_mode'])) {
$message = t("This view mode is being controlled via Panelizer. For performance reasons, it is recommended to move all fields to 'hidden'. Fields not marked as hidden will be prepared for output but will not actually output, thus needlessly increasing render and page load time.");
drupal_set_message($message, 'error', FALSE);
}
}
}
/**
* Implements hook_form_alter().
*/
function panelizer_form_alter(&$form, &$form_state, $form_id) {
// Delegate.
foreach (panelizer_get_plugins_with_hook('form_alter') as $handler) {
$handler->hook_form_alter($form, $form_state, $form_id);
// Support default content and layout settings.
foreach ($handler->plugin['bundles'] as $bundle_name => $bundle) {
if ($form_id == 'panels_common_settings' && $form_state['build_info']['args'][0] == 'panelizer_' . $handler->entity_type . ':' . $bundle_name) {
// Provide settings for the default content and layout options.
$form['default_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Default settings'),
'#group' => 'additional_settings',
'#weight' => -20,
);
$form['default_settings']['default_content_settings'] = array(
'#title' => t('Use the same allowed content as standard Panels pages?'),
'#type' => 'checkbox',
'#default_value' => variable_get($form_state['build_info']['args'][0] . '_allowed_types_default', FALSE),
);
$form['default_settings']['default_layout_settings'] = array(
'#title' => t('Use the same allowed layouts as standard Panels pages?'),
'#type' => 'checkbox',
'#default_value' => variable_get($form_state['build_info']['args'][0] . '_allowed_layouts_default', FALSE),
);
// Disable the layout options when the default layout setting is enabled
if (!empty($form['layout_selection']['layouts']) && variable_get($form_state['build_info']['args'][0] . '_allowed_layouts_default', FALSE)) {
$form['layout_selection']['layouts']['#disabled'] = TRUE;
}
// Disable the content options when the default content setting is
// enabled.
if (variable_get($form_state['build_info']['args'][0] . '_allowed_types_default', FALSE)) {
$content_types = ctools_content_get_all_types();
$content_types['other'] = array('title' => t('Other'), 'weight' => 10);
foreach ($content_types as $content_type => $content_type_value) {
if (!empty($form['content_types'][$content_type]['options'])) {
$form['content_types'][$content_type]['options']['#disabled'] = TRUE;
}
}
$form['common']['panels_common_default']['#disabled'] = TRUE;
}
$form['#submit'][] = 'panelizer_panels_default_settings_submit';
}
}
}
}
/**
* Custom submission handler for setting default content and layout settings.
*/
function panelizer_panels_default_settings_submit($form, &$form_state) {
variable_set($form_state['values']['module_name'] . '_allowed_types_default', $form_state['values']['default_content_settings']);
variable_set($form_state['values']['module_name'] . '_allowed_layouts_default', $form_state['values']['default_layout_settings']);
}
/**
* Implements hook_process_page().
*/
function panelizer_process_page(&$variables) {
// Delegate.
// Target the theme layer to ensure we can manipulate the overview table.
foreach (panelizer_get_plugins_with_hook('page_alter') as $handler) {
$handler->hook_page_alter($variables['page']);
}
}
/**
* Implements hook_entity_load().
*/
function panelizer_entity_load(&$entities, $entity_type) {
// Delegate to the handler.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
$handler->hook_entity_load($entities);
}
}
/**
* Implements hook_entity_update().
*/
function panelizer_entity_update($entity, $entity_type) {
// Delegate to the handler.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
$handler->hook_entity_update($entity);
}
}
/**
* Implements hook_entity_insert().
*/
function panelizer_entity_insert($entity, $entity_type) {
// Delegate to the handler.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
$handler->hook_entity_insert($entity);
}
}
/**
* Implements hook_entity_delete().
*/
function panelizer_entity_delete($entity, $entity_type) {
// Delegate to the handler.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
$handler->hook_entity_delete($entity);
}
}
/**
* Implements hook_field_attach_delete_revision().
*/
function panelizer_field_attach_delete_revision($entity_type, $entity) {
// Delegate to the handler.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
$handler->hook_field_attach_delete_revision($entity);
}
}
function panelizer_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
// Delegate to the handler.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
$handler->hook_field_attach_form($entity, $form, $form_state, $langcode);
}
}
function panelizer_field_attach_submit($entity_type, $entity, &$form, &$form_state) {
// Delegate to the handler.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
$handler->hook_field_attach_submit($entity, $form, $form_state);
}
}
/**
* Implements hook_entity_view_alter().
*/
function panelizer_entity_view_alter(&$build, $entity_type) {
static $recursion_prevention = array();
// Prepare variables.
$handler = panelizer_entity_plugin_get_handler($entity_type);
if (!$handler) {
return;
}
$entity = $handler->get_entity_view_entity($build);
// Safety check in case the entity can't be loaded.
if (!$entity) {
return;
}
list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
// If the requested view mode does not exist, check if a substitute is
// assigned, otherwise rendering will fall back to 'default' and we should
// check that one instead.
$view_mode = $handler->get_view_mode($build['#view_mode'], $bundle);
// Make sure the bundle + view mode is actually panelized!
if (!$handler->is_panelized($bundle . '.' . $view_mode)) {
return;
}
// Also verify that the configuration exists. This may happen if a display is
// improperly configured.
if (empty($entity->panelizer[$view_mode])) {
return;
}
if (!empty($recursion_prevention[$entity_type][$entity_id][$view_mode])) {
return;
}
$recursion_prevention[$entity_type][$entity_id][$view_mode] = TRUE;
if ($info = $handler->render_entity($entity, $view_mode)) {
// Change theming function and add the content on the $build array.
$build['#theme'] = 'panelizer_view_mode';
$build['#panelizer'] = $entity->panelizer[$view_mode];
$build['#panelizer_content'] = $info;
$build['#panelizer_handler'] = $handler;
$build['#panelizer_entity'] = $entity;
$build['#panelizer_bundle'] = $bundle;
$build['#panelizer_entity_id'] = $entity_id;
}
$recursion_prevention[$entity_type][$entity_id][$view_mode] = FALSE;
}
/**
* Implements hook_node_update_index().
*/
function panelizer_node_update_index($node) {
// Populate search index for nodes managed via Panelizer if 'search_index'
// view mode is configured to do so.
if (
($handler = panelizer_entity_plugin_get_handler('node'))
&& ($view_mode = $handler->get_view_mode('search_index', $node->type))
&& $handler->is_panelized($node->type . '.' . $view_mode)
&& !empty($node->panelizer[$view_mode])
&& ($info = $handler->render_entity($node, $view_mode))
) {
$build['#view_mode'] = $view_mode;
$build['#theme'] = 'panelizer_view_mode';
$build['#panelizer'] = $node->panelizer[$view_mode];
$build['#panelizer_content'] = $info;
$build['#panelizer_handler'] = $handler;
$build['#panelizer_entity'] = $node;
$build['#panelizer_bundle'] = $node->type;
$build['#panelizer_entity_id'] = $node->nid;
return drupal_render($build);
}
}
/**
* Implements hook_node_search_result().
*/
function panelizer_node_search_result($node) {
// Populate search result highlighting input for nodes managed via Panelizer
// if 'search_result' view mode is configured to do so.
if (
($handler = panelizer_entity_plugin_get_handler('node'))
&& ($view_mode = $handler->get_view_mode('search_result', $node->type))
&& $handler->is_panelized($node->type . '.' . $view_mode)
&& !empty($node->panelizer[$view_mode])
&& ($info = $handler->render_entity($node, $view_mode))
) {
$build['#view_mode'] = $view_mode;
$build['#theme'] = 'panelizer_view_mode';
$build['#panelizer'] = $node->panelizer[$view_mode];
$build['#panelizer_content'] = $info;
$build['#panelizer_handler'] = $handler;
$build['#panelizer_entity'] = $node;
$build['#panelizer_bundle'] = $node->type;
$build['#panelizer_entity_id'] = $node->nid;
$node->rendered .= drupal_render($build);
}
}
// -----------------------------------------------------------------------
// Panels and CTools hooks
/**
* Implements hook_ctools_plugin_type().
*/
function panelizer_ctools_plugin_type() {
$items['entity'] = array(
'cache' => FALSE,
'process' => array(
'function' => 'panelizer_entity_plugin_process',
),
'classes' => array('handler'),
);
return $items;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function panelizer_ctools_plugin_directory($module, $plugin) {
if (in_array($module, array('panelizer', 'ctools', 'page_manager'))) {
return 'plugins/' . $plugin;
}
if ($module == 'panels' && $plugin == 'panels_storage') {
return 'plugins/' . $plugin;
}
}
/**
* Implements hook_ctools_plugin_api().
*/
function panelizer_ctools_plugin_api($module, $api) {
if (($module == 'page_manager' && $api == 'pages_default') || $module == 'panelizer') {
return array(
'version' => 1,
'path' => drupal_get_path('module', 'panelizer') . '/includes',
);
}
}
/**
* Implements hook_features_api().
*/
function panelizer_features_api() {
$api = array();
if (function_exists('_ctools_features_get_info') && defined('FEATURES_ALTER_TYPE_NONE')) {
$api['panelizer_defaults'] = _ctools_features_get_info('panelizer_defaults');
// CTools calls our hook_panelizer_defaults_alter so prevent Features from
// calling it too. FEATURES_ALTER_TYPE_INLINE means we are handling alter
// hooks ourselves here.
$api['panelizer_defaults']['alter_type'] = FEATURES_ALTER_TYPE_INLINE;
// Provide a separate alter hook for features_override.
$api['panelizer_defaults']['alter_hook'] = 'panelizer_defaults_override';
}
return $api;
}
/**
* Implementation of hook_views_api().
*/
function panelizer_views_api() {
return array(
'api' => 2.0,
'path' => drupal_get_path('module', 'panelizer') . '/plugins/views',
);
}
/**
* Implements hook_panelizer_defaults_alter().
*/
function panelizer_panelizer_defaults_alter(&$items) {
// Delegate.
foreach (panelizer_get_plugins_with_hook('panelizer_defaults') as $handler) {
$handler->hook_panelizer_defaults($items);
}
// If a default Panels display has no storage type, set it.
foreach ($items as &$panelizer) {
$display =& $panelizer->display;
if (empty($display->storage_type)) {
$display->storage_type = 'panelizer_default';
$display->storage_id = $panelizer->name;
}
}
// Allow features_overrides to alter the config.
drupal_alter('panelizer_defaults_override', $items);
}
/**
* Implements hook_default_page_manager_handlers().
*/
function panelizer_default_page_manager_handlers() {
$items = array();
// Delegate.
foreach (panelizer_get_plugins_with_hook('default_page_manager_handlers') as $handler) {
$handler->hook_default_page_manager_handlers($items);
}
return $items;
}
/**
* Implement CTools access form caching callback: get.
*/
function panelizer_ctools_access_get($argument) {
list($entity_type, $bundle, $name) = explode(':', $argument);
$handler = panelizer_entity_plugin_get_handler($entity_type);
$panelizer = $handler->get_default_panelizer_object($bundle, $name);
if (empty($panelizer)) {
return;
}
if (!$handler->access_default_panelizer_object($panelizer)) {
return;
}
// First, see if there's a cache.
ctools_include('object-cache');
$access = ctools_object_cache_get('panelizer_access', $argument);
if (!$access) {
$access = $panelizer->access;
}
$context = $handler->get_contexts($panelizer);
return array($access, $context);
}
/**
* Implement CTools access form caching callback: set.
*/
function panelizer_ctools_access_set($argument, $access) {
list($entity_type, $bundle, $name) = explode(':', $argument);
$handler = panelizer_entity_plugin_get_handler($entity_type);
$panelizer = $handler->get_default_panelizer_object($bundle, $name);
if (empty($panelizer)) {
return;
}
if (!$handler->access_default_panelizer_object($panelizer)) {
return;
}
ctools_include('object-cache');
ctools_object_cache_set('panelizer_access', $argument, $access);
}
/**
* Implement CTools access form caching callback: get.
*/
function panelizer_ctools_access_clear($argument) {
list($entity_type, $bundle, $name) = explode(':', $argument);
$handler = panelizer_entity_plugin_get_handler($entity_type);
$panelizer = $handler->get_default_panelizer_object($bundle, $name);
if (empty($panelizer)) {
return;
}
if (!$handler->access_default_panelizer_object($panelizer)) {
return;
}
ctools_include('object-cache');
ctools_object_cache_clear('panelizer', $argument);
}
// -----------------------------------------------------------------------
// CTools entity plugin support code.
/**
* CTools process callback for an entity plugin.
*
* This adds configuration data to the plugin so that we know what bundles it is
* enabled for.
*/
function panelizer_entity_plugin_process(&$plugin, $info) {
$entity_type = $plugin['name'];
$entity_info = entity_get_info($entity_type);
$plugin['bundles'] = array();
if ($entity_info) {
foreach ($entity_info['bundles'] as $bundle => $label) {
if ($settings = variable_get('panelizer_defaults_' . $entity_type . '_' . $bundle, array())) {
// Translate from settings that existed prior to view mode support.
if (empty($settings['view modes'])) {
$old_settings = $settings;
$settings = array('view modes' => array());
if (empty($plugin['uses page manager'])) {
$settings['view modes']['default'] = $old_settings;
}
else {
$settings['view modes']['page_manager'] = $old_settings;
}
$settings['status'] = $old_settings['status'];
}
$plugin['bundles'][$bundle] = $settings;
// Build the custom settings of the view modes for this bundle.
$view_mode_settings = field_view_mode_settings($entity_type, $bundle);
foreach ($entity_info['view modes'] as $view_mode_name => $view_mode_info) {
$plugin['view mode status'][$bundle][$view_mode_name] = !empty($view_mode_settings[$view_mode_name]['custom_settings']);
}
}
}
// Add our fake view modes.
$plugin['view modes'] = array(
'page_manager' => array(
'label' => t('Full page override'),
),
'default' => array(
'label' => t('Default'),
),
);
if (!empty($entity_info['view modes'])) {
foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
$plugin['view modes'][$view_mode] = $view_mode_info;
}
}
// It seems silly to unset this after but the logic is cleaner to read.
if (empty($plugin['uses page manager'])) {
unset($plugin['view modes']['page_manager']);
}
}
drupal_alter('panelizer_entity_plugin_process', $plugin, $info);
}
/**
* Fetch a single entity plugin.
*/
function panelizer_get_entity_plugin($entity_type) {
ctools_include('plugins');
return ctools_get_plugins('panelizer', 'entity', $entity_type);
}
/**
* Fetch all entity plugin.
*/
function panelizer_get_entity_plugins() {
ctools_include('plugins');
return ctools_get_plugins('panelizer', 'entity');
}
/**
* Get the class to handle custom code for a given entity type plugin.
*
* If a plugin does not define a class at all, then the default class.
*
* @return
* Either the instantiated handler or FALSE if one could not be had.
*/
function panelizer_entity_plugin_get_handler($plugin) {
// The default plugin handler is abstract and cannot be loaded.
if ($plugin == 'default') {
return;
}
$cache = &drupal_static(__FUNCTION__, array());
// If a string was passed, turn it into a plugin.
if (is_string($plugin)) {
$plugin = panelizer_get_entity_plugin($plugin);
if (!$plugin) {
return FALSE;
}
}
// Get the class name from the 'handler' property if we have not already
// cached a handler.
if (empty($cache[$plugin['name']]) && ($class = ctools_plugin_get_class($plugin, 'handler'))) {
// @todo is there a good reason to use ->init instead of __construct?
$cache[$plugin['name']] = new $class();
$cache[$plugin['name']]->init($plugin);
}
return !empty($cache[$plugin['name']]) ? $cache[$plugin['name']] : FALSE;
}
/**
* Load handler to get a plugin as a menu callback.
*/
function panelizer_handler_load($entity_type) {
return panelizer_entity_plugin_get_handler($entity_type);
}
/**
* Fetch handler objects for all plugins that implement the named hook.
*
* These plugins must set $plugin['hooks'][$hook] = TRUE in order to be
* instantiated.
*
* This is only called for system wide hooks such as hook_menu and
* hook_menu_alter; entity specific hooks will always be called.
*/
function panelizer_get_plugins_with_hook($hook) {
$objects = array();
$plugins = panelizer_get_entity_plugins();
foreach ($plugins as $entity_type => $plugin) {
if (!empty($plugin['hooks'][$hook])) {
if ($handler = panelizer_entity_plugin_get_handler($plugin)) {
$objects[$entity_type] = $handler;
}
}
}
return $objects;
}
/**
* Page callback for entity menu callbacks.
*
* This function is to be used as a menu callback for menu items that are to be
* handled by a method on the handler object. It loads the object defined in the
* plugin and hands it off to a method based upon the name of the operation in
* use.
*
* For example, if the 'op' is 'revision' then the callback method will be
* 'page_revisions', with all of the arguments *except* the $op and the plugin
* name.
*/
function panelizer_entity_plugin_switcher_page($entity_type, $op) {
$args = func_get_args();
// Load the $plugin information.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
// Replace the first two arguments.
$args[0] = !empty($_REQUEST['js']);
$args[1] = $_POST;
if (empty($args[3])) {
$args[3] = 'page_manager';
}
$method = 'page_' . $op;
if (method_exists($handler, $method)) {
return call_user_func_array(array($handler, $method), $args);
}
// Check to see if this is an operation from panelizer_operations with a
// callback instead.
$operations = panelizer_operations();
if (isset($operations[$op]) && isset($operations[$op]['entity callback']) && function_exists($operations[$op]['entity callback'])) {
array_unshift($args, $handler);
return call_user_func_array($operations[$op]['entity callback'], $args);
}
}
else {
return t('Configuration error. No handler found.');
}
}
/**
* Callback used for switching callbacks into the proper plugin.
*/
function panelizer_entity_plugin_callback_switcher($entity_type, $switcher_type, $op) {
$args = func_get_args();
if (count($args) < 3) {
return FALSE;
}
$entity_type = array_shift($args);
$switcher_type = array_shift($args);
$op = array_shift($args);
// Load the $plugin information.
if ($handler = panelizer_entity_plugin_get_handler($entity_type)) {
$method = $switcher_type . '_' . $op;
if (method_exists($handler, $method)) {
return call_user_func_array(array($handler, $method), $args);
}
}
else {
return FALSE;
}
}
/**
* Page callback to delegate to either the settings page or list page.
*/
function panelizer_default_list_or_settings_page($handler, $bundle, $name, $view_mode, $operation = 'list', $item = NULL) {
if (is_string($handler)) {
$handler = panelizer_entity_plugin_get_handler($handler);
}
// We need a version of $bundle with $view_mode but we need to retain one
// without it so we can pass straight $bundle to the settings page.
$test_bundle = $bundle;
if ($view_mode) {
$test_bundle .= '.' . $view_mode;
}
if ($handler->has_panel_choice($test_bundle)) {
// Call through to the UI switcher for the list.
ctools_include('export-ui');
return panelizer_export_ui_switcher_page($handler, $test_bundle, 'panelizer_defaults', $operation, $item);
}
else {
return panelizer_default_settings_page($handler, $bundle, $name, $view_mode);
}
}
/**
* Specialized version of ctools_export_ui_switcher_page().
*
* This one is designed to set our entity handler and bundle on the object so we
* can refer to it later without having to override all of the entry points.
*/
function panelizer_export_ui_switcher_page($entity_handler, $bundle, $plugin_name, $op) {
$args = func_get_args();
// Remove the handler and the bundle.
array_shift($args);
array_shift($args);
$js = !empty($_REQUEST['js']);
// Break our bundle up as necessary.
if (strpos($bundle, '.') !== FALSE) {
list($bundle, $view_mode) = explode('.', $bundle);
}
else {
$view_mode = 'page_manager';
}
// Load the $plugin information.
ctools_include('export-ui');
$plugin = ctools_get_export_ui($plugin_name);
$handler = ctools_export_ui_get_handler($plugin);
if ($handler) {
if (is_string($entity_handler)) {
$entity_handler = panelizer_entity_plugin_get_handler($entity_handler);
}
$handler->entity_handler = $entity_handler;
$handler->entity_bundle = $bundle;
$handler->entity_view_mode = $view_mode;
if (empty($entity_handler->entity_admin_root) || substr($_GET['q'], 30) == 'admin/structure/panelizer') {
$handler->plugin['menu']['menu prefix'] = 'admin/structure/panelizer' . $entity_handler->entity_type;
$handler->plugin['menu']['menu item'] = $bundle;
}
else {
$base_path = $entity_handler->entity_admin_root . '/panelizer/' . $view_mode;
if (is_numeric($entity_handler->entity_admin_bundle)) {
$bits = explode('/', $base_path);
$bits[$entity_handler->entity_admin_bundle] = $bundle;
$base_path = implode('/', $bits);
}
$handler->plugin['menu']['menu prefix'] = dirname($base_path);
$handler->plugin['menu']['menu item'] = basename($base_path);
foreach ($handler->plugin['menu']['items'] as $key => &$item) {
$item['path'] = str_replace('list/', '', $item['path']);
}
}
$path = $handler->plugin['menu']['menu prefix'] . '/' . $handler->plugin['menu']['menu item'];
foreach ($handler->plugin['redirect'] as $key => $old_path) {
if ($key == 'add') {
$handler->plugin['redirect'][$key] = $path . '/%ctools_export_ui/settings';
}
else {
$handler->plugin['redirect'][$key] = $path;
}
}
$method = $op . '_page';
if (method_exists($handler, $method)) {
// Replace the first two arguments.
$args[0] = $js;
$args[1] = $_POST;
return call_user_func_array(array($handler, $method), $args);
}
}
else {
return t('Configuration error. No handler found.');
}
}
// ---------------------------------------------------------------------------
// Menu callbacks
/**
* Title callback to properly set the tile when editing panelizer defaults.
*/
function panelizer_default_title_callback($handler, $bundle) {
if (is_string($handler)) {
$handler = panelizer_entity_plugin_get_handler($handler);
}
if (!$handler) {
return '';
}
$entity_info = entity_get_info($handler->entity_type);
$title = $entity_info['label'];
if (strpos($bundle, '.') === FALSE) {
$bundle = $bundle;
$view_mode = '';
}
else {
list($bundle, $view_mode) = explode('.', $bundle);
}
$title .= ' | ' . $handler->get_bundle_title($bundle);
if ($view_mode && !empty($handler->plugin['view modes'][$view_mode]['label'])) {
$title .= ' | ' . $handler->plugin['view modes'][$view_mode]['label'];
}
return $title;
}
/**
* Menu callback to determine if a type has a choice of defaults.
*
* We use this to make sure the right tabs appear.
*/
function panelizer_has_choice_callback($handler, $bundle, $name = NULL) {
if (is_string($handler)) {
$handler = panelizer_entity_plugin_get_handler($handler);
}
if (empty($handler)) {
return FALSE;
}
if (!panelizer_administer_entity_bundle($handler, $bundle)) {
return FALSE;
}
// Check to see if $name is valid.
if (!empty($name) && !$handler->get_default_panelizer_object($bundle, $name)) {
return FALSE;
}
return $handler->has_panel_choice($bundle);
}
/**
* Menu callback to determine if a type+view_mode has a choice of defaults.
*/
function panelizer_has_choice_callback_view_mode($handler, $bundle, $view_mode) {
return panelizer_has_choice_callback($handler, $bundle . '.' . $view_mode);
}
/**
* Menu callback to determine if a type has a choice of defaults.
*
* Use to make sure the right tabs appear.
*/
function panelizer_has_no_choice_callback($handler, $bundle, $view_mode = NULL) {
if (is_string($handler)) {