simplecontext_content_type.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Sample ctools content type that takes advantage of context.
  5. *
  6. * This example uses the context it gets (simplecontext) to demo how a
  7. * ctools content type can access and use context. Note that the simplecontext
  8. * can be either configured manually into a panel or it can be retrieved via
  9. * an argument.
  10. */
  11. /**
  12. * Plugins are described by creating a $plugin array which will be used
  13. * by the system that includes this file.
  14. */
  15. $plugin = array(
  16. 'title' => t('Simplecontext content type'),
  17. 'content_types' => 'simplecontext_content_type',
  18. // 'single' means not to be subtyped.
  19. 'single' => TRUE,
  20. // Name of a function which will render the block.
  21. 'render callback' => 'simplecontext_content_type_render',
  22. // Icon goes in the directory with the content type.
  23. 'icon' => 'icon_example.png',
  24. 'description' => t('Simplecontext content type - works with a simplecontext context.'),
  25. 'required context' => new ctools_context_required(t('Simplecontext'), 'simplecontext'),
  26. 'edit form' => 'simplecontext_content_type_edit_form',
  27. 'admin title' => 'ctools_plugin_example_simplecontext_content_type_admin_title',
  28. // Presents a block which is used in the preview of the data.
  29. // Pn Panels this is the preview pane shown on the panels building page.
  30. 'admin info' => 'ctools_plugin_example_simplecontext_content_type_admin_info',
  31. 'category' => array(t('CTools Examples'), -9),
  32. );
  33. function ctools_plugin_example_simplecontext_content_type_admin_title($subtype, $conf, $context = NULL) {
  34. $output = t('Simplecontext');
  35. if ($conf['override_title'] && !empty($conf['override_title_text'])) {
  36. $output = filter_xss_admin($conf['override_title_text']);
  37. }
  38. return $output;
  39. }
  40. /**
  41. * Callback to provide administrative info (the preview in panels when building
  42. * a panel).
  43. *
  44. * In this case we'll render the content with a dummy argument and
  45. * a dummy context.
  46. */
  47. function ctools_plugin_example_simplecontext_content_type_admin_info($subtype, $conf, $context = NULL) {
  48. $context = new stdClass();
  49. $context->data = new stdClass();
  50. $context->data->description = t("no real context");
  51. $block = simplecontext_content_type_render($subtype, $conf, array("Example"), $context);
  52. return $block;
  53. }
  54. /**
  55. * Run-time rendering of the body of the block (content type)
  56. *
  57. * @param $subtype
  58. * @param $conf
  59. * Configuration as done at admin time
  60. * @param $args
  61. * @param $context
  62. * Context - in this case we don't have any
  63. *
  64. * @return
  65. * An object with at least title and content members
  66. */
  67. function simplecontext_content_type_render($subtype, $conf, $args, $context) {
  68. $data = $context->data;
  69. $block = new stdClass();
  70. // Don't forget to check this data if it's untrusted.
  71. // The title actually used in rendering.
  72. $block->title = "Simplecontext content type";
  73. $block->content = t("
  74. This is a block of data created by the Simplecontext content type.
  75. Data in the block may be assembled from static text (like this) or from the
  76. content type settings form (\$conf) for the content type, or from the context
  77. that is passed in. <br />
  78. In our case, the configuration form (\$conf) has just one field, 'config_item_1;
  79. and it's configured with:
  80. ");
  81. if (!empty($conf)) {
  82. $block->content .= '<div style="border: 1px solid red;">' . print_r(filter_xss_admin($conf['config_item_1']), TRUE) . '</div>';
  83. }
  84. if (!empty($context)) {
  85. $block->content .= '<br />The args ($args) were <div style="border: 1px solid yellow;" >' .
  86. var_export($args, TRUE) . '</div>';
  87. }
  88. $block->content .= '<br />And the simplecontext context ($context->data->description) was <div style="border: 1px solid green;" >' .
  89. print_r($context->data->description, TRUE) . '</div>';
  90. return $block;
  91. }
  92. /**
  93. * 'Edit' callback for the content type.
  94. * This example just returns a form.
  95. */
  96. function simplecontext_content_type_edit_form($form, &$form_state) {
  97. $conf = $form_state['conf'];
  98. $form['config_item_1'] = array(
  99. '#type' => 'textfield',
  100. '#title' => t('Config Item 1 for simplecontext content type'),
  101. '#size' => 50,
  102. '#description' => t('The stuff for item 1.'),
  103. '#default_value' => !empty($conf['config_item_1']) ? $conf['config_item_1'] : '',
  104. '#prefix' => '<div class="clear-block no-float">',
  105. '#suffix' => '</div>',
  106. );
  107. return $form;
  108. }
  109. function simplecontext_content_type_edit_form_submit($form, &$form_state) {
  110. foreach (element_children($form) as $key) {
  111. if (!empty($form_state['values'][$key])) {
  112. $form_state['conf'][$key] = $form_state['values'][$key];
  113. }
  114. }
  115. }