comparison sites/all/modules/ctools/ctools_plugin_example/plugins/contexts/simplecontext.inc @ 0:ff03f76ab3fe

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