query_string.inc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * @file query_string.inc
  4. * Context plugin that can extract arbitrary values from the query string.
  5. */
  6. /**
  7. * $plugin array which will be used by the system that includes this file.
  8. */
  9. $plugin = array(
  10. 'title' => t('Query string value'),
  11. 'description' => t('A context that extracts a value from the query string.'),
  12. 'context' => 'ctools_context_query_string_create_query_string',
  13. 'context name' => 'query_string',
  14. 'keyword' => 'query_string',
  15. 'edit form' => 'ctools_context_query_string_settings_form',
  16. 'convert list' => array(
  17. 'raw' => t('Raw string'),
  18. 'html_safe' => t('HTML-safe string'),
  19. ),
  20. 'convert' => 'ctools_context_query_string_convert',
  21. );
  22. /**
  23. * Create a context from manual configuration.
  24. */
  25. function ctools_context_query_string_create_query_string($empty, $data = NULL, $conf = FALSE) {
  26. $context = new ctools_context('query_string');
  27. $context->plugin = 'query_string';
  28. if ($empty) {
  29. return $context;
  30. }
  31. if ($conf) {
  32. if (!empty($_GET[$data['key']])) {
  33. $context->data = $_GET[$data['key']];
  34. }
  35. else {
  36. $context->data = $data['fallback_value'];
  37. }
  38. }
  39. return $context;
  40. }
  41. /**
  42. * Form builder; settings for the context.
  43. */
  44. function ctools_context_query_string_settings_form($form, &$form_state) {
  45. $form['key'] = array(
  46. '#title' => t('Query string key'),
  47. '#description' => t('Enter the key of the value that must be returned from the query string.'),
  48. '#type' => 'textfield',
  49. '#required' => TRUE
  50. );
  51. if (isset($form_state['conf']['key'])) {
  52. $form['key']['#default_value'] = $form_state['conf']['key'];
  53. }
  54. $form['fallback_value'] = array(
  55. '#title' => t('Fallback value'),
  56. '#description' => t('Enter a value that must be returned if the above specified key does not exist in the query string.'),
  57. '#type' => 'textfield',
  58. );
  59. if (!empty($form_state['conf']['fallback_value'])) {
  60. $form['fallback_value']['#default_value'] = $form_state['conf']['fallback_value'];
  61. }
  62. return $form;
  63. }
  64. /**
  65. * Submit handler; settings form for the context.
  66. */
  67. function ctools_context_query_string_settings_form_submit($form, &$form_state) {
  68. $form_state['conf']['key'] = $form_state['values']['key'];
  69. $form_state['conf']['fallback_value'] = $form_state['values']['fallback_value'];
  70. }
  71. /**
  72. * Convert a context into a string.
  73. */
  74. function ctools_context_query_string_convert($context, $type) {
  75. switch ($type) {
  76. case 'raw':
  77. return $context->data;
  78. case 'html_safe':
  79. return check_plain($context->data);
  80. }
  81. }