View source
<?php
function link_node_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
t("Link Node Filter"),
);
case 'name':
return t("Link Node Filter");
case 'description':
return t("Allows you to create meta tags that link to other nodes.");
case 'settings':
return link_node_filter_settings();
case 'process':
return link_node_filter_process($text);
default:
return $text;
}
}
function link_node_filter_settings() {
$form['filter_link_node'] = array(
'#type' => 'fieldset',
'#title' => t("Link Node codes"),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['filter_link_node']['link_node'] = array(
'#type' => 'item',
'#title' => t('Instructions'),
'#value' => t('This filter translates special node tags into links to other nodes.
Syntax: [node:node_id,title="val2"]; every param but node_id is optional.
Each list below should be a comma separated list (no spaces) of what node properties users are
allowed to override for the purposes of displaying a node. E.g. <em>title,border</em>'),
);
foreach (node_get_types() as $type => $name) {
$form['filter_link_node']["link_node_allowed_vars_{$type}"] = array(
'#type' => 'textfield',
'#title' => t("Allowed parameters for %type nodes", array(
'%type' => $type,
)),
'#default_value' => variable_get("link_node_allowed_vars_{$type}", ""),
'#size' => 40,
'#maxlength' => 256,
);
}
return $form;
}
function link_node_filter_process($text, $show_error_msgs = 1) {
if (variable_get("link_node_enabled", 1)) {
$meta_tag_regexp = '/\\[node:(\\d+)([^\\]]*)\\]/i';
if (preg_match_all($meta_tag_regexp, $text, $match)) {
foreach ($match[1] as $key => $nid) {
if (module_exists('i18n') && module_exists('translation')) {
$lang = i18n_get_lang();
$transnids = translation_node_get_translations($nid);
if ($transnids[$lang]) {
$nid = $transnids[$lang]->nid;
}
}
$node = node_load(array(
"nid" => $nid,
));
if ($node) {
if (!node_access("view", $node)) {
$matches_html[$key] = t('You do not have access to view this node');
continue;
}
$meta_tag_params_regexp = '/,\\s*([\\w_-]+)\\s*=\\s*"([^"]+)"\\s*/';
$meta_tag_params_regexp2 = '/,\\s*([\\w_-]+)\\s*=\\s*"([^,]+)"\\s*/';
if (preg_match_all($meta_tag_params_regexp, $match[2][$key], $parm_match) == 0) {
$meta_tag_params_regexp = $meta_tag_params_regexp2;
}
if (preg_match_all($meta_tag_params_regexp, $match[2][$key], $parm_match)) {
$allowed_vars = explode(',', variable_get("link_node_allowed_vars_{$node->type}", ""));
for ($i = 0; $i < count($parm_match[1]); $i++) {
$parm = $parm_match[1][$i];
$val = $parm_match[2][$i];
if (in_array($parm, $allowed_vars)) {
$node->{$parm} = $val;
}
else {
if ($show_error_msgs) {
drupal_set_message("ignoring param '{$parm}' for " . $node->type . " as it is not valid. Possible values are '<code>" . variable_get("link_node_allowed_vars_{$node->type}", "") . "</code>'.");
}
}
}
}
$matches_html[$key] = theme_link_node_thumbnail($node);
}
else {
if ($show_error_msgs) {
$matches_html[$key] = theme("link_node_format", t("Nonexistent node nid: ") . "{$nid}.");
}
else {
$matches_html[$key] = "";
}
}
}
foreach ($match[1] as $key => $value) {
$mtch[] = $match[0][$key];
$repl[] = $matches_html[$key];
}
$text = str_replace($mtch, $repl, $text);
}
}
return $text;
}
function theme_link_node_thumbnail(&$node) {
$main = TRUE;
if ($node->type == "image") {
if (function_exists("image_link_node_thumbnail")) {
return image_link_node_thumbnail($node);
}
}
$output = l($node->title, drupal_get_path_alias('node/' . $node->nid), array(
'absolute' => TRUE,
));
return $output;
}
function theme_link_node_format($content) {
return '<div class="link_node">' . $content . "</div>\n";
}
function link_node_nodeapi(&$node, $op) {
if ($op == "update") {
cache_clear_all('*', 'cache_filter', TRUE);
}
}
function link_node_help($path, $arg) {
$output = "";
switch ($path) {
case 'admin/help#link_node':
$output = t('<p><strong>Nodes That Link To Other Nodes</strong></p>
<p>
You can link nodes to other nodes using the following syntax:<br>
[node:<em>node_id</em>,title="val1"]<br><br>
</p>
<p>
Some examples:<br>
[node:123]<br>
[node:123,title="original"]<br>
</p><p>Currently available parameters:<table border="1">');
foreach (node_get_types() as $type => $name) {
$output .= "<tr><td>" . t("Allowed parameters for <strong>{$type}</strong> nodes: ") . "</td><td>" . variable_get("link_node_allowed_vars_{$type}", "") . "</td></tr>\n";
}
$output .= t('</table></p><p>
Site admin: If there are properties of a node that you want to allow your users to modify when referring to them,
you can specify those in the filter configuration.
</p>');
break;
}
return $output;
}
function link_node_filter_tips($delta, $format, $long = FALSE) {
if ($long) {
$txt = t('<strong>Linking Nodes To Other Nodes</strong>
<p>
You can link nodes to other nodes using the following syntax:<br>
[node:<em>node_id</em>,title="val2"]<br><br>
</p>
<p>
Some examples:<br>
[node:123]<br>
[node:123,title="original"]<br>
</p><p>Currently available parameters:<table border="1">');
foreach (node_get_types() as $type => $name) {
$txt .= "<tr><td>" . t("Allowed parameters for <strong>{$type}</strong> nodes: ") . "</td><td>" . variable_get("link_node_allowed_vars_{$type}", "") . "</td></tr>\n";
}
$txt .= t('</table></p><p>
Site admin: If there are properties of a node that you want to allow your users to modify when referring to them,
you can specify those in the filter configuration.
</p>');
return $txt;
}
else {
return t('You can link nodes to other nodes using the following syntax:<br>
[node:<em>node_id</em>,title="val2"]');
}
}
function link_node_theme() {
return array(
'link_node_thumbnail' => array(
'arguments' => array(
'node' => NULL,
),
),
'link_node_format' => array(
'arguments' => array(
'content' => NULL,
),
),
);
}