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