string.inc 2.4 KB

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