Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / plugins / content_types / token / token.inc @ 651307cd

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Plugin automatically declare 'tokens' as plugins.
6
 */
7
8
/**
9
 * Plugin decleration.
10
 */
11
$plugin = array(
12
  'title' => t('Tokens'),
13
  'content type' => 'ctools_token_content_type_content_type',
14
  'defaults' => array('sanitize' => TRUE),
15
);
16
17
/**
18
 * Just one subtype.
19
 *
20
 * Ordinarily this function is meant to get just one subtype. However, we are
21
 * using it to deal with the fact that we have changed the subtype names. This
22
 * lets us translate the name properly.
23
 */
24
function ctools_token_content_type_content_type($subtype) {
25
  $types = ctools_token_content_type_content_types();
26
  if (isset($types[$subtype])) {
27
    return $types[$subtype];
28
  }
29
}
30
31
/**
32
 * Return all field content types available.
33
 */
34
function ctools_token_content_type_content_types() {
35
  // This will hold all the properties.
36 3753f249 Assos Assos
  $types = &drupal_static(__FUNCTION__);
37
  if (isset($types)) {
38
    return $types;
39
  }
40
41 85ad3d82 Assos Assos
  $types = array();
42
  $info = token_info();
43
44
  foreach ($info['tokens'] as $entity_type => $tokens) {
45 3753f249 Assos Assos
    $category = t('@entity (tokens)', array('@entity' => ucfirst($entity_type)));
46
    $context = new ctools_context_required(t(ucfirst($entity_type)), $entity_type);
47 85ad3d82 Assos Assos
    foreach ($tokens as $name => $token) {
48
      if (!empty($token['name'])) {
49
        $token += array('description' => '');
50
        $types[$entity_type . ':' . $name] = array(
51 3753f249 Assos Assos
          'category' => $category,
52 85ad3d82 Assos Assos
          'icon' => 'icon_token.png',
53
          'title' => $token['name'],
54
          'description' => $token['description'],
55 3753f249 Assos Assos
          'required context' => $context,
56 85ad3d82 Assos Assos
        );
57
      }
58
    }
59
  }
60
61
  return $types;
62
}
63
64
/**
65
* Render the custom content type.
66
*/
67
function ctools_token_content_type_render($subtype, $conf, $panel_args, $context) {
68
  if (empty($context) || empty($context->data)) {
69
    return FALSE;
70
  }
71
72
  $sanitize = $conf['sanitize'];
73
74
  $entity = $context->data;
75
  list($entity_type, $name) = explode(':', $subtype, 2);
76
77
  $info = token_info();
78
  $values = token_generate($entity_type, array($name => $name), array($entity_type => $entity), array('sanitize' => $sanitize));
79
  if (!isset($values[$name])) {
80
    return;
81
  }
82
83
  // Build the content type block.
84
  $block = new stdClass();
85
  $block->module  = 'ctools';
86
  $block->title   = $info['tokens'][$entity_type][$name]['name'];
87
  $block->content = $values[$name];
88
  $block->delta   = str_replace('_', '-', $entity_type . '-' . $name);
89
90
  return $block;
91
}
92
93
/**
94
* Returns an edit form for custom type settings.
95
*/
96
function ctools_token_content_type_edit_form($form, &$form_state) {
97
  $conf = $form_state['conf'];
98
99
  $form['sanitize'] = array(
100
    '#type' => 'checkbox',
101
    '#default_value' => !empty($conf['sanitize']),
102
    '#title' => t('Sanitize'),
103
    '#description' => t('When enabled that output of the token will be stripped from dangerous HTML.'),
104
  );
105
106
  return $form;
107
}
108
109
/**
110
 * Validate the node selection.
111
 */
112
function ctools_token_content_type_edit_form_submit($form, &$form_state) {
113
  $form_state['conf']['sanitize'] = $form_state['values']['sanitize'];
114
}
115
116
117
/**
118
* Returns the administrative title for a type.
119
*/
120
function ctools_token_content_type_admin_title($subtype, $conf, $context) {
121
  return t('"@s" @name', array('@s' => $context->identifier, '@name' => $subtype));
122
}