content.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. * - title_heading: The title heading.
  219. * - content: The safe to render HTML content.
  220. * - links: An array of links associated with the content suitable for
  221. * theme('links').
  222. * - more: An optional 'more' link (destination only)
  223. * - admin_links: Administrative links associated with the content, suitable
  224. * for theme('links').
  225. * - feeds: An array of feed icons or links associated with the content.
  226. * Each member of the array is rendered HTML.
  227. * - type: The content type.
  228. * - subtype: The content subtype. These two may be used together as
  229. * module-delta for block style rendering.
  230. */
  231. function ctools_content_render($type, $subtype, $conf, $keywords = array(), $args = array(), $context = array(), $incoming_content = '') {
  232. if (is_array($type)) {
  233. $plugin = $type;
  234. }
  235. else {
  236. $plugin = ctools_get_content_type($type);
  237. }
  238. $subtype_info = ctools_content_get_subtype($plugin, $subtype);
  239. $function = ctools_plugin_get_function($subtype_info, 'render callback');
  240. if (!$function) {
  241. $function = ctools_plugin_get_function($plugin, 'render callback');
  242. }
  243. if ($function) {
  244. $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
  245. if ($pane_context === FALSE) {
  246. return;
  247. }
  248. $content = $function($subtype, $conf, $args, $pane_context, $incoming_content);
  249. if (empty($content)) {
  250. return;
  251. }
  252. // Set up some defaults and other massaging on the content before we hand
  253. // it back to the caller.
  254. if (!isset($content->type)) {
  255. $content->type = $plugin['name'];
  256. }
  257. if (!isset($content->subtype)) {
  258. $content->subtype = $subtype;
  259. }
  260. // Override the title if configured to
  261. if (!empty($conf['override_title'])) {
  262. // Give previous title as an available substitution here.
  263. $keywords['%title'] = empty($content->title) ? '' : $content->title;
  264. $content->original_title = $keywords['%title'];
  265. $content->title = $conf['override_title_text'];
  266. $content->title_heading = isset($conf['override_title_heading']) ? $conf['override_title_heading'] : 'h2';
  267. }
  268. if (!empty($content->title)) {
  269. // Perform substitutions
  270. if (!empty($keywords) || !empty($context)) {
  271. $content->title = ctools_context_keyword_substitute($content->title, $keywords, $context);
  272. }
  273. // Sterilize the title
  274. $content->title = filter_xss_admin($content->title);
  275. // If a link is specified, populate.
  276. if (!empty($content->title_link)) {
  277. if (!is_array($content->title_link)) {
  278. $url = array('href' => $content->title_link);
  279. }
  280. else {
  281. $url = $content->title_link;
  282. }
  283. // set defaults so we don't bring up notices
  284. $url += array('href' => '', 'attributes' => array(), 'query' => array(), 'fragment' => '', 'absolute' => NULL, 'html' => TRUE);
  285. $content->title = l($content->title, $url['href'], $url);
  286. }
  287. }
  288. return $content;
  289. }
  290. }
  291. /**
  292. * Determine if a content type can be edited or not.
  293. *
  294. * Some content types simply have their content and no options. This function
  295. * lets a UI determine if it should display an edit link or not.
  296. */
  297. function ctools_content_editable($type, $subtype, $conf) {
  298. if (empty($type['edit form']) && empty($subtype['edit form'])) {
  299. return FALSE;
  300. }
  301. $function = FALSE;
  302. if (!empty($subtype['check editable'])) {
  303. $function = ctools_plugin_get_function($subtype, 'check editable');
  304. }
  305. elseif (!empty($type['check editable'])) {
  306. $function = ctools_plugin_get_function($type, 'check editable');
  307. }
  308. if ($function) {
  309. return $function($type, $subtype, $conf);
  310. }
  311. return TRUE;
  312. }
  313. /**
  314. * Get the administrative title from a given content type.
  315. *
  316. * @param $type
  317. * The content type. May be the name or an already loaded content type object.
  318. * @param $subtype
  319. * The subtype being rendered.
  320. * @param $conf
  321. * The configuration for the content type.
  322. * @param $context
  323. * An array of context objects available for use. These may be placeholders.
  324. */
  325. function ctools_content_admin_title($type, $subtype, $conf, $context = NULL) {
  326. if (is_array($type)) {
  327. $plugin = $type;
  328. }
  329. else if (is_string($type)) {
  330. $plugin = ctools_get_content_type($type);
  331. }
  332. else {
  333. return;
  334. }
  335. if ($function = ctools_plugin_get_function($plugin, 'admin title')) {
  336. $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
  337. if ($pane_context === FALSE) {
  338. if ($plugin['name'] == $subtype) {
  339. return t('@type will not display due to missing context', array('@type' => $plugin['name']));
  340. }
  341. return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype));
  342. }
  343. return $function($subtype, $conf, $pane_context);
  344. }
  345. else if (isset($plugin['admin title'])) {
  346. return $plugin['admin title'];
  347. }
  348. else if (isset($plugin['title'])) {
  349. return $plugin['title'];
  350. }
  351. }
  352. /**
  353. * Get the proper icon path to use, falling back to default icons if no icon exists.
  354. *
  355. * $subtype
  356. * The loaded subtype info.
  357. */
  358. function ctools_content_admin_icon($subtype) {
  359. $icon = '';
  360. if (isset($subtype['icon'])) {
  361. $icon = $subtype['icon'];
  362. if (!file_exists($icon)) {
  363. $icon = $subtype['path'] . '/' . $icon;
  364. }
  365. }
  366. if (empty($icon) || !file_exists($icon)) {
  367. $icon = ctools_image_path('no-icon.png');
  368. }
  369. return $icon;
  370. }
  371. /**
  372. * Set up the default $conf for a new instance of a content type.
  373. */
  374. function ctools_content_get_defaults($plugin, $subtype) {
  375. if (isset($plugin['defaults'])) {
  376. $defaults = $plugin['defaults'];
  377. }
  378. else if (isset($subtype['defaults'])) {
  379. $defaults = $subtype['defaults'];
  380. }
  381. if (isset($defaults)) {
  382. if (is_string($defaults) && function_exists($defaults)) {
  383. if ($return = $defaults($pane)) {
  384. return $return;
  385. }
  386. }
  387. else if (is_array($defaults)) {
  388. return $defaults;
  389. }
  390. }
  391. return array();
  392. }
  393. /**
  394. * Get the administrative title from a given content type.
  395. *
  396. * @param $type
  397. * The content type. May be the name or an already loaded content type object.
  398. * @param $subtype
  399. * The subtype being rendered.
  400. * @param $conf
  401. * The configuration for the content type.
  402. * @param $context
  403. * An array of context objects available for use. These may be placeholders.
  404. */
  405. function ctools_content_admin_info($type, $subtype, $conf, $context = NULL) {
  406. if (is_array($type)) {
  407. $plugin = $type;
  408. }
  409. else {
  410. $plugin = ctools_get_content_type($type);
  411. }
  412. if ($function = ctools_plugin_get_function($plugin, 'admin info')) {
  413. $output = $function($subtype, $conf, $context);
  414. }
  415. if (empty($output) || !is_object($output)) {
  416. $output = new stdClass();
  417. // replace the _ with " " for a better output
  418. $subtype = check_plain(str_replace("_", " ", $subtype));
  419. $output->title = $subtype;
  420. $output->content = t('No info available.');
  421. }
  422. return $output;
  423. }
  424. /**
  425. * Add the default FAPI elements to the content type configuration form
  426. */
  427. function ctools_content_configure_form_defaults($form, &$form_state) {
  428. $plugin = $form_state['plugin'];
  429. $subtype = $form_state['subtype'];
  430. $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : NULL;
  431. $conf = $form_state['conf'];
  432. $add_submit = FALSE;
  433. if (!empty($subtype['required context']) && is_array($contexts)) {
  434. $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ? $conf['context'] : array());
  435. $add_submit = TRUE;
  436. }
  437. ctools_include('dependent');
  438. // Unless we're not allowed to override the title on this content type, add this
  439. // gadget to all panes.
  440. if (empty($plugin['no title override']) && empty($subtype['no title override'])) {
  441. $form['aligner_start'] = array(
  442. '#markup' => '<div class="option-text-aligner clearfix">',
  443. );
  444. $form['override_title'] = array(
  445. '#type' => 'checkbox',
  446. '#default_value' => isset($conf['override_title']) ? $conf['override_title'] : '',
  447. '#title' => t('Override title'),
  448. '#id' => 'override-title-checkbox',
  449. );
  450. $form['override_title_text'] = array(
  451. '#type' => 'textfield',
  452. '#default_value' => isset($conf['override_title_text']) ? $conf['override_title_text'] : '',
  453. '#size' => 35,
  454. '#id' => 'override-title-textfield',
  455. '#dependency' => array('override-title-checkbox' => array(1)),
  456. '#dependency_type' => 'hidden',
  457. );
  458. $form['override_title_heading'] = array(
  459. '#type' => 'select',
  460. '#default_value' => isset($conf['override_title_heading']) ? $conf['override_title_heading'] : 'h2',
  461. '#options' => array(
  462. 'h1' => t('h1'),
  463. 'h2' => t('h2'),
  464. 'h3' => t('h3'),
  465. 'h4' => t('h4'),
  466. 'h5' => t('h5'),
  467. 'h6' => t('h6'),
  468. 'div' => t('div'),
  469. 'span' => t('span'),
  470. ),
  471. '#id' => 'override-title-heading',
  472. '#dependency' => array('override-title-checkbox' => array(1)),
  473. '#dependency_type' => 'hidden',
  474. );
  475. $form['aligner_stop'] = array(
  476. '#markup' => '</div>',
  477. );
  478. if (is_array($contexts)) {
  479. $form['override_title_markup'] = array(
  480. '#prefix' => '<div class="description">',
  481. '#suffix' => '</div>',
  482. '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'),
  483. );
  484. }
  485. $add_submit = TRUE;
  486. }
  487. if ($add_submit) {
  488. // '#submit' is already set up due to the wizard.
  489. $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
  490. }
  491. return $form;
  492. }
  493. /**
  494. * Submit handler to store context/title override info.
  495. */
  496. function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
  497. if (isset($form_state['values']['context'])) {
  498. $form_state['conf']['context'] = $form_state['values']['context'];
  499. }
  500. if (isset($form_state['values']['override_title'])) {
  501. $form_state['conf']['override_title'] = $form_state['values']['override_title'];
  502. $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
  503. $form_state['conf']['override_title_heading'] = $form_state['values']['override_title_heading'];
  504. }
  505. }
  506. /**
  507. * Get the config form.
  508. *
  509. * The $form_info and $form_state need to be preconfigured with data you'll need
  510. * such as whether or not you're using ajax, or the modal. $form_info will need
  511. * your next/submit callbacks so that you can cache your data appropriately.
  512. *
  513. * @return
  514. * If this function returns false, no form exists.
  515. */
  516. function ctools_content_form($op, $form_info, &$form_state, $plugin, $subtype_name, $subtype, &$conf, $step = NULL) {
  517. $form_state += array(
  518. 'plugin' => $plugin,
  519. 'subtype' => $subtype,
  520. 'subtype_name' => $subtype_name,
  521. 'conf' => &$conf,
  522. 'op' => $op,
  523. );
  524. $form_info += array(
  525. 'id' => 'ctools_content_form',
  526. 'show back' => TRUE,
  527. );
  528. // Turn the forms defined in the plugin into the format the wizard needs.
  529. if ($op == 'add') {
  530. if (!empty($subtype['add form'])) {
  531. _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
  532. }
  533. else if (!empty($plugin['add form'])) {
  534. _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
  535. }
  536. }
  537. if (empty($form_info['order'])) {
  538. // Use the edit form for the add form if add form was completely left off.
  539. if (!empty($subtype['edit form'])) {
  540. _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
  541. }
  542. else if (!empty($plugin['edit form'])) {
  543. _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
  544. }
  545. }
  546. if (empty($form_info['order'])) {
  547. return FALSE;
  548. }
  549. ctools_include('wizard');
  550. return ctools_wizard_multistep_form($form_info, $step, $form_state);
  551. }
  552. function _ctools_content_create_form_info(&$form_info, $info, $plugin, $subtype, $op) {
  553. if (is_string($info)) {
  554. if (empty($subtype['title'])) {
  555. $title = t('Configure');
  556. }
  557. else if ($op == 'add') {
  558. $title = t('Configure new !subtype_title', array('!subtype_title' => $subtype['title']));
  559. }
  560. else {
  561. $title = t('Configure !subtype_title', array('!subtype_title' => $subtype['title']));
  562. }
  563. $form_info['order'] = array('form' => $title);
  564. $form_info['forms'] = array(
  565. 'form' => array(
  566. 'title' => $title,
  567. 'form id' => $info,
  568. 'wrapper' => 'ctools_content_configure_form_defaults',
  569. ),
  570. );
  571. }
  572. else if (is_array($info)) {
  573. $form_info['order'] = array();
  574. $form_info['forms'] = array();
  575. $count = 0;
  576. $base = 'step';
  577. $wrapper = NULL;
  578. foreach ($info as $form_id => $title) {
  579. // @todo -- docs say %title can be used to sub for the admin title.
  580. $step = $base . ++$count;
  581. if (empty($wrapper)) {
  582. $wrapper = $step;
  583. }
  584. if (is_array($title)) {
  585. if (!empty($title['default'])) {
  586. $wrapper = $step;
  587. }
  588. $title = $title['title'];
  589. }
  590. $form_info['order'][$step] = $title;
  591. $form_info['forms'][$step] = array(
  592. 'title' => $title,
  593. 'form id' => $form_id,
  594. );
  595. }
  596. if ($wrapper) {
  597. $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
  598. }
  599. }
  600. }
  601. /**
  602. * Get an array of all available content types that can be fed into the
  603. * display editor for the add content list.
  604. *
  605. * @param $context
  606. * If a context is provided, content that requires that context can apepar.
  607. * @param $has_content
  608. * Whether or not the display will have incoming content
  609. * @param $allowed_types
  610. * An array of allowed content types (pane types) keyed by content_type . '-' . sub_type
  611. * @param $default_types
  612. * A default allowed/denied status for content that isn't known about
  613. */
  614. function ctools_content_get_available_types($contexts = NULL, $has_content = FALSE, $allowed_types = NULL, $default_types = NULL) {
  615. $plugins = ctools_get_content_types();
  616. $available = array();
  617. foreach ($plugins as $id => $plugin) {
  618. foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
  619. // exclude items that require content if we're saying we don't
  620. // provide it.
  621. if (!empty($subtype['requires content']) && !$has_content) {
  622. continue;
  623. }
  624. // Check to see if the content type can be used in this context.
  625. if (!empty($subtype['required context'])) {
  626. if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
  627. continue;
  628. }
  629. }
  630. // Check to see if the passed-in allowed types allows this content.
  631. if ($allowed_types) {
  632. $key = $id . '-' . $subtype_id;
  633. if (!isset($allowed_types[$key])) {
  634. $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
  635. }
  636. if (!$allowed_types[$key]) {
  637. continue;
  638. }
  639. }
  640. // Check if the content type provides an access callback.
  641. if (isset($subtype['create content access']) && function_exists($subtype['create content access']) && !$subtype['create content access']($plugin, $subtype)) {
  642. continue;
  643. }
  644. // If we made it through all the tests, then we can use this content.
  645. $available[$id][$subtype_id] = $subtype;
  646. }
  647. }
  648. return $available;
  649. }
  650. /**
  651. * Get an array of all content types that can be fed into the
  652. * display editor for the add content list, regardless of
  653. * availability.
  654. *
  655. */
  656. function ctools_content_get_all_types() {
  657. $plugins = ctools_get_content_types();
  658. $available = array();
  659. foreach ($plugins as $id => $plugin) {
  660. foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
  661. // If we made it through all the tests, then we can use this content.
  662. $available[$id][$subtype_id] = $subtype;
  663. }
  664. }
  665. return $available;
  666. }
  667. /**
  668. * Select the context to be used for a piece of content, based upon config.
  669. *
  670. * @param $plugin
  671. * The content plugin
  672. * @param $subtype
  673. * The subtype of the content.
  674. * @param $conf
  675. * The configuration array that should contain the context.
  676. * @param $contexts
  677. * A keyed array of available contexts.
  678. *
  679. * @return
  680. * The matching contexts or NULL if none or necessary, or FALSE if
  681. * requirements can't be met.
  682. */
  683. function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
  684. // Identify which of our possible contexts apply.
  685. if (empty($subtype)) {
  686. return;
  687. }
  688. $subtype_info = ctools_content_get_subtype($plugin, $subtype);
  689. if (empty($subtype_info)) {
  690. return;
  691. }
  692. if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
  693. return $contexts;
  694. }
  695. // If the content requires a context, fetch it; if no context is returned,
  696. // do not display the pane.
  697. if (empty($subtype_info['required context'])) {
  698. return;
  699. }
  700. // Deal with dynamic required contexts not getting updated in the panes.
  701. // For example, Views let you dynamically change context info. While
  702. // we cannot be perfect, one thing we can do is if no context at all
  703. // was asked for, and then was later added but none is selected, make
  704. // a best guess as to what context should be used. THis is right more
  705. // than it's wrong.
  706. if (is_array($subtype_info['required context'])) {
  707. if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
  708. foreach ($subtype_info['required context'] as $index => $required) {
  709. if (!isset($conf['context'][$index])) {
  710. $filtered = ctools_context_filter($contexts, $required);
  711. if ($filtered) {
  712. $keys = array_keys($filtered);
  713. $conf['context'][$index] = array_shift($keys);
  714. }
  715. }
  716. }
  717. }
  718. }
  719. else {
  720. if (empty($conf['context'])) {
  721. $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
  722. if ($filtered) {
  723. $keys = array_keys($filtered);
  724. $conf['context'] = array_shift($keys);
  725. }
  726. }
  727. }
  728. if (empty($conf['context'])) {
  729. return;
  730. }
  731. $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
  732. return $context;
  733. }
  734. /**
  735. * Fetch a piece of content from the addressable content system.
  736. *
  737. * @param $address
  738. * A string or an array representing the address of the content.
  739. * @param $type
  740. * The type of content to return. The type is dependent on what
  741. * the content actually is. The default, 'content' means a simple
  742. * string representing the content. However, richer systems may
  743. * offer more options. For example, Panels might allow the
  744. * fetching of 'display' and 'pane' objects. Page Manager
  745. * might allow for the fetching of 'task_handler' objects
  746. * (AKA variants).
  747. */
  748. function ctools_get_addressable_content($address, $type = 'content') {
  749. if (!is_array($address)) {
  750. $address = explode('::', $address);
  751. }
  752. if (!$address) {
  753. return;
  754. }
  755. // This removes the module from the address so the
  756. // implementor is not responsible for that part.
  757. $module = array_shift($address);
  758. return module_invoke($module, 'addressable_content', $address, $type);
  759. }