Project

General

Profile

Paste
Download (2.36 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / ctools / plugins / contexts / string.inc @ 6e3ce7c2

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide a string context.
6
 */
7

    
8
/**
9
 * Plugins are described by creating a $plugin array which will be used
10
 * by the system that includes this file.
11
 */
12
$plugin = array(
13
  'title' => t('String'),
14
  'description' => t('A context that is just a string.'),
15
  'context' => 'ctools_context_create_string',
16
  'edit form' => 'ctools_context_string_settings_form',
17
  'defaults' => '',
18
  'keyword' => 'string',
19
  'no ui' => FALSE,
20
  'context name' => 'string',
21
  'convert list' => array(
22
    'raw' => t('Raw string'),
23
    'html_safe' => t('HTML-safe string'),
24
    'uppercase_words_html_safe' => t('Uppercase words HTML-safe string'),
25
  ),
26
  'convert' => 'ctools_context_string_convert',
27
  'placeholder form' => array(
28
    '#type' => 'textfield',
29
    '#description' => t('Enter the string for this context.'),
30
  ),
31
);
32

    
33
/**
34
 * It's important to remember that $conf is optional here, because contexts
35
 * are not always created from the UI.
36
 */
37
function ctools_context_create_string($empty, $data = NULL, $conf = FALSE) {
38
  // The input is expected to be an object as created by ctools_break_phrase
39
  // which contains a group of string.
40
  $context = new ctools_context('string');
41
  $context->plugin = 'string';
42

    
43
  if ($empty) {
44
    return $context;
45
  }
46

    
47
  if ($data !== FALSE) {
48
    // Support the array storage from the settings form but also handle direct input from arguments.
49
    $context->data = is_array($data) ? $data['string'] : $data;
50
    $context->title = ($conf) ? check_plain($data['identifier']) : check_plain($data);
51
    return $context;
52
  }
53
}
54

    
55
/**
56
 * Convert a context into a string.
57
 */
58
function ctools_context_string_convert($context, $type) {
59
  switch ($type) {
60
    case 'raw':
61
      return $context->data;
62

    
63
    case 'html_safe':
64
      return check_plain($context->data);
65

    
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
}