d36deaba85479a22c6b0146044424b8c5c977562
[project/panels.git] / panels_mini / plugins / content_types / panels_mini.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Contains the content type plugin for a mini panel. While this does not
7 * need to be broken out into a .inc file, it's convenient that we do so
8 * that we don't load code unneccessarily. Plus it demonstrates plugins
9 * in modules other than Panels itself.
10 *
11 */
12
13 /**
14 * Specially named hook. for .inc file. This looks a little silly due to the
15 * redundancy, but that's really just because the content type shares a
16 * name with the module.
17 */
18 function panels_mini_panels_mini_ctools_content_types() {
19 return array(
20 'title' => t('Mini panels'),
21 'content type' => 'panels_mini_panels_mini_content_type_content_type',
22 );
23 }
24
25 /**
26 * Return each available mini panel available as a subtype.
27 */
28 function panels_mini_panels_mini_content_type_content_type($subtype_id, $plugin) {
29 $mini = panels_mini_load($subtype_id);
30 return _panels_mini_panels_mini_content_type_content_type($mini);
31 }
32
33 /**
34 * Return each available mini panel available as a subtype.
35 */
36 function panels_mini_panels_mini_content_type_content_types($plugin) {
37 $types = array();
38 foreach (panels_mini_load_all() as $mini) {
39 $type = _panels_mini_panels_mini_content_type_content_type($mini);
40 if ($type) {
41 $types[$mini->name] = $type;
42 }
43 }
44 return $types;
45 }
46
47 /**
48 * Return an info array describing a single mini panel.
49 */
50 function _panels_mini_panels_mini_content_type_content_type($mini) {
51 if (!empty($mini->disabled)) {
52 return;
53 }
54
55 $title = filter_xss_admin($mini->admin_title);
56 $type = array(
57 'title' => $title,
58 // For now mini panels will just use the contrib block icon.
59 'icon' => 'icon_mini_panel.png',
60 'description' => $title,
61 'category' => !empty($mini->category) ? $mini->category : t('Mini panel'),
62 );
63 if (!empty($mini->requiredcontexts)) {
64 $type['required context'] = array();
65 foreach ($mini->requiredcontexts as $context) {
66 $info = ctools_get_context($context['name']);
67 // TODO: allow an optional setting
68 $type['required context'][] = new ctools_context_required($context['identifier'], $info['context name']);
69 }
70 }
71 return $type;
72 }
73
74 /**
75 * Render a mini panel called from a panels display.
76 */
77 function panels_mini_panels_mini_content_type_render($subtype, $conf, $panel_args, &$contexts) {
78 static $viewing = array();
79 $mini = panels_mini_load($subtype);
80 if (!$mini) {
81 return FALSE;
82 }
83 if (!empty($viewing[$mini->name])) {
84 return FALSE;
85 }
86
87 // Load up any contexts we might be using.
88 $context = ctools_context_match_required_contexts($mini->requiredcontexts, $contexts);
89 $mini->context = $mini->display->context = ctools_context_load_contexts($mini, FALSE, $context);
90
91 if (empty($mini) || !empty($mini->disabled)) {
92 return;
93 }
94 $viewing[$mini->name] = TRUE;
95
96 $mini->display->args = $panel_args;
97 $mini->display->css_id = panels_mini_get_id($subtype);
98 $mini->display->owner = $mini;
99 // unique ID of this mini.
100 $mini->display->owner->id = $mini->name;
101
102 $block = new stdClass();
103 $block->module = 'panels_mini';
104 $block->delta = $subtype;
105 $block->content = panels_render_display($mini->display);
106 $block->title = $mini->display->get_title();
107
108 unset($viewing[$mini->name]);
109 return $block;
110 }
111
112 /**
113 * Edit form for the mini panel content type.
114 */
115 function panels_mini_panels_mini_content_type_edit_form($form, &$form_state) {
116 // Empty form to ensure we have the override title + context gadgets.
117 return $form;
118 }
119
120 /**
121 * Provide the administrative title of a mini panel.
122 */
123 function panels_mini_panels_mini_content_type_admin_title($subtype, $conf) {
124 $mini = panels_mini_load($subtype);
125 if (!$mini) {
126 return t('Deleted/missing mini panel @name', array('@name' => $subtype));
127 }
128
129 $title = filter_xss_admin($mini->admin_title);
130 if (empty($title)) {
131 $title = t('Untitled mini panel');
132 }
133 return $title;
134 }
135