simplecontext.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Sample ctools context type plugin that shows how to create a context from an arg.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t("Simplecontext"),
  12. 'description' => t('A single "simplecontext" context, or data element.'),
  13. // Func to create context.
  14. 'context' => 'ctools_plugin_example_context_create_simplecontext',
  15. 'context name' => 'simplecontext',
  16. 'settings form' => 'simplecontext_settings_form',
  17. 'keyword' => 'simplecontext',
  18. // Provides a list of items which are exposed as keywords.
  19. 'convert list' => 'simplecontext_convert_list',
  20. // Convert keywords into data.
  21. 'convert' => 'simplecontext_convert',
  22. 'placeholder form' => array(
  23. '#type' => 'textfield',
  24. '#description' => t('Enter some data to represent this "simplecontext".'),
  25. ),
  26. );
  27. /**
  28. * Create a context, either from manual configuration or from an argument on the URL.
  29. *
  30. * @param $empty
  31. * If true, just return an empty context.
  32. * @param $data
  33. * If from settings form, an array as from a form. If from argument, a string.
  34. * @param $conf
  35. * TRUE if the $data is coming from admin configuration, FALSE if it's from a URL arg.
  36. *
  37. * @return
  38. * a Context object/
  39. */
  40. function ctools_plugin_example_context_create_simplecontext($empty, $data = NULL, $conf = FALSE) {
  41. $context = new ctools_context('simplecontext');
  42. $context->plugin = 'simplecontext';
  43. if ($empty) {
  44. return $context;
  45. }
  46. if ($conf) {
  47. if (!empty($data)) {
  48. $context->data = new stdClass();
  49. // For this simple item we'll just create our data by stripping non-alpha and
  50. // adding '_from_configuration_item_1' to it.
  51. $context->data->item1 = t("Item1");
  52. $context->data->item2 = t("Item2");
  53. $context->data->description = preg_replace('/[^a-z]/i', '', $data['sample_simplecontext_setting']);
  54. $context->data->description .= '_from_configuration_sample_simplecontext_setting';
  55. $context->title = t("Simplecontext context from config");
  56. return $context;
  57. }
  58. }
  59. else {
  60. // $data is coming from an arg - it's just a string.
  61. // This is used for keyword.
  62. $context->title = $data;
  63. $context->argument = $data;
  64. // Make up a bogus context.
  65. $context->data = new stdClass();
  66. $context->data->item1 = t("Item1");
  67. $context->data->item2 = t("Item2");
  68. // For this simple item we'll just create our data by stripping non-alpha and
  69. // adding '_from_simplecontext_argument' to it.
  70. $context->data->description = preg_replace('/[^a-z]/i', '', $data);
  71. $context->data->description .= '_from_simplecontext_argument';
  72. $context->arg_length = strlen($context->argument);
  73. return $context;
  74. }
  75. }
  76. function simplecontext_settings_form($conf, $external = FALSE) {
  77. if (empty($conf)) {
  78. $conf = array(
  79. 'sample_simplecontext_setting' => 'default simplecontext setting',
  80. );
  81. }
  82. $form = array();
  83. $form['sample_simplecontext_setting'] = array(
  84. '#type' => 'textfield',
  85. '#title' => t('Setting for simplecontext'),
  86. '#size' => 50,
  87. '#description' => t('An example setting that could be used to configure a context'),
  88. '#default_value' => $conf['sample_simplecontext_setting'],
  89. '#prefix' => '<div class="clear-block no-float">',
  90. '#suffix' => '</div>',
  91. );
  92. return $form;
  93. }
  94. /**
  95. * Provide a list of sub-keywords.
  96. *
  97. * This is used to provide keywords from the context for use in a content type,
  98. * pane, etc.
  99. */
  100. function simplecontext_convert_list() {
  101. return array(
  102. 'item1' => t('Item1'),
  103. 'item2' => t('Item2'),
  104. 'description' => t('Description'),
  105. );
  106. }
  107. /**
  108. * Convert a context into a string to be used as a keyword by content types, etc.
  109. */
  110. function simplecontext_convert($context, $type) {
  111. switch ($type) {
  112. case 'item1':
  113. return $context->data->item1;
  114. case 'item2':
  115. return $context->data->item2;
  116. case 'description':
  117. return $context->data->description;
  118. }
  119. }