content.inc 26 KB

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