string.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Plugin to provide a string context
  6. */
  7. /**
  8. * Plugins are described by creating a $plugin array which will be used
  9. * by the system that includes this file.
  10. */
  11. $plugin = array(
  12. 'title' => t('String'),
  13. 'description' => t('A context that is just a string.'),
  14. 'context' => 'ctools_context_create_string',
  15. 'edit form' => 'ctools_context_string_settings_form',
  16. 'defaults' => '',
  17. 'keyword' => 'string',
  18. 'no ui' => FALSE,
  19. 'context name' => 'string',
  20. 'convert list' => array(
  21. 'raw' => t('Raw string'),
  22. 'html_safe' => t('HTML-safe string'),
  23. 'uppercase_words_html_safe' => t('Uppercase words HTML-safe string'),
  24. ),
  25. 'convert' => 'ctools_context_string_convert',
  26. 'placeholder form' => array(
  27. '#type' => 'textfield',
  28. '#description' => t('Enter the string for this context.'),
  29. ),
  30. );
  31. /**
  32. * It's important to remember that $conf is optional here, because contexts
  33. * are not always created from the UI.
  34. */
  35. function ctools_context_create_string($empty, $data = NULL, $conf = FALSE) {
  36. // The input is expected to be an object as created by ctools_break_phrase
  37. // which contains a group of string.
  38. $context = new ctools_context('string');
  39. $context->plugin = 'string';
  40. if ($empty) {
  41. return $context;
  42. }
  43. if ($data !== FALSE ) {
  44. // Support the array storage from the settings form but also handle direct input from arguments.
  45. $context->data = is_array($data) ? $data['string'] : $data;
  46. $context->title = ($conf) ? check_plain($data['identifier']) : check_plain($data);
  47. return $context;
  48. }
  49. }
  50. /**
  51. * Convert a context into a string.
  52. */
  53. function ctools_context_string_convert($context, $type) {
  54. switch ($type) {
  55. case 'raw':
  56. return $context->data;
  57. case 'html_safe':
  58. return check_plain($context->data);
  59. case 'uppercase_words_html_safe':
  60. return ucwords(str_replace('-', ' ', check_plain($context->data)));
  61. }
  62. }
  63. /**
  64. * String settings form.
  65. */
  66. function ctools_context_string_settings_form($form, &$form_state) {
  67. $conf = &$form_state['conf'];
  68. $form['string'] = array(
  69. '#title' => t('Enter the string'),
  70. '#type' => 'textfield',
  71. '#maxlength' => 512,
  72. '#weight' => -10,
  73. '#default_value' => $conf['string'],
  74. );
  75. return $form;
  76. }
  77. function ctools_context_string_settings_form_submit($form, &$form_state) {
  78. $form_state['conf']['string'] = $form_state['values']['string'];
  79. }