5 * Contains the tools to handle pluggable content that can be used by other
6 * applications such as Panels or Dashboard.
8 * See the context-content.html file in advanced help for documentation
13 * Provide defaults for a content type.
15 * Currently we check for automatically named callbacks to make life a little
16 * easier on the developer.
18 function ctools_content_process(&$plugin, $info) {
19 $function_base = $plugin['module'] .
'_' .
$plugin['name'] .
'_content_type_';
21 if (empty($plugin['render callback']) && function_exists($function_base .
'render')) {
22 $plugin['render callback'] = $function_base .
'render';
25 if (empty($plugin['admin title'])) {
26 if (function_exists($function_base .
'admin_title')) {
27 $plugin['admin title'] = $function_base .
'admin_title';
30 $plugin['admin title'] = $plugin['title'];
34 if (empty($plugin['admin info']) && function_exists($function_base .
'admin_info')) {
35 $plugin['admin info'] = $function_base .
'admin_info';
38 if (!isset($plugin['edit form']) && function_exists($function_base .
'edit_form')) {
39 $plugin['edit form'] = $function_base .
'edit_form';
42 if (!isset($plugin['add form']) && function_exists($function_base .
'add_form')) {
43 $plugin['add form'] = $function_base .
'add_form';
46 if (!isset($plugin['add form']) && function_exists($function_base .
'edit_form')) {
47 $plugin['add form'] = $function_base .
'edit_form';
50 // Another ease of use check:
51 if (!isset($plugin['content types'])) {
52 // If a subtype plugin exists, try to use it. Otherwise assume single.
53 if (function_exists($function_base .
'content_types')) {
54 $plugin['content types'] = $function_base .
'content_types';
58 'title' => $plugin['title'],
59 'description' => $plugin['description'],
60 'icon' => ctools_content_admin_icon($plugin),
61 'category' => $plugin['category'],
64 if (isset($plugin['required context'])) {
65 $type['required context'] = $plugin['required context'];
67 if (isset($plugin['top level'])) {
68 $type['top level'] = $plugin['top level'];
70 $plugin['content types'] = array($plugin['name'] => $type);
71 if (!isset($plugin['single'])) {
72 $plugin['single'] = TRUE
;
79 * Fetch metadata on a specific content_type plugin.
81 * @param $content type
82 * Name of a panel content type.
85 * An array with information about the requested panel content type.
87 function ctools_get_content_type($content_type) {
88 ctools_include('context');
89 ctools_include('plugins');
90 return ctools_get_plugins('ctools', 'content_types', $content_type);
94 * Fetch metadata for all content_type plugins.
97 * An array of arrays with information about all available panel content types.
99 function ctools_get_content_types() {
100 ctools_include('context');
101 ctools_include('plugins');
102 return ctools_get_plugins('ctools', 'content_types');
106 * Get all of the individual subtypes provided by a given content type. This
107 * would be all of the blocks for the block type, or all of the views for
111 * The content type to load.
114 * An array of all subtypes available.
116 function ctools_content_get_subtypes($type) {
117 static
$cache = array();
121 if (is_array($type)) {
125 $plugin = ctools_get_content_type($type);
128 if (empty($plugin) || empty($plugin['name'])) {
132 if (isset($cache[$plugin['name']])) {
133 return $cache[$plugin['name']];
136 if (isset($plugin['content types'])) {
137 $function = $plugin['content types'];
138 if (is_array($function)) {
139 $subtypes = $function;
141 else if (function_exists($function)) {
142 // Cast to array to prevent errors from non-array returns.
143 $subtypes = (array) $function($plugin);
147 // Walk through the subtypes and ensure minimal settings are
149 foreach ($subtypes as
$id => $subtype) {
150 // Use exact name since this is a modify by reference.
151 ctools_content_prepare_subtype($subtypes[$id], $plugin);
154 $cache[$plugin['name']] = $subtypes;
160 * Given a content type and a subtype id, return the information about that
164 * The content type being fetched.
166 * The id of the subtype being fetched.
169 * An array of information describing the content subtype.
171 function ctools_content_get_subtype($type, $subtype_id) {
173 if (is_array($type)) {
177 $plugin = ctools_get_content_type($type);
180 $function = ctools_plugin_get_function($plugin, 'content type');
182 $subtype = $function($subtype_id, $plugin);
185 $subtypes = ctools_content_get_subtypes($type);
186 if (isset($subtypes[$subtype_id])) {
187 $subtype = $subtypes[$subtype_id];
189 // If there's only 1 and we somehow have the wrong subtype ID, do not
190 // care. Return the proper subtype anyway.
191 if (empty($subtype) && !empty($plugin['single'])) {
192 $subtype = current($subtypes);
197 ctools_content_prepare_subtype($subtype, $plugin);
203 * Ensure minimal required settings on a content subtype exist.
205 function ctools_content_prepare_subtype(&$subtype, $plugin) {
206 foreach (array('path', 'js', 'css') as
$key) {
207 if (!isset($subtype[$key]) && isset($plugin[$key])) {
208 $subtype[$key] = $plugin[$key];
214 * Get the content from a given content type.
217 * The content type. May be the name or an already loaded content type plugin.
219 * The name of the subtype being rendered.
221 * The configuration for the content type.
223 * An array of replacement keywords that come from outside contexts.
225 * The arguments provided to the owner of the content type. Some content may
226 * wish to configure itself based on the arguments the panel or dashboard
229 * An array of context objects available for use.
230 * @param $incoming_content
231 * Any incoming content, if this display is a wrapper.
234 * The content as rendered by the plugin. This content should be an array
235 * with the following possible keys:
236 * - title: The safe to render title of the content.
237 * - content: The safe to render HTML content.
238 * - links: An array of links associated with the content suitable for
240 * - more: An optional 'more' link (destination only)
241 * - admin_links: Administrative links associated with the content, suitable
242 * for theme('links').
243 * - feeds: An array of feed icons or links associated with the content.
244 * Each member of the array is rendered HTML.
245 * - type: The content type.
246 * - subtype: The content subtype. These two may be used together as
247 * module-delta for block style rendering.
249 function ctools_content_render($type, $subtype, $conf, $keywords = array(), $args = array(), $context = array(), $incoming_content = '') {
250 if (is_array($type)) {
254 $plugin = ctools_get_content_type($type);
257 $subtype_info = ctools_content_get_subtype($plugin, $subtype);
259 $function = ctools_plugin_get_function($subtype_info, 'render callback');
261 $function = ctools_plugin_get_function($plugin, 'render callback');
265 $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
266 if ($pane_context === FALSE
) {
270 $content = $function($subtype, $conf, $args, $pane_context, $incoming_content);
272 // Set up some defaults and other massaging on the content before we hand
273 // it back to the caller.
274 if (!isset($content->type
)) {
275 $content->type
= $plugin['name'];
278 if (!isset($content->subtype
)) {
279 $content->subtype
= $subtype;
282 // Override the title if configured to
283 if (!empty($conf['override_title'])) {
284 // Give previous title as an available substitution here.
285 $keywords['%title'] = empty($content->title
) ?
'' : $content->title
;
286 $content->original_title
= $keywords['%title'];
287 $content->title
= $conf['override_title_text'];
290 if (!empty($content->title
)) {
291 // Perform substitutions
292 if (!empty($keywords) || !empty($context)) {
293 $content->title
= ctools_context_keyword_substitute($content->title
, $keywords, $context);
296 // Sterilize the title
297 $content->title
= filter_xss_admin($content->title
);
299 // If a link is specified, populate.
300 if (!empty($content->title_link
)) {
301 if (!is_array($content->title_link
)) {
302 $url = array('href' => $content->title_link
);
305 $url = $content->title_link
;
307 // set defaults so we don't bring up notices
308 $url += array('href' => '', 'attributes' => array(), 'query' => array(), 'fragment' => '', 'absolute' => NULL
, 'html' => TRUE
);
309 $content->title
= l($content->title
, $url['href'], $url);
318 * Determine if a content type can be edited or not.
320 * Some content types simply have their content and no options. This function
321 * lets a UI determine if it should display an edit link or not.
323 function ctools_content_editable($type, $subtype, $conf) {
324 if (empty($type['edit form']) && empty($subtype['edit form'])) {
328 if ($function = ctools_plugin_get_function($subtype, 'check editable')) {
329 return $function($type, $subtype, $conf);
336 * Get the administrative title from a given content type.
339 * The content type. May be the name or an already loaded content type object.
341 * The subtype being rendered.
343 * The configuration for the content type.
345 * An array of context objects available for use. These may be placeholders.
347 function ctools_content_admin_title($type, $subtype, $conf, $context = NULL
) {
348 if (is_array($type)) {
351 else if (is_string($type)) {
352 $plugin = ctools_get_content_type($type);
358 if ($function = ctools_plugin_get_function($plugin, 'admin title')) {
359 $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
360 if ($pane_context === FALSE
) {
361 if ($plugin['name'] == $subtype) {
362 return t('@type will not display due to missing context', array('@type' => $plugin['name']));
364 return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype));
367 return $function($subtype, $conf, $pane_context);
369 else if (isset($plugin['admin title'])) {
370 return $plugin['admin title'];
372 else if (isset($plugin['title'])) {
373 return $plugin['title'];
378 * Get the proper icon path to use, falling back to default icons if no icon exists.
381 * The loaded subtype info.
383 function ctools_content_admin_icon($subtype) {
386 if (isset($subtype['icon'])) {
387 $icon = $subtype['icon'];
388 if (!file_exists($icon)) {
389 $icon = $subtype['path'] .
'/' .
$icon;
393 if (empty($icon) || !file_exists($icon)) {
394 $icon = ctools_image_path('no-icon.png');
401 * Set up the default $conf for a new instance of a content type.
403 function ctools_content_get_defaults($plugin, $subtype) {
404 if (isset($plugin['defaults'])) {
405 $defaults = $plugin['defaults'];
407 else if (isset($subtype['defaults'])) {
408 $defaults = $subtype['defaults'];
410 if (isset($defaults)) {
411 if (is_string($defaults) && function_exists($defaults)) {
412 if ($return = $defaults($pane)) {
416 else if (is_array($defaults)) {
425 * Get the administrative title from a given content type.
428 * The content type. May be the name or an already loaded content type object.
430 * The subtype being rendered.
432 * The configuration for the content type.
434 * An array of context objects available for use. These may be placeholders.
436 function ctools_content_admin_info($type, $subtype, $conf, $context = NULL
) {
437 if (is_array($type)) {
441 $plugin = ctools_get_content_type($type);
444 if ($function = ctools_plugin_get_function($plugin, 'admin info')) {
445 $output = $function($subtype, $conf, $context);
447 if (empty($output) || !is_object($output)) {
448 $output = new
stdClass();
449 $output->title
= t('No info');
450 $output->content
= t('No info available.');
456 * Add the default FAPI elements to the content type configuration form
458 function ctools_content_configure_form_defaults($form, &$form_state) {
459 $plugin = $form_state['plugin'];
460 $subtype = $form_state['subtype'];
461 $contexts = isset($form_state['contexts']) ?
$form_state['contexts'] : NULL
;
462 $conf = $form_state['conf'];
465 if (!empty($subtype['required context']) && is_array($contexts)) {
466 $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ?
$conf['context'] : array());
470 ctools_include('dependent');
472 // Unless we're not allowed to override the title on this content type, add this
473 // gadget to all panes.
474 if (empty($plugin['no title override']) && empty($subtype['no title override'])) {
475 $form['aligner_start'] = array(
476 '#markup' => '<div class="option-text-aligner clearfix">',
478 $form['override_title'] = array(
479 '#type' => 'checkbox',
480 '#default_value' => isset($conf['override_title']) ?
$conf['override_title'] : '',
481 '#title' => t('Override title'),
482 '#id' => 'override-title-checkbox',
484 $form['override_title_text'] = array(
485 '#type' => 'textfield',
486 '#default_value' => isset($conf['override_title_text']) ?
$conf['override_title_text'] : '',
488 '#id' => 'override-title-textfield',
489 '#process' => array('ctools_dependent_process'),
490 '#dependency' => array('override-title-checkbox' => array(1)),
491 '#dependency_type' => 'disable',
493 $form['aligner_stop'] = array(
494 '#markup' => '</div>',
496 if (is_array($contexts)) {
497 $form['override_title_markup'] = array(
498 '#prefix' => '<div class="description">',
499 '#suffix' => '</div>',
500 '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'),
507 // '#submit' is already set up due to the wizard.
508 $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
514 * Submit handler to store context/title override info.
516 function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
517 if (isset($form_state['values']['context'])) {
518 $form_state['conf']['context'] = $form_state['values']['context'];
520 if (isset($form_state['values']['override_title'])) {
521 $form_state['conf']['override_title'] = $form_state['values']['override_title'];
522 $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
527 * Get the config form.
529 * The $form_info and $form_state need to be preconfigured with data you'll need
530 * such as whether or not you're using ajax, or the modal. $form_info will need
531 * your next/submit callbacks so that you can cache your data appropriately.
534 * If this function returns false, no form exists.
536 function ctools_content_form($op, $form_info, &$form_state, $plugin, $subtype_name, $subtype, &$conf, $step = NULL
) {
537 $form_state += array(
539 'subtype' => $subtype,
540 'subtype_name' => $subtype_name,
546 'id' => 'ctools_content_form',
550 // Turn the forms defined in the plugin into the format the wizard needs.
552 if (!empty($subtype['add form'])) {
553 _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
555 else if (!empty($plugin['add form'])) {
556 _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
560 if (empty($form_info['order'])) {
561 // Use the edit form for the add form if add form was completely left off.
562 if (!empty($subtype['edit form'])) {
563 _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
565 else if (!empty($plugin['edit form'])) {
566 _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
570 if (empty($form_info['order'])) {
574 ctools_include('wizard');
575 return ctools_wizard_multistep_form($form_info, $step, $form_state);
579 function _ctools_content_create_form_info(&$form_info, $info, $plugin, $subtype, $op) {
580 if (is_string($info)) {
581 if (empty($subtype['title'])) {
582 $title = t('Configure');
584 else if ($op == 'add') {
585 $title = t('Configure new !subtype_title', array('!subtype_title' => $subtype['title']));
588 $title = t('Configure !subtype_title', array('!subtype_title' => $subtype['title']));
590 $form_info['order'] = array('form' => $title);
591 $form_info['forms'] = array(
595 'wrapper' => 'ctools_content_configure_form_defaults',
599 else if (is_array($info)) {
600 $form_info['order'] = array();
601 $form_info['forms'] = array();
605 foreach ($info as
$form_id => $title) {
606 // @todo -- docs say %title can be used to sub for the admin title.
607 $step = $base .
++$count;
608 if (empty($wrapper)) {
612 if (is_array($title)) {
613 if (!empty($title['default'])) {
616 $title = $title['title'];
619 $form_info['order'][$step] = $title;
620 $form_info['forms'][$step] = array(
622 'form id' => $form_id,
626 $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
632 * Get an array of all available content types that can be fed into the
633 * display editor for the add content list.
636 * If a context is provided, content that requires that context can apepar.
637 * @param $has_content
638 * Whether or not the display will have incoming content
639 * @param $allowed_types
640 * An array of allowed content types (pane types) keyed by content_type . '-' . sub_type
641 * @param $default_types
642 * A default allowed/denied status for content that isn't known about
644 function ctools_content_get_available_types($contexts = NULL
, $has_content = FALSE
, $allowed_types = NULL
, $default_types = NULL
) {
645 $plugins = ctools_get_content_types();
646 $available = array();
648 foreach ($plugins as
$id => $plugin) {
649 foreach (ctools_content_get_subtypes($plugin) as
$subtype_id => $subtype) {
650 // exclude items that require content if we're saying we don't
652 if (!empty($subtype['requires content']) && !$has_content) {
656 // Check to see if the content type can be used in this context.
657 if (!empty($subtype['required context'])) {
658 if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
663 // Check to see if the passed-in allowed types allows this content.
664 if ($allowed_types) {
665 $key = $id .
'-' .
$subtype_id;
666 if (!isset($allowed_types[$key])) {
667 $allowed_types[$key] = isset($default_types[$id]) ?
$default_types[$id] : $default_types['other'];
669 if (!$allowed_types[$key]) {
674 // If we made it through all the tests, then we can use this content.
675 $available[$id][$subtype_id] = $subtype;
682 * Get an array of all content types that can be fed into the
683 * display editor for the add content list, regardless of
687 function ctools_content_get_all_types() {
688 $plugins = ctools_get_content_types();
689 $available = array();
691 foreach ($plugins as
$id => $plugin) {
692 foreach (ctools_content_get_subtypes($plugin) as
$subtype_id => $subtype) {
693 // If we made it through all the tests, then we can use this content.
694 $available[$id][$subtype_id] = $subtype;
701 * Select the context to be used for a piece of content, based upon config.
706 * The subtype of the content.
708 * The configuration array that should contain the context.
710 * A keyed array of available contexts.
713 * The matching contexts or NULL if none or necessary, or FALSE if
714 * requirements can't be met.
716 function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
717 // Identify which of our possible contexts apply.
718 if (empty($subtype)) {
722 $subtype_info = ctools_content_get_subtype($plugin, $subtype);
723 if (empty($subtype_info)) {
727 if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
731 // If the content requires a context, fetch it; if no context is returned,
732 // do not display the pane.
733 if (empty($subtype_info['required context'])) {
737 // Deal with dynamic required contexts not getting updated in the panes.
738 // For example, Views let you dynamically change context info. While
739 // we cannot be perfect, one thing we can do is if no context at all
740 // was asked for, and then was later added but none is selected, make
741 // a best guess as to what context should be used. THis is right more
743 if (is_array($subtype_info['required context'])) {
744 if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
745 foreach($subtype_info['required context'] as
$index => $required) {
746 if (!isset($conf['context'][$index])) {
747 $filtered = ctools_context_filter($contexts, $required);
749 $keys = array_keys($filtered);
750 $conf['context'][$index] = array_shift($keys);
757 if (empty($conf['context'])) {
758 $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
760 $keys = array_keys($filtered);
761 $conf['context'] = array_shift($keys);
766 if (empty($conf['context'])) {
770 $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);