Skip to content

Instantly share code, notes, and snippets.

@lefnire
Created February 17, 2012 18:45
Show Gist options
  • Save lefnire/1854789 to your computer and use it in GitHub Desktop.
Save lefnire/1854789 to your computer and use it in GitHub Desktop.
InsertNode 7 Snippet (requires token_filter)
<?php
/**
* Implements hook_tokens().
* This is used to insert nodes or node-tokens into content with token_filter,
* @see http://drupal.org/node/902244#comment-5616694
*/
function sharpe_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
// node.tokens.inc lets you work with the current-node's tokens,
// but not a different node's. This pulls in external node info
if ($type == 'node') {
foreach ($tokens as $token => $full) { // gives 1:title => [node:1:title]
$parts = explode(':', $token, 2);
if (count($parts) == 2) {
list($nid, $attr) = $parts;
// is this [node:1:title] instead of [node:title]?
if (is_numeric($nid)) {
// EITHER Render the node passing in build-mode, e.g. [node:1:node_view:full]
$parts = explode(':', $attr, 2);
if ( count($parts) == 2 && $parts[0] == 'node_view' ) {
$replacements[$full] = drupal_render( node_view(node_load($nid), $parts[1]) );
}
// OR Replace the node's token
else {
$new_token = array( $attr => "[node:$attr]" );
$node_replacements = token_generate($type, $new_token, $data = array('node' => node_load($nid)), $options);
// get first (there's only one if successful)
if ($replacement = array_pop($node_replacements) ) {
$replacements[$full] = $replacement;
}
}
}
}
}
}
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment