Created
February 17, 2012 18:45
-
-
Save lefnire/1854789 to your computer and use it in GitHub Desktop.
InsertNode 7 Snippet (requires token_filter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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