content.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. // If we made it through all the tests, then we can use this content.
  614. $available[$id][$subtype_id] = $subtype;
  615. }
  616. }
  617. return $available;
  618. }
  619. /**
  620. * Get an array of all content types that can be fed into the
  621. * display editor for the add content list, regardless of
  622. * availability.
  623. *
  624. */
  625. function ctools_content_get_all_types() {
  626. $plugins = ctools_get_content_types();
  627. $available = array();
  628. foreach ($plugins as $id => $plugin) {
  629. foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
  630. // If we made it through all the tests, then we can use this content.
  631. $available[$id][$subtype_id] = $subtype;
  632. }
  633. }
  634. return $available;
  635. }
  636. /**
  637. * Select the context to be used for a piece of content, based upon config.
  638. *
  639. * @param $plugin
  640. * The content plugin
  641. * @param $subtype
  642. * The subtype of the content.
  643. * @param $conf
  644. * The configuration array that should contain the context.
  645. * @param $contexts
  646. * A keyed array of available contexts.
  647. *
  648. * @return
  649. * The matching contexts or NULL if none or necessary, or FALSE if
  650. * requirements can't be met.
  651. */
  652. function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
  653. // Identify which of our possible contexts apply.
  654. if (empty($subtype)) {
  655. return;
  656. }
  657. $subtype_info = ctools_content_get_subtype($plugin, $subtype);
  658. if (empty($subtype_info)) {
  659. return;
  660. }
  661. if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
  662. return $contexts;
  663. }
  664. // If the content requires a context, fetch it; if no context is returned,
  665. // do not display the pane.
  666. if (empty($subtype_info['required context'])) {
  667. return;
  668. }
  669. // Deal with dynamic required contexts not getting updated in the panes.
  670. // For example, Views let you dynamically change context info. While
  671. // we cannot be perfect, one thing we can do is if no context at all
  672. // was asked for, and then was later added but none is selected, make
  673. // a best guess as to what context should be used. THis is right more
  674. // than it's wrong.
  675. if (is_array($subtype_info['required context'])) {
  676. if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
  677. foreach ($subtype_info['required context'] as $index => $required) {
  678. if (!isset($conf['context'][$index])) {
  679. $filtered = ctools_context_filter($contexts, $required);
  680. if ($filtered) {
  681. $keys = array_keys($filtered);
  682. $conf['context'][$index] = array_shift($keys);
  683. }
  684. }
  685. }
  686. }
  687. }
  688. else {
  689. if (empty($conf['context'])) {
  690. $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
  691. if ($filtered) {
  692. $keys = array_keys($filtered);
  693. $conf['context'] = array_shift($keys);
  694. }
  695. }
  696. }
  697. if (empty($conf['context'])) {
  698. return;
  699. }
  700. $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
  701. return $context;
  702. }
  703. /**
  704. * Fetch a piece of content from the addressable content system.
  705. *
  706. * @param $address
  707. * A string or an array representing the address of the content.
  708. * @param $type
  709. * The type of content to return. The type is dependent on what
  710. * the content actually is. The default, 'content' means a simple
  711. * string representing the content. However, richer systems may
  712. * offer more options. For example, Panels might allow the
  713. * fetching of 'display' and 'pane' objects. Page Manager
  714. * might allow for the fetching of 'task_handler' objects
  715. * (AKA variants).
  716. */
  717. function ctools_get_addressable_content($address, $type = 'content') {
  718. if (!is_array($address)) {
  719. $address = explode('::', $address);
  720. }
  721. if (!$address) {
  722. return;
  723. }
  724. // This removes the module from the address so the
  725. // implementor is not responsible for that part.
  726. $module = array_shift($address);
  727. return module_invoke($module, 'addressable_content', $address, $type);
  728. }