token.inc 3.2 KB

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