simplecontext.inc
3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* @file
* Sample ctools context type plugin that shows how to create a context from an arg.
*
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Simplecontext"),
'description' => t('A single "simplecontext" context, or data element.'),
'context' => 'ctools_plugin_example_context_create_simplecontext', // func to create context
'context name' => 'simplecontext',
'settings form' => 'simplecontext_settings_form',
'keyword' => 'simplecontext',
// Provides a list of items which are exposed as keywords.
'convert list' => 'simplecontext_convert_list',
// Convert keywords into data.
'convert' => 'simplecontext_convert',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter some data to represent this "simplecontext".'),
),
);
/**
* Create a context, either from manual configuration or from an argument on the URL.
*
* @param $empty
* If true, just return an empty context.
* @param $data
* If from settings form, an array as from a form. If from argument, a string.
* @param $conf
* TRUE if the $data is coming from admin configuration, FALSE if it's from a URL arg.
*
* @return
* a Context object/
*/
function ctools_plugin_example_context_create_simplecontext($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context('simplecontext');
$context->plugin = 'simplecontext';
if ($empty) {
return $context;
}
if ($conf) {
if (!empty($data)) {
$context->data = new stdClass();
// For this simple item we'll just create our data by stripping non-alpha and
// adding '_from_configuration_item_1' to it.
$context->data->item1 = t("Item1");
$context->data->item2 = t("Item2");
$context->data->description = preg_replace('/[^a-z]/i', '', $data['sample_simplecontext_setting']);
$context->data->description .= '_from_configuration_sample_simplecontext_setting';
$context->title = t("Simplecontext context from config");
return $context;
}
}
else {
// $data is coming from an arg - it's just a string.
// This is used for keyword.
$context->title = $data;
$context->argument = $data;
// Make up a bogus context
$context->data = new stdClass();
$context->data->item1 = t("Item1");
$context->data->item2 = t("Item2");
// For this simple item we'll just create our data by stripping non-alpha and
// adding '_from_simplecontext_argument' to it.
$context->data->description = preg_replace('/[^a-z]/i', '', $data);
$context->data->description .= '_from_simplecontext_argument';
$context->arg_length = strlen($context->argument);
return $context;
}
}
function simplecontext_settings_form($conf, $external = FALSE) {
if (empty($conf)) {
$conf = array(
'sample_simplecontext_setting' => 'default simplecontext setting',
);
}
$form = array();
$form['sample_simplecontext_setting'] = array(
'#type' => 'textfield',
'#title' => t('Setting for simplecontext'),
'#size' => 50,
'#description' => t('An example setting that could be used to configure a context'),
'#default_value' => $conf['sample_simplecontext_setting'],
'#prefix' => '<div class="clear-block no-float">',
'#suffix' => '</div>',
);
return $form;
}
/**
* Provide a list of sub-keywords.
*
* This is used to provide keywords from the context for use in a content type,
* pane, etc.
*/
function simplecontext_convert_list() {
return array(
'item1' => t('Item1'),
'item2' => t('Item2'),
'description' => t('Description'),
);
}
/**
* Convert a context into a string to be used as a keyword by content types, etc.
*/
function simplecontext_convert($context, $type) {
switch ($type) {
case 'item1':
return $context->data->item1;
case 'item2':
return $context->data->item2;
case 'description':
return $context->data->description;
}
}