Index: linodef.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/linodef/linodef.module,v retrieving revision 1.1.2.12 diff -u -r1.1.2.12 linodef.module --- linodef.module 17 Dec 2009 23:23:04 -0000 1.1.2.12 +++ linodef.module 1 Jan 2010 15:59:59 -0000 @@ -10,6 +10,7 @@ define('LINODEF_FORMAT_TITLE_NODE', t('Node') . ' [nid]: [title]'); define('LINODEF_FORMAT_TITLE_TERM', t('Shows a teaser list of nodes using term !term', array('!term' => '[cat-raw]'))); define('LINODEF_FORMAT_TITLE_VIEW', t('Show !view', array('!view' => '[viewname]'))); +define('LINODEF_JOKER', '#'); /** * @file @@ -51,10 +52,17 @@ // the old method of the override option, so valid bytes U+10004C & U+1003CD // have been used. case 'prepare': - return preg_replace('@<#([0-9]+)>(.*?)@s', "\xf4\x80\x81\x8c#$1\xf4\x80\x8f\x8d$2\xf4\x80\x81\x8c/#\xf4\x80\x8f\x8d", $text); + $text = preg_replace('@<#([0-9]+)>(.*?)@s', "\xf4\x80\x81\x8c#$1\xf4\x80\x8f\x8d$2\xf4\x80\x81\x8c/#\xf4\x80\x8f\x8d", $text); + // Replace joker when previewing (nodeapi->load would replace joker also in textfields). + if (arg(0) == 'node' && arg(2) == 'edit' && is_numeric(arg(1))) { + $node = node_load(arg(1)); + $text = linodef_joker($text, $node); + } + return $text; case 'process': // Check if the current node is in teaser of full view. + // Major downside of this method: In a view teaser is set always. $buildmode = (arg(0) == 'node' && is_numeric(arg(1))) ? 'full' : 'teaser'; define('LINODEF_BUILDMODE', $buildmode); @@ -129,6 +137,54 @@ } /** + * Implementation of hook_nodeapi. + * + * Load joker replacements on node load. + */ +function linodef_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { + switch ($op) { + case 'load': + // No replacement when editing or adding translation. + if (arg(0) == 'node' && (arg(2) == 'edit' || arg(1) == 'add')) { + break; + } + // Debug: print('
');print_r($node);print('
'); + // Alter body & teaser. Check if linodef is activated in the used format. + if (array_key_exists('linodef/0', filter_list_format($node->format))) { + $node->body = linodef_joker($node->body, $node); + $node->teaser = linodef_joker($node->teaser, $node); + } + // Alter fields. + foreach($node as $key => &$value) { + // Check each field. + if (substr($key, 0, 6) == 'field_' && isset($value[0]['format'])) { + // Check each field value. + foreach ($value as &$field) { + // Check if linodef is activated in the current field format. + if (array_key_exists('linodef/0', filter_list_format($field['format'])) && !empty($field['value'])) { + $field['value'] = linodef_joker($field['value'], $node); + } + } + } + } + break; + } +} + +/** + * Replaces the joker character with content. + */ +function linodef_joker($text, $node) { + $joker = array( + // Add id of current node. + '[#' . LINODEF_JOKER => '[#' . $node->nid, + // Add language of current node. + ',translation="' . LINODEF_JOKER . '"' => ',translation="' . $node->language . '"', + ); + return str_replace(array_keys($joker), array_values($joker), $text); +} + +/** * Implementation of hook_filter_tips. */ function linodef_filter_tips($delta, $format, $long = FALSE) {