summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.26/sites/all/modules/ctools/plugins/content_types/token/token.inc
blob: c43abd29c424a97151a2100f9b7116c5ef13f41a (plain)
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
<?php

/**
 * @file
 * Plugin automatically declare 'tokens' as plugins.
 */

/**
 * Plugin decleration.
 */
$plugin = array(
  'title' => t('Tokens'),
  'content type' => 'ctools_token_content_type_content_type',
  'defaults' => array('sanitize' => TRUE),
);

/**
 * Just one subtype.
 *
 * Ordinarily this function is meant to get just one subtype. However, we are
 * using it to deal with the fact that we have changed the subtype names. This
 * lets us translate the name properly.
 */
function ctools_token_content_type_content_type($subtype) {
  $types = ctools_token_content_type_content_types();
  if (isset($types[$subtype])) {
    return $types[$subtype];
  }
}

/**
 * Return all field content types available.
 */
function ctools_token_content_type_content_types() {
  // This will hold all the properties.
  $types = array();
  $info = token_info();

  foreach ($info['tokens'] as $entity_type => $tokens) {
    foreach ($tokens as $name => $token) {
      if (!empty($token['name'])) {
        $token += array('description' => '');
        $types[$entity_type . ':' . $name] = array(
          'category' => t('@entity (tokens)', array('@entity' => ucfirst($entity_type))),
          'icon' => 'icon_token.png',
          'title' => $token['name'],
          'description' => $token['description'],
          'required context' => new ctools_context_required(t(ucfirst($entity_type)), $entity_type),
        );
      }
    }
  }

  return $types;
}

/**
* Render the custom content type.
*/
function ctools_token_content_type_render($subtype, $conf, $panel_args, $context) {
  if (empty($context) || empty($context->data)) {
    return FALSE;
  }

  $sanitize = $conf['sanitize'];

  $entity = $context->data;
  list($entity_type, $name) = explode(':', $subtype, 2);

  $info = token_info();
  $values = token_generate($entity_type, array($name => $name), array($entity_type => $entity), array('sanitize' => $sanitize));
  if (!isset($values[$name])) {
    return;
  }

  // Build the content type block.
  $block = new stdClass();
  $block->module  = 'ctools';
  $block->title   = $info['tokens'][$entity_type][$name]['name'];
  $block->content = $values[$name];
  $block->delta   = str_replace('_', '-', $entity_type . '-' . $name);

  return $block;
}

/**
* Returns an edit form for custom type settings.
*/
function ctools_token_content_type_edit_form($form, &$form_state) {
  $conf = $form_state['conf'];

  $form['sanitize'] = array(
    '#type' => 'checkbox',
    '#default_value' => !empty($conf['sanitize']),
    '#title' => t('Sanitize'),
    '#description' => t('When enabled that output of the token will be stripped from dangerous HTML.'),
  );

  return $form;
}

/**
 * Validate the node selection.
 */
function ctools_token_content_type_edit_form_submit($form, &$form_state) {
  $form_state['conf']['sanitize'] = $form_state['values']['sanitize'];
}


/**
* Returns the administrative title for a type.
*/
function ctools_token_content_type_admin_title($subtype, $conf, $context) {
  return t('"@s" @name', array('@s' => $context->identifier, '@name' => $subtype));
}