comparison sites/all/modules/ctools/views_content/views_content.module @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file views_content.module
5 *
6 * Provides views as panels content, configurable by the administrator.
7 * Each view provided as panel content must be configured in advance,
8 * but once configured, building panels with views is a little bit simpler.
9 */
10
11 /**
12 * Implements hook_menu().
13 */
14 function views_content_menu() {
15 $items = array();
16
17 if (!module_exists('panels')) {
18 $items['admin/config/content-views'] = array(
19 'title' => 'Views panes',
20 'access arguments' => array('administer views content plugin'),
21 'page callback' => 'drupal_get_form',
22 'page arguments' => array('views_content_admin_page'),
23 'description' => 'Configure Views to be used as CTools content.',
24 'type' => MENU_NORMAL_ITEM,
25 );
26 }
27
28 return $items;
29 }
30
31 /**
32 * Implementation of hook_ctools_plugin_dierctory() to let the system know
33 * where our content_type plugins are.
34 */
35 function views_content_ctools_plugin_directory($owner, $plugin_type) {
36 if ($owner == 'ctools') {
37 return 'plugins/' . $plugin_type;
38 }
39 }
40
41 /**
42 * Don't show Views' blocks; we expose them already.
43 */
44 function views_ctools_block_info($module, $delta, &$info) {
45 if (strlen($delta) == 32) {
46 $hashes = variable_get('views_block_hashes', array());
47 if (!empty($hashes[$delta])) {
48 $delta = $hashes[$delta];
49 }
50 }
51
52 if (substr($delta, 0, 1) != '-') {
53 $info = NULL;
54 }
55 else {
56 $info['category'] = t('Views');
57 $info['icon'] = 'icon_views_block_legacy.png';
58 $info['path'] = drupal_get_path('module', 'views_content');
59 $info['edit form'] = 'views_content_exposed_form_pane_edit';
60 $info['add form'] = 'views_content_exposed_form_pane_edit';
61 $info['render callback'] = 'views_content_exposed_form_pane_render';
62 }
63 }
64
65 /**
66 * Add settings to the "exposed form in block" views.
67 */
68 function views_content_exposed_form_pane_edit($form, &$form_state) {
69 // This is a cheesy way to add defaults only to new versions of the block
70 // but leave older blocks without the setting alone. We can tell because
71 // all older content will have something set for override_title which is
72 // the only pre-existing setting.
73 if (!isset($form_state['conf']['inherit_path']) && !isset($form_state['conf']['override_title'])) {
74 $form_state['conf']['inherit_path'] = TRUE;
75 }
76
77 $form['inherit_path'] = array(
78 '#type' => 'checkbox',
79 '#title' => t('Inherit path'),
80 '#default_value' => !empty($form_state['conf']['inherit_path']),
81 );
82
83 return $form;
84 }
85
86 /**
87 * Store data for the exposed form in block settings page.
88 */
89 function views_content_exposed_form_pane_edit_submit($form, &$form_state) {
90 $form_state['conf']['inherit_path'] = $form_state['values']['inherit_path'];
91 }
92
93 /**
94 * Render function for 'special' view blocks.
95 *
96 * We took over the render for the special view blocks so that we could
97 * add options to it.
98 */
99 function views_content_exposed_form_pane_render($subtype, $conf, $panel_args, $contexts) {
100 $delta = str_replace('views-', '', $subtype);
101
102 if (strlen($delta) == 32) {
103 $hashes = variable_get('views_block_hashes', array());
104 if (!empty($hashes[$delta])) {
105 $delta = $hashes[$delta];
106 }
107 }
108
109 list($nothing, $type, $name, $display_id) = explode('-', $delta);
110 // Put the - back on. For views special blocks, the first character is always - but
111 // the explode killed it. Note that this code is mostly copied from views_block().
112 $type = '-' . $type;
113 if ($view = views_get_view($name)) {
114 if ($view->access($display_id)) {
115 if (!empty($conf['inherit_path'])) {
116 $view->override_path = $_GET['q'];
117 }
118
119 $view->set_display($display_id);
120 if (isset($view->display_handler)) {
121 $block = (object) $view->display_handler->view_special_blocks($type);
122 return $block;
123 }
124 }
125 $view->destroy();
126 }
127 }
128
129 /**
130 * Implements hook_views_api().
131 *
132 * This one is used as the base to reduce errors when updating.
133 */
134 function views_content_views_api() {
135 return array(
136 'api' => 2,
137 'path' => drupal_get_path('module', 'views_content') . '/plugins/views',
138 );
139 }
140
141 /**
142 * Page callback to provide the basic administration form.
143 */
144 function views_content_admin_page() {
145 $form = array();
146 views_content_admin_form($form);
147
148 return system_settings_form($form);
149 }
150
151 function views_content_admin_form(&$form) {
152 $form['ctools_content_all_views'] = array(
153 '#type' => 'checkbox',
154 '#title' => t('Make all views available as panes'),
155 '#description' => t("If checked, all views will be made available as content panes to be added to content types. If not checked, only Views that have a 'Content pane' display will be available as content panes. Uncheck this if you want to be able to more carefully control what view content is available to users using the panels layout UI."),
156 '#default_value' => variable_get('ctools_content_all_views', TRUE),
157 );
158 }
159
160 /**
161 * API function to get the view.
162 */
163 function views_content_context_get_view(&$context) {
164 if (empty($context->view) || get_class($context->view) == '__PHP_Incomplete_Class') {
165 $context->view = views_get_view($context->data['name']);
166 if ($context->view) {
167 $context->view->set_display($context->data['display']);
168 $context->view->set_arguments($context->data['args']);
169 }
170 }
171
172 return $context->view;
173 }
174
175 /**
176 * API function to get the view.
177 */
178 function views_content_context_get_output(&$context) {
179 if (empty($context->output)) {
180 $view = views_content_context_get_view($context);
181 $context->output = $view->execute_display($context->data['display']);
182 }
183
184 return $context->output;
185 }
186
187 /**
188 * Get the title to display for a views content display for pane or context.
189 */
190 function views_content_get_display_title($view, $display_id, $option = 'pane_title') {
191 $handler = $view->display[$display_id]->handler;
192 $title = $handler->get_option($option);
193 if (!$title) {
194 if ($handler->display->display_title == $handler->definition['title']) {
195 $title = t('View: @view', array('@view' => $view->get_human_name()));
196 }
197 else {
198 $title = t('View: @view: @display', array('@view' => $view->get_human_name(), '@display' => $handler->display->display_title));
199 }
200 }
201
202 return $title;
203 }
204
205 /**
206 * Get the proper label for a display.
207 *
208 * Views renamed the default to Master, but it can still have a conflicting
209 * display title. Fix that.
210 */
211 function views_content_get_display_label($view, $display_id) {
212 $title = $display_id == 'default' ? t('Master') : $view->display[$display_id]->display_title;
213 return $title;
214 }
215
216 /**
217 * Get the child plugin for a view context display.
218 *
219 * This can return both the context and relationship style. The
220 * $required parameter is used to distinguish if context is required
221 * or not, so we know whether we need it suitable as a pure context
222 * (i.e, no context required) or a relationship (i.e, context required).
223 */
224 function _views_content_get_context_from_display($view, $id, $parent, $required = TRUE) {
225 $title = views_content_get_display_title($view, $id, 'admin_title');
226
227 $description = $view->description;
228 $contexts = array();
229
230 $arguments = $view->display_handler->get_argument_input();
231 ctools_include('views');
232 foreach ($arguments as $argument) {
233 $argument['label'] = $argument['name'] ? $argument['name'] : '';
234 $contexts[] = ctools_views_get_argument_context($argument);
235 }
236
237 $pass = FALSE;
238 if ($required) {
239 // If context is required, make sure we have at least one optional
240 // or required context.
241 foreach ($contexts as $context) {
242 if (is_object($context)) {
243 $pass = TRUE;
244 break;
245 }
246 }
247
248 if (!$pass) {
249 return;
250 }
251 }
252 else {
253 // If context is not required, then having any required context
254 // causes a fail.
255 foreach ($contexts as $context) {
256 if (is_object($context) && get_class($context) == 'ctools_context_required') {
257 return;
258 }
259 }
260
261 // Since we know we don't want contexts, we an unset this now.
262 $contexts = array();
263 }
264
265 if ($required) {
266 $function = 'views_content_view_from_argument_context';
267 }
268 else {
269 $function = 'views_content_context_view_create';
270 }
271
272 return array(
273 'title' => $title,
274 'description' => filter_xss_admin($description),
275 'required context' => $contexts,
276 'keyword' => 'view',
277 'context' => $function,
278 'context name' => $view->name,
279 'name' => $parent . ':' . $view->name . '-' . $id,
280 'view name' => $view->name,
281 'view display id' => $id,
282 );
283 }