content.inc 25 KB

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