Projet

Général

Profil

Paste
Télécharger (3,95 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / ctools_plugin_example / plugins / contexts / simplecontext.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 * Sample ctools context type plugin that shows how to create a context from an arg.
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("Simplecontext"),
14
  'description' => t('A single "simplecontext" context, or data element.'),
15
// Func to create context.
16
  'context' => 'ctools_plugin_example_context_create_simplecontext',
17
  'context name' => 'simplecontext',
18
  'settings form' => 'simplecontext_settings_form',
19
  'keyword' => 'simplecontext',
20

    
21
  // Provides a list of items which are exposed as keywords.
22
  'convert list' => 'simplecontext_convert_list',
23
  // Convert keywords into data.
24
  'convert' => 'simplecontext_convert',
25

    
26
  'placeholder form' => array(
27
    '#type' => 'textfield',
28
    '#description' => t('Enter some data to represent this "simplecontext".'),
29
  ),
30
);
31

    
32
/**
33
 * Create a context, either from manual configuration or from an argument on the URL.
34
 *
35
 * @param $empty
36
 *   If true, just return an empty context.
37
 * @param $data
38
 *   If from settings form, an array as from a form. If from argument, a string.
39
 * @param $conf
40
 *   TRUE if the $data is coming from admin configuration, FALSE if it's from a URL arg.
41
 *
42
 * @return
43
 *   a Context object/
44
 */
45
function ctools_plugin_example_context_create_simplecontext($empty, $data = NULL, $conf = FALSE) {
46
  $context = new ctools_context('simplecontext');
47
  $context->plugin = 'simplecontext';
48

    
49
  if ($empty) {
50
    return $context;
51
  }
52

    
53
  if ($conf) {
54
    if (!empty($data)) {
55
      $context->data = new stdClass();
56
      // For this simple item we'll just create our data by stripping non-alpha and
57
      // adding '_from_configuration_item_1' to it.
58
      $context->data->item1 = t("Item1");
59
      $context->data->item2 = t("Item2");
60
      $context->data->description = preg_replace('/[^a-z]/i', '', $data['sample_simplecontext_setting']);
61
      $context->data->description .= '_from_configuration_sample_simplecontext_setting';
62
      $context->title = t("Simplecontext context from config");
63
      return $context;
64
    }
65
  }
66
  else {
67
    // $data is coming from an arg - it's just a string.
68
    // This is used for keyword.
69
    $context->title = $data;
70
    $context->argument = $data;
71
    // Make up a bogus context.
72
    $context->data = new stdClass();
73
    $context->data->item1 = t("Item1");
74
    $context->data->item2 = t("Item2");
75

    
76
    // For this simple item we'll just create our data by stripping non-alpha and
77
    // adding '_from_simplecontext_argument' to it.
78
    $context->data->description = preg_replace('/[^a-z]/i', '', $data);
79
    $context->data->description .= '_from_simplecontext_argument';
80
    $context->arg_length = strlen($context->argument);
81
    return $context;
82
  }
83
}
84

    
85
function simplecontext_settings_form($conf, $external = FALSE) {
86
  if (empty($conf)) {
87
    $conf = array(
88
      'sample_simplecontext_setting' => 'default simplecontext setting',
89
    );
90
  }
91
  $form = array();
92
  $form['sample_simplecontext_setting'] = array(
93
    '#type' => 'textfield',
94
    '#title' => t('Setting for simplecontext'),
95
    '#size' => 50,
96
    '#description' => t('An example setting that could be used to configure a context'),
97
    '#default_value' => $conf['sample_simplecontext_setting'],
98
    '#prefix' => '<div class="clear-block no-float">',
99
    '#suffix' => '</div>',
100
  );
101
  return $form;
102
}
103

    
104
/**
105
 * Provide a list of sub-keywords.
106
 *
107
 * This is used to provide keywords from the context for use in a content type,
108
 * pane, etc.
109
 */
110
function simplecontext_convert_list() {
111
  return array(
112
    'item1' => t('Item1'),
113
    'item2' => t('Item2'),
114
    'description' => t('Description'),
115
  );
116
}
117

    
118
/**
119
 * Convert a context into a string to be used as a keyword by content types, etc.
120
 */
121
function simplecontext_convert($context, $type) {
122
  switch ($type) {
123
    case 'item1':
124
      return $context->data->item1;
125

    
126
    case 'item2':
127
      return $context->data->item2;
128

    
129
    case 'description':
130
      return $context->data->description;
131
  }
132
}