content.inc

  1. cis7 sites/all/modules/ulmus/ctools/includes/content.inc
  2. cle7 sites/all/modules/ulmus/ctools/includes/content.inc
  3. ecd7 sites/all/modules/ulmus/ctools/includes/content.inc
  4. elmsmedia7 sites/all/modules/ulmus/ctools/includes/content.inc
  5. harmony7 sites/all/modules/ulmus/ctools/includes/content.inc
  6. icor7 sites/all/modules/ulmus/ctools/includes/content.inc
  7. meedjum_blog7 sites/all/modules/ulmus/ctools/includes/content.inc
  8. mooc7 sites/all/modules/ulmus/ctools/includes/content.inc

Contains the tools to handle pluggable content that can be used by other applications such as Panels or Dashboard.

See the context-content.html file in advanced help for documentation of this tool.

Functions

Namesort descending Description
ctools_content_admin_icon Get the proper icon path to use, falling back to default icons if no icon exists.
ctools_content_admin_info Get the administrative title from a given content type.
ctools_content_admin_title Get the administrative title from a given content type.
ctools_content_configure_form_defaults Add the default FAPI elements to the content type configuration form
ctools_content_configure_form_defaults_submit Submit handler to store context/title override info.
ctools_content_editable Determine if a content type can be edited or not.
ctools_content_form Get the config form.
ctools_content_get_all_types Get an array of all content types that can be fed into the display editor for the add content list, regardless of availability.
ctools_content_get_available_types Get an array of all available content types that can be fed into the display editor for the add content list.
ctools_content_get_defaults Set up the default $conf for a new instance of a content type.
ctools_content_get_subtype Given a content type and a subtype id, return the information about that content subtype.
ctools_content_get_subtypes Get all of the individual subtypes provided by a given content type. This would be all of the blocks for the block type, or all of the views for the view type.
ctools_content_prepare_subtype Ensure minimal required settings on a content subtype exist.
ctools_content_process Provide defaults for a content type.
ctools_content_render Get the content from a given content type.
ctools_content_select_context Select the context to be used for a piece of content, based upon config.
ctools_get_addressable_content Fetch a piece of content from the addressable content system.
ctools_get_content_type Fetch metadata on a specific content_type plugin.
ctools_get_content_types Fetch metadata for all content_type plugins.
_ctools_content_create_form_info

File

sites/all/modules/ulmus/ctools/includes/content.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the tools to handle pluggable content that can be used by other
  5. * applications such as Panels or Dashboard.
  6. *
  7. * See the context-content.html file in advanced help for documentation
  8. * of this tool.
  9. */
  10. /**
  11. * Provide defaults for a content type.
  12. *
  13. * Currently we check for automatically named callbacks to make life a little
  14. * easier on the developer.
  15. */
  16. function ctools_content_process(&$plugin, $info) {
  17. $function_base = $plugin['module'] . '_' . $plugin['name'] . '_content_type_';
  18. if (empty($plugin['render callback']) && function_exists($function_base . 'render')) {
  19. $plugin['render callback'] = $function_base . 'render';
  20. }
  21. if (empty($plugin['admin title'])) {
  22. if (function_exists($function_base . 'admin_title')) {
  23. $plugin['admin title'] = $function_base . 'admin_title';
  24. }
  25. else {
  26. $plugin['admin title'] = $plugin['title'];
  27. }
  28. }
  29. if (empty($plugin['admin info']) && function_exists($function_base . 'admin_info')) {
  30. $plugin['admin info'] = $function_base . 'admin_info';
  31. }
  32. if (!isset($plugin['edit form']) && function_exists($function_base . 'edit_form')) {
  33. $plugin['edit form'] = $function_base . 'edit_form';
  34. }
  35. if (!isset($plugin['add form']) && function_exists($function_base . 'add_form')) {
  36. $plugin['add form'] = $function_base . 'add_form';
  37. }
  38. if (!isset($plugin['add form']) && function_exists($function_base . 'edit_form')) {
  39. $plugin['add form'] = $function_base . 'edit_form';
  40. }
  41. if (!isset($plugin['description'])) {
  42. $plugin['description'] = '';
  43. }
  44. if (!isset($plugin['icon'])) {
  45. $plugin['icon'] = ctools_content_admin_icon($plugin);
  46. }
  47. // Another ease of use check:
  48. if (!isset($plugin['content types'])) {
  49. // If a subtype plugin exists, try to use it. Otherwise assume single.
  50. if (function_exists($function_base . 'content_types')) {
  51. $plugin['content types'] = $function_base . 'content_types';
  52. }
  53. else {
  54. $type = array(
  55. 'title' => $plugin['title'],
  56. 'description' => $plugin['description'],
  57. 'icon' => ctools_content_admin_icon($plugin),
  58. 'category' => $plugin['category'],
  59. );
  60. if (isset($plugin['required context'])) {
  61. $type['required context'] = $plugin['required context'];
  62. }
  63. if (isset($plugin['top level'])) {
  64. $type['top level'] = $plugin['top level'];
  65. }
  66. $plugin['content types'] = array($plugin['name'] => $type);
  67. if (!isset($plugin['single'])) {
  68. $plugin['single'] = TRUE;
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Fetch metadata on a specific content_type plugin.
  75. *
  76. * @param $content type
  77. * Name of a panel content type.
  78. *
  79. * @return
  80. * An array with information about the requested panel content type.
  81. */
  82. function ctools_get_content_type($content_type) {
  83. ctools_include('context');
  84. ctools_include('plugins');
  85. return ctools_get_plugins('ctools', 'content_types', $content_type);
  86. }
  87. /**
  88. * Fetch metadata for all content_type plugins.
  89. *
  90. * @return
  91. * An array of arrays with information about all available panel content types.
  92. */
  93. function ctools_get_content_types() {
  94. ctools_include('context');
  95. ctools_include('plugins');
  96. return ctools_get_plugins('ctools', 'content_types');
  97. }
  98. /**
  99. * Get all of the individual subtypes provided by a given content type. This
  100. * would be all of the blocks for the block type, or all of the views for
  101. * the view type.
  102. *
  103. * @param $type
  104. * The content type to load.
  105. *
  106. * @return
  107. * An array of all subtypes available.
  108. */
  109. function ctools_content_get_subtypes($type) {
  110. static $cache = array();
  111. $subtypes = array();
  112. if (is_array($type)) {
  113. $plugin = $type;
  114. }
  115. else {
  116. $plugin = ctools_get_content_type($type);
  117. }
  118. if (empty($plugin) || empty($plugin['name'])) {
  119. return;
  120. }
  121. if (isset($cache[$plugin['name']])) {
  122. return $cache[$plugin['name']];
  123. }
  124. if (isset($plugin['content types'])) {
  125. $function = $plugin['content types'];
  126. if (is_array($function)) {
  127. $subtypes = $function;
  128. }
  129. else if (function_exists($function)) {
  130. // Cast to array to prevent errors from non-array returns.
  131. $subtypes = (array) $function($plugin);
  132. }
  133. }
  134. // Walk through the subtypes and ensure minimal settings are
  135. // retained.
  136. foreach ($subtypes as $id => $subtype) {
  137. // Use exact name since this is a modify by reference.
  138. ctools_content_prepare_subtype($subtypes[$id], $plugin);
  139. }
  140. $cache[$plugin['name']] = $subtypes;
  141. return $subtypes;
  142. }
  143. /**
  144. * Given a content type and a subtype id, return the information about that
  145. * content subtype.
  146. *
  147. * @param $type
  148. * The content type being fetched.
  149. * @param $subtype_id
  150. * The id of the subtype being fetched.
  151. *
  152. * @return
  153. * An array of information describing the content subtype.
  154. */
  155. function ctools_content_get_subtype($type, $subtype_id) {
  156. $subtype = array();
  157. if (is_array($type)) {
  158. $plugin = $type;
  159. }
  160. else {
  161. $plugin = ctools_get_content_type($type);
  162. }
  163. $function = ctools_plugin_get_function($plugin, 'content type');
  164. if ($function) {
  165. $subtype = $function($subtype_id, $plugin);
  166. }
  167. else {
  168. $subtypes = ctools_content_get_subtypes($type);
  169. if (isset($subtypes[$subtype_id])) {
  170. $subtype = $subtypes[$subtype_id];
  171. }
  172. // If there's only 1 and we somehow have the wrong subtype ID, do not
  173. // care. Return the proper subtype anyway.
  174. if (empty($subtype) && !empty($plugin['single'])) {
  175. $subtype = current($subtypes);
  176. }
  177. }
  178. if ($subtype) {
  179. ctools_content_prepare_subtype($subtype, $plugin);
  180. }
  181. return $subtype;
  182. }
  183. /**
  184. * Ensure minimal required settings on a content subtype exist.
  185. */
  186. function ctools_content_prepare_subtype(&$subtype, $plugin) {
  187. foreach (array('path', 'js', 'css') as $key) {
  188. if (!isset($subtype[$key]) && isset($plugin[$key])) {
  189. $subtype[$key] = $plugin[$key];
  190. }
  191. }
  192. drupal_alter('ctools_content_subtype', $subtype, $plugin);
  193. }
  194. /**
  195. * Get the content from a given content type.
  196. *
  197. * @param $type
  198. * The content type. May be the name or an already loaded content type plugin.
  199. * @param $subtype
  200. * The name of the subtype being rendered.
  201. * @param $conf
  202. * The configuration for the content type.
  203. * @param $keywords
  204. * An array of replacement keywords that come from outside contexts.
  205. * @param $args
  206. * The arguments provided to the owner of the content type. Some content may
  207. * wish to configure itself based on the arguments the panel or dashboard
  208. * received.
  209. * @param $context
  210. * An array of context objects available for use.
  211. * @param $incoming_content
  212. * Any incoming content, if this display is a wrapper.
  213. *
  214. * @return
  215. * The content as rendered by the plugin. This content should be an array
  216. * with the following possible keys:
  217. * - title: The safe to render title of the content.
  218. * - content: The safe to render HTML content.
  219. * - links: An array of links associated with the content suitable for
  220. * theme('links').
  221. * - more: An optional 'more' link (destination only)
  222. * - admin_links: Administrative links associated with the content, suitable
  223. * for theme('links').
  224. * - feeds: An array of feed icons or links associated with the content.
  225. * Each member of the array is rendered HTML.
  226. * - type: The content type.
  227. * - subtype: The content subtype. These two may be used together as
  228. * module-delta for block style rendering.
  229. */
  230. function ctools_content_render($type, $subtype, $conf, $keywords = array(), $args = array(), $context = array(), $incoming_content = '') {
  231. if (is_array($type)) {
  232. $plugin = $type;
  233. }
  234. else {
  235. $plugin = ctools_get_content_type($type);
  236. }
  237. $subtype_info = ctools_content_get_subtype($plugin, $subtype);
  238. $function = ctools_plugin_get_function($subtype_info, 'render callback');
  239. if (!$function) {
  240. $function = ctools_plugin_get_function($plugin, 'render callback');
  241. }
  242. if ($function) {
  243. $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
  244. if ($pane_context === FALSE) {
  245. return;
  246. }
  247. $content = $function($subtype, $conf, $args, $pane_context, $incoming_content);
  248. if (empty($content)) {
  249. return;
  250. }
  251. // Set up some defaults and other massaging on the content before we hand
  252. // it back to the caller.
  253. if (!isset($content->type)) {
  254. $content->type = $plugin['name'];
  255. }
  256. if (!isset($content->subtype)) {
  257. $content->subtype = $subtype;
  258. }
  259. // Override the title if configured to
  260. if (!empty($conf['override_title'])) {
  261. // Give previous title as an available substitution here.
  262. $keywords['%title'] = empty($content->title) ? '' : $content->title;
  263. $content->original_title = $keywords['%title'];
  264. $content->title = $conf['override_title_text'];
  265. }
  266. if (!empty($content->title)) {
  267. // Perform substitutions
  268. if (!empty($keywords) || !empty($context)) {
  269. $content->title = ctools_context_keyword_substitute($content->title, $keywords, $context);
  270. }
  271. // Sterilize the title
  272. $content->title = filter_xss_admin($content->title);
  273. // If a link is specified, populate.
  274. if (!empty($content->title_link)) {
  275. if (!is_array($content->title_link)) {
  276. $url = array('href' => $content->title_link);
  277. }
  278. else {
  279. $url = $content->title_link;
  280. }
  281. // set defaults so we don't bring up notices
  282. $url += array('href' => '', 'attributes' => array(), 'query' => array(), 'fragment' => '', 'absolute' => NULL, 'html' => TRUE);
  283. $content->title = l($content->title, $url['href'], $url);
  284. }
  285. }
  286. return $content;
  287. }
  288. }
  289. /**
  290. * Determine if a content type can be edited or not.
  291. *
  292. * Some content types simply have their content and no options. This function
  293. * lets a UI determine if it should display an edit link or not.
  294. */
  295. function ctools_content_editable($type, $subtype, $conf) {
  296. if (empty($type['edit form']) && empty($subtype['edit form'])) {
  297. return FALSE;
  298. }
  299. if ($function = ctools_plugin_get_function($subtype, 'check editable')) {
  300. return $function($type, $subtype, $conf);
  301. }
  302. return TRUE;
  303. }
  304. /**
  305. * Get the administrative title from a given content type.
  306. *
  307. * @param $type
  308. * The content type. May be the name or an already loaded content type object.
  309. * @param $subtype
  310. * The subtype being rendered.
  311. * @param $conf
  312. * The configuration for the content type.
  313. * @param $context
  314. * An array of context objects available for use. These may be placeholders.
  315. */
  316. function ctools_content_admin_title($type, $subtype, $conf, $context = NULL) {
  317. if (is_array($type)) {
  318. $plugin = $type;
  319. }
  320. else if (is_string($type)) {
  321. $plugin = ctools_get_content_type($type);
  322. }
  323. else {
  324. return;
  325. }
  326. if ($function = ctools_plugin_get_function($plugin, 'admin title')) {
  327. $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
  328. if ($pane_context === FALSE) {
  329. if ($plugin['name'] == $subtype) {
  330. return t('@type will not display due to missing context', array('@type' => $plugin['name']));
  331. }
  332. return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype));
  333. }
  334. return $function($subtype, $conf, $pane_context);
  335. }
  336. else if (isset($plugin['admin title'])) {
  337. return $plugin['admin title'];
  338. }
  339. else if (isset($plugin['title'])) {
  340. return $plugin['title'];
  341. }
  342. }
  343. /**
  344. * Get the proper icon path to use, falling back to default icons if no icon exists.
  345. *
  346. * $subtype
  347. * The loaded subtype info.
  348. */
  349. function ctools_content_admin_icon($subtype) {
  350. $icon = '';
  351. if (isset($subtype['icon'])) {
  352. $icon = $subtype['icon'];
  353. if (!file_exists($icon)) {
  354. $icon = $subtype['path'] . '/' . $icon;
  355. }
  356. }
  357. if (empty($icon) || !file_exists($icon)) {
  358. $icon = ctools_image_path('no-icon.png');
  359. }
  360. return $icon;
  361. }
  362. /**
  363. * Set up the default $conf for a new instance of a content type.
  364. */
  365. function ctools_content_get_defaults($plugin, $subtype) {
  366. if (isset($plugin['defaults'])) {
  367. $defaults = $plugin['defaults'];
  368. }
  369. else if (isset($subtype['defaults'])) {
  370. $defaults = $subtype['defaults'];
  371. }
  372. if (isset($defaults)) {
  373. if (is_string($defaults) && function_exists($defaults)) {
  374. if ($return = $defaults($pane)) {
  375. return $return;
  376. }
  377. }
  378. else if (is_array($defaults)) {
  379. return $defaults;
  380. }
  381. }
  382. return array();
  383. }
  384. /**
  385. * Get the administrative title from a given content type.
  386. *
  387. * @param $type
  388. * The content type. May be the name or an already loaded content type object.
  389. * @param $subtype
  390. * The subtype being rendered.
  391. * @param $conf
  392. * The configuration for the content type.
  393. * @param $context
  394. * An array of context objects available for use. These may be placeholders.
  395. */
  396. function ctools_content_admin_info($type, $subtype, $conf, $context = NULL) {
  397. if (is_array($type)) {
  398. $plugin = $type;
  399. }
  400. else {
  401. $plugin = ctools_get_content_type($type);
  402. }
  403. if ($function = ctools_plugin_get_function($plugin, 'admin info')) {
  404. $output = $function($subtype, $conf, $context);
  405. }
  406. if (empty($output) || !is_object($output)) {
  407. $output = new stdClass();
  408. // replace the _ with " " for a better output
  409. $subtype = check_plain(str_replace("_", " ", $subtype));
  410. $output->title = $subtype;
  411. $output->content = t('No info available.');
  412. }
  413. return $output;
  414. }
  415. /**
  416. * Add the default FAPI elements to the content type configuration form
  417. */
  418. function ctools_content_configure_form_defaults($form, &$form_state) {
  419. $plugin = $form_state['plugin'];
  420. $subtype = $form_state['subtype'];
  421. $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : NULL;
  422. $conf = $form_state['conf'];
  423. $add_submit = FALSE;
  424. if (!empty($subtype['required context']) && is_array($contexts)) {
  425. $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ? $conf['context'] : array());
  426. $add_submit = TRUE;
  427. }
  428. ctools_include('dependent');
  429. // Unless we're not allowed to override the title on this content type, add this
  430. // gadget to all panes.
  431. if (empty($plugin['no title override']) && empty($subtype['no title override'])) {
  432. $form['aligner_start'] = array(
  433. '#markup' => '<div class="option-text-aligner clearfix">',
  434. );
  435. $form['override_title'] = array(
  436. '#type' => 'checkbox',
  437. '#default_value' => isset($conf['override_title']) ? $conf['override_title'] : '',
  438. '#title' => t('Override title'),
  439. '#id' => 'override-title-checkbox',
  440. );
  441. $form['override_title_text'] = array(
  442. '#type' => 'textfield',
  443. '#default_value' => isset($conf['override_title_text']) ? $conf['override_title_text'] : '',
  444. '#size' => 35,
  445. '#id' => 'override-title-textfield',
  446. '#dependency' => array('override-title-checkbox' => array(1)),
  447. '#dependency_type' => 'disable',
  448. );
  449. $form['aligner_stop'] = array(
  450. '#markup' => '</div>',
  451. );
  452. if (is_array($contexts)) {
  453. $form['override_title_markup'] = array(
  454. '#prefix' => '<div class="description">',
  455. '#suffix' => '</div>',
  456. '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'),
  457. );
  458. }
  459. $add_submit = TRUE;
  460. }
  461. if ($add_submit) {
  462. // '#submit' is already set up due to the wizard.
  463. $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
  464. }
  465. return $form;
  466. }
  467. /**
  468. * Submit handler to store context/title override info.
  469. */
  470. function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
  471. if (isset($form_state['values']['context'])) {
  472. $form_state['conf']['context'] = $form_state['values']['context'];
  473. }
  474. if (isset($form_state['values']['override_title'])) {
  475. $form_state['conf']['override_title'] = $form_state['values']['override_title'];
  476. $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
  477. }
  478. }
  479. /**
  480. * Get the config form.
  481. *
  482. * The $form_info and $form_state need to be preconfigured with data you'll need
  483. * such as whether or not you're using ajax, or the modal. $form_info will need
  484. * your next/submit callbacks so that you can cache your data appropriately.
  485. *
  486. * @return
  487. * If this function returns false, no form exists.
  488. */
  489. function ctools_content_form($op, $form_info, &$form_state, $plugin, $subtype_name, $subtype, &$conf, $step = NULL) {
  490. $form_state += array(
  491. 'plugin' => $plugin,
  492. 'subtype' => $subtype,
  493. 'subtype_name' => $subtype_name,
  494. 'conf' => &$conf,
  495. 'op' => $op,
  496. );
  497. $form_info += array(
  498. 'id' => 'ctools_content_form',
  499. 'show back' => TRUE,
  500. );
  501. // Turn the forms defined in the plugin into the format the wizard needs.
  502. if ($op == 'add') {
  503. if (!empty($subtype['add form'])) {
  504. _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
  505. }
  506. else if (!empty($plugin['add form'])) {
  507. _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
  508. }
  509. }
  510. if (empty($form_info['order'])) {
  511. // Use the edit form for the add form if add form was completely left off.
  512. if (!empty($subtype['edit form'])) {
  513. _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
  514. }
  515. else if (!empty($plugin['edit form'])) {
  516. _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
  517. }
  518. }
  519. if (empty($form_info['order'])) {
  520. return FALSE;
  521. }
  522. ctools_include('wizard');
  523. return ctools_wizard_multistep_form($form_info, $step, $form_state);
  524. }
  525. function _ctools_content_create_form_info(&$form_info, $info, $plugin, $subtype, $op) {
  526. if (is_string($info)) {
  527. if (empty($subtype['title'])) {
  528. $title = t('Configure');
  529. }
  530. else if ($op == 'add') {
  531. $title = t('Configure new !subtype_title', array('!subtype_title' => $subtype['title']));
  532. }
  533. else {
  534. $title = t('Configure !subtype_title', array('!subtype_title' => $subtype['title']));
  535. }
  536. $form_info['order'] = array('form' => $title);
  537. $form_info['forms'] = array(
  538. 'form' => array(
  539. 'title' => $title,
  540. 'form id' => $info,
  541. 'wrapper' => 'ctools_content_configure_form_defaults',
  542. ),
  543. );
  544. }
  545. else if (is_array($info)) {
  546. $form_info['order'] = array();
  547. $form_info['forms'] = array();
  548. $count = 0;
  549. $base = 'step';
  550. $wrapper = NULL;
  551. foreach ($info as $form_id => $title) {
  552. // @todo -- docs say %title can be used to sub for the admin title.
  553. $step = $base . ++$count;
  554. if (empty($wrapper)) {
  555. $wrapper = $step;
  556. }
  557. if (is_array($title)) {
  558. if (!empty($title['default'])) {
  559. $wrapper = $step;
  560. }
  561. $title = $title['title'];
  562. }
  563. $form_info['order'][$step] = $title;
  564. $form_info['forms'][$step] = array(
  565. 'title' => $title,
  566. 'form id' => $form_id,
  567. );
  568. }
  569. if ($wrapper) {
  570. $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
  571. }
  572. }
  573. }
  574. /**
  575. * Get an array of all available content types that can be fed into the
  576. * display editor for the add content list.
  577. *
  578. * @param $context
  579. * If a context is provided, content that requires that context can apepar.
  580. * @param $has_content
  581. * Whether or not the display will have incoming content
  582. * @param $allowed_types
  583. * An array of allowed content types (pane types) keyed by content_type . '-' . sub_type
  584. * @param $default_types
  585. * A default allowed/denied status for content that isn't known about
  586. */
  587. function ctools_content_get_available_types($contexts = NULL, $has_content = FALSE, $allowed_types = NULL, $default_types = NULL) {
  588. $plugins = ctools_get_content_types();
  589. $available = array();
  590. foreach ($plugins as $id => $plugin) {
  591. foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
  592. // exclude items that require content if we're saying we don't
  593. // provide it.
  594. if (!empty($subtype['requires content']) && !$has_content) {
  595. continue;
  596. }
  597. // Check to see if the content type can be used in this context.
  598. if (!empty($subtype['required context'])) {
  599. if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
  600. continue;
  601. }
  602. }
  603. // Check to see if the passed-in allowed types allows this content.
  604. if ($allowed_types) {
  605. $key = $id . '-' . $subtype_id;
  606. if (!isset($allowed_types[$key])) {
  607. $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
  608. }
  609. if (!$allowed_types[$key]) {
  610. continue;
  611. }
  612. }
  613. // Check if the content type provides an access callback.
  614. if (isset($subtype['create content access']) && function_exists($subtype['create content access']) && !$subtype['create content access']($plugin, $subtype)) {
  615. continue;
  616. }
  617. // If we made it through all the tests, then we can use this content.
  618. $available[$id][$subtype_id] = $subtype;
  619. }
  620. }
  621. return $available;
  622. }
  623. /**
  624. * Get an array of all content types that can be fed into the
  625. * display editor for the add content list, regardless of
  626. * availability.
  627. *
  628. */
  629. function ctools_content_get_all_types() {
  630. $plugins = ctools_get_content_types();
  631. $available = array();
  632. foreach ($plugins as $id => $plugin) {
  633. foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
  634. // If we made it through all the tests, then we can use this content.
  635. $available[$id][$subtype_id] = $subtype;
  636. }
  637. }
  638. return $available;
  639. }
  640. /**
  641. * Select the context to be used for a piece of content, based upon config.
  642. *
  643. * @param $plugin
  644. * The content plugin
  645. * @param $subtype
  646. * The subtype of the content.
  647. * @param $conf
  648. * The configuration array that should contain the context.
  649. * @param $contexts
  650. * A keyed array of available contexts.
  651. *
  652. * @return
  653. * The matching contexts or NULL if none or necessary, or FALSE if
  654. * requirements can't be met.
  655. */
  656. function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
  657. // Identify which of our possible contexts apply.
  658. if (empty($subtype)) {
  659. return;
  660. }
  661. $subtype_info = ctools_content_get_subtype($plugin, $subtype);
  662. if (empty($subtype_info)) {
  663. return;
  664. }
  665. if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
  666. return $contexts;
  667. }
  668. // If the content requires a context, fetch it; if no context is returned,
  669. // do not display the pane.
  670. if (empty($subtype_info['required context'])) {
  671. return;
  672. }
  673. // Deal with dynamic required contexts not getting updated in the panes.
  674. // For example, Views let you dynamically change context info. While
  675. // we cannot be perfect, one thing we can do is if no context at all
  676. // was asked for, and then was later added but none is selected, make
  677. // a best guess as to what context should be used. THis is right more
  678. // than it's wrong.
  679. if (is_array($subtype_info['required context'])) {
  680. if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
  681. foreach ($subtype_info['required context'] as $index => $required) {
  682. if (!isset($conf['context'][$index])) {
  683. $filtered = ctools_context_filter($contexts, $required);
  684. if ($filtered) {
  685. $keys = array_keys($filtered);
  686. $conf['context'][$index] = array_shift($keys);
  687. }
  688. }
  689. }
  690. }
  691. }
  692. else {
  693. if (empty($conf['context'])) {
  694. $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
  695. if ($filtered) {
  696. $keys = array_keys($filtered);
  697. $conf['context'] = array_shift($keys);
  698. }
  699. }
  700. }
  701. if (empty($conf['context'])) {
  702. return;
  703. }
  704. $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
  705. return $context;
  706. }
  707. /**
  708. * Fetch a piece of content from the addressable content system.
  709. *
  710. * @param $address
  711. * A string or an array representing the address of the content.
  712. * @param $type
  713. * The type of content to return. The type is dependent on what
  714. * the content actually is. The default, 'content' means a simple
  715. * string representing the content. However, richer systems may
  716. * offer more options. For example, Panels might allow the
  717. * fetching of 'display' and 'pane' objects. Page Manager
  718. * might allow for the fetching of 'task_handler' objects
  719. * (AKA variants).
  720. */
  721. function ctools_get_addressable_content($address, $type = 'content') {
  722. if (!is_array($address)) {
  723. $address = explode('::', $address);
  724. }
  725. if (!$address) {
  726. return;
  727. }
  728. // This removes the module from the address so the
  729. // implementor is not responsible for that part.
  730. $module = array_shift($address);
  731. return module_invoke($module, 'addressable_content', $address, $type);
  732. }