Project

General

Profile

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Context plugin that can extract arbitrary values from the query string.
6
 */
7

    
8
/**
9
 * $plugin array which will be used by the system that includes this file.
10
 */
11
$plugin = array(
12
  'title' => t('Query string value'),
13
  'description' => t('A context that extracts a value from the query string.'),
14
  'context' => 'ctools_context_query_string_create_query_string',
15
  'context name' => 'query_string',
16
  'keyword' => 'query_string',
17
  'edit form' => 'ctools_context_query_string_settings_form',
18
  'convert list' => array(
19
    'raw' => t('Raw string'),
20
    'html_safe' => t('HTML-safe string'),
21
  ),
22
  'convert' => 'ctools_context_query_string_convert',
23
);
24

    
25
/**
26
 * Create a context from manual configuration.
27
 */
28
function ctools_context_query_string_create_query_string($empty, $data = NULL, $conf = FALSE) {
29
  $context = new ctools_context('query_string');
30
  $context->plugin = 'query_string';
31

    
32
  if ($empty) {
33
    return $context;
34
  }
35

    
36
  if ($conf) {
37
    if (!empty($_GET[$data['key']])) {
38
      $context->data = $_GET[$data['key']];
39
    }
40
    else {
41
      $context->data = $data['fallback_value'];
42
    }
43
  }
44
  return $context;
45
}
46

    
47
/**
48
 * Form builder; settings for the context.
49
 */
50
function ctools_context_query_string_settings_form($form, &$form_state) {
51
  $form['key'] = array(
52
    '#title' => t('Query string key'),
53
    '#description' => t('Enter the key of the value that must be returned from the query string.'),
54
    '#type' => 'textfield',
55
    '#required' => TRUE,
56
  );
57
  if (isset($form_state['conf']['key'])) {
58
    $form['key']['#default_value'] = $form_state['conf']['key'];
59
  }
60
  $form['fallback_value'] = array(
61
    '#title' => t('Fallback value'),
62
    '#description' => t('Enter a value that must be returned if the above specified key does not exist in the query string.'),
63
    '#type' => 'textfield',
64
  );
65
  if (!empty($form_state['conf']['fallback_value'])) {
66
    $form['fallback_value']['#default_value'] = $form_state['conf']['fallback_value'];
67
  }
68
  return $form;
69
}
70

    
71
/**
72
 * Submit handler; settings form for the context.
73
 */
74
function ctools_context_query_string_settings_form_submit($form, &$form_state) {
75
  $form_state['conf']['key'] = $form_state['values']['key'];
76
  $form_state['conf']['fallback_value'] = $form_state['values']['fallback_value'];
77
}
78

    
79
/**
80
 * Convert a context into a string.
81
 */
82
function ctools_context_query_string_convert($context, $type) {
83
  switch ($type) {
84
    case 'raw':
85
      return $context->data;
86

    
87
    case 'html_safe':
88
      return check_plain($context->data);
89
  }
90
}