Projet

Général

Profil

Paste
Télécharger (4,49 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / ctools_plugin_example / plugins / content_types / simplecontext_content_type.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 * Sample ctools content type that takes advantage of context.
6
 *
7
 * This example uses the context it gets (simplecontext) to demo how a
8
 * ctools content type can access and use context. Note that the simplecontext
9
 * can be either configured manually into a panel or it can be retrieved via
10
 * an argument.
11
 */
12

    
13
/**
14
 * Plugins are described by creating a $plugin array which will be used
15
 * by the system that includes this file.
16
 */
17
$plugin = array(
18
  'title' => t('Simplecontext content type'),
19
  'content_types' => 'simplecontext_content_type',
20
  // 'single' means not to be subtyped.
21
  'single' => TRUE,
22
  // Name of a function which will render the block.
23
  'render callback' => 'simplecontext_content_type_render',
24

    
25
  // Icon goes in the directory with the content type.
26
  'icon' => 'icon_example.png',
27
  'description' => t('Simplecontext content type - works with a simplecontext context.'),
28
  'required context' => new ctools_context_required(t('Simplecontext'), 'simplecontext'),
29
  'edit form' => 'simplecontext_content_type_edit_form',
30
  'admin title' => 'ctools_plugin_example_simplecontext_content_type_admin_title',
31

    
32
  // Presents a block which is used in the preview of the data.
33
  // Pn Panels this is the preview pane shown on the panels building page.
34
  'admin info' => 'ctools_plugin_example_simplecontext_content_type_admin_info',
35
  'category' => array(t('CTools Examples'), -9),
36
);
37

    
38
function ctools_plugin_example_simplecontext_content_type_admin_title($subtype, $conf, $context = NULL) {
39
  $output = t('Simplecontext');
40
  if ($conf['override_title'] && !empty($conf['override_title_text'])) {
41
    $output = filter_xss_admin($conf['override_title_text']);
42
  }
43
  return $output;
44
}
45

    
46
/**
47
 * Callback to provide administrative info (the preview in panels when building
48
 * a panel).
49
 *
50
 * In this case we'll render the content with a dummy argument and
51
 * a dummy context.
52
 */
53
function ctools_plugin_example_simplecontext_content_type_admin_info($subtype, $conf, $context = NULL) {
54
  $context = new stdClass();
55
  $context->data = new stdClass();
56
  $context->data->description = t("no real context");
57
  $block = simplecontext_content_type_render($subtype, $conf, array("Example"), $context);
58
  return $block;
59
}
60

    
61
/**
62
 * Run-time rendering of the body of the block (content type)
63
 *
64
 * @param $subtype
65
 * @param $conf
66
 *   Configuration as done at admin time
67
 * @param $args
68
 * @param $context
69
 *   Context - in this case we don't have any
70
 *
71
 * @return
72
 *   An object with at least title and content members
73
 */
74
function simplecontext_content_type_render($subtype, $conf, $args, $context) {
75
  $data = $context->data;
76
  $block = new stdClass();
77

    
78
  // Don't forget to check this data if it's untrusted.
79
  // The title actually used in rendering.
80
  $block->title = "Simplecontext content type";
81
  $block->content = t("
82
    This is a block of data created by the Simplecontext content type.
83
    Data in the block may be assembled from static text (like this) or from the
84
    content type settings form (\$conf) for the content type, or from the context
85
    that is passed in. <br />
86
    In our case, the configuration form (\$conf) has just one field, 'config_item_1;
87
    and it's configured with:
88
    ");
89
  if (!empty($conf)) {
90
    $block->content .= '<div style="border: 1px solid red;">' . print_r(filter_xss_admin($conf['config_item_1']), TRUE) . '</div>';
91
  }
92
  if (!empty($context)) {
93
    $block->content .= '<br />The args ($args) were <div style="border: 1px solid yellow;" >' .
94
      var_export($args, TRUE) . '</div>';
95
  }
96
  $block->content .= '<br />And the simplecontext context ($context->data->description) was <div style="border: 1px solid green;" >' .
97
    print_r($context->data->description, TRUE) . '</div>';
98
  return $block;
99
}
100

    
101
/**
102
 * 'Edit' callback for the content type.
103
 * This example just returns a form.
104
 */
105
function simplecontext_content_type_edit_form($form, &$form_state) {
106
  $conf = $form_state['conf'];
107
  $form['config_item_1'] = array(
108
    '#type' => 'textfield',
109
    '#title' => t('Config Item 1 for simplecontext content type'),
110
    '#size' => 50,
111
    '#description' => t('The stuff for item 1.'),
112
    '#default_value' => !empty($conf['config_item_1']) ? $conf['config_item_1'] : '',
113
    '#prefix' => '<div class="clear-block no-float">',
114
    '#suffix' => '</div>',
115
  );
116

    
117
  return $form;
118
}
119

    
120
function simplecontext_content_type_edit_form_submit($form, &$form_state) {
121
  foreach (element_children($form) as $key) {
122
    if (!empty($form_state['values'][$key])) {
123
      $form_state['conf'][$key] = $form_state['values'][$key];
124
    }
125
  }
126
}