link_node.module in link node 7
Same filename and directory in other branches
Implements the link_node module.
File
link_node.moduleView source
<?php
/**
* @file
* Implements the link_node module.
*/
/**
* Implementation of hook_filter_info().
*/
function link_node_filter_info() {
$filters['filter_link_node'] = array(
'title' => t('Link Node'),
'description' => t('Insert automatically links to drupal nodes using [node:node_id,title="val2"] tags.'),
'process callback' => 'link_node_filter_link_node_process',
'settings callback' => 'link_node_filter_link_node_settings',
'tips callback' => 'link_node_filter_link_node_tips',
'cache' => false,
'weight' => -20,
);
foreach (node_type_get_types() as $type) {
$filters['filter_link_node']['filter_link_node_allowed_vars_' . $type->type] = array();
}
return $filters;
}
/**
* Implementation of hook_theme().
*/
function link_node_theme() {
return array(
'link_node_thumbnail' => array(
'variables' => array(
'node' => NULL,
),
),
'link_node_format' => array(
'variables' => array(
'content' => NULL,
),
),
);
}
/**
* Function used to discover what filters are supplied by this module.
*/
function link_node_filter_link_node_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
$elements['filter_link_node'] = array(
'#type' => 'item',
'#description' => 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_type_get_types() as $type) {
$elements["filter_link_node_allowed_vars_" . $type->type] = array(
'#type' => 'textfield',
'#title' => t("Allowed parameters for %type nodes", array(
'%type' => $type->type,
)),
'#default_value' => array_key_exists("filter_link_node_allowed_vars_{$type->type}", $filter->settings) ? $filter->settings['filter_link_node_allowed_vars_' . $type->type] : "",
'#size' => 40,
'#maxlength' => 256,
);
}
return $elements;
}
/**
* Actually execute filter on given text.
* Parse text for all meta tags.
* Lookup each specified node.
* Parse all parameters specified in each meta tag and apply them to the corresponding node.
* Replace meta tag with themed thumbnail view of the referrent node.
*/
function link_node_filter_link_node_process($text, $filter, $format, $langcode, $cache, $cache_id) {
if (variable_get("link_node_enabled", 1)) {
// *** [node:NID,a="b",c="d",width="40",etc="foo"] ***
// first we find every tag
$meta_tag_regexp = '/\\[node:(\\d+)([^\\]]*)\\]/i';
$meta_tag_params_regexp = '/,\\s*([\\w_-]+)\\s*=\\s*"([^"]+)"\\s*/';
$meta_tag_params_regexp2 = '/,\\s*([\\w_-]+)\\s*=\\s*"([^,]+)"\\s*/';
if (preg_match_all($meta_tag_regexp, $text, $match)) {
// then we make the html
foreach ($match[1] as $key => $nid) {
if (module_exists('i18n') && module_exists('translation')) {
$transnids = translation_node_get_translations($nid);
if (array_key_exists($langcode, $transnids)) {
$nid = $transnids[$langcode]->nid;
}
}
$node = node_load($nid);
if ($node) {
// check here to see if user has permissions to access this node
if (!node_access("view", $node)) {
$matches_html[$key] = t('You do not have access to view this node');
continue;
}
//drupal_set_message("found node $nid and it's of type $node->type");
// iterate over all name=value pairs and override $node values with them
if (preg_match_all($meta_tag_params_regexp, $match[2][$key], $parm_match) == 0) {
preg_match_all($meta_tag_params_regexp2, $match[2][$key], $parm_match);
}
//if (preg_match_all($meta_tag_params_regexp, $match[2][$key], $parm_match)) {
if (count($parm_match)) {
$allowed_vars = explode(',', $filter->settings["filter_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];
// check the name ($parm) to make sure it's an allowable value (like "align" or "border" or something)
if (in_array($parm, $allowed_vars)) {
$node->{$parm} = $val;
}
else {
drupal_set_message("ignoring param '{$parm}' for " . $node->type . " as it is not valid. Possible values are '<code>" . $filter->settings["filter_link_node_allowed_vars_{$node->type}"] . "</code>'.", 'warning');
}
}
}
$matches_html[$key] = theme('link_node_thumbnail', array(
'node' => $node,
));
}
else {
$output = theme("link_node_format", array(
'content' => t("Nonexistent node nid: ") . "{$nid}.",
));
drupal_set_message($output, 'warning');
$matches_html[$key] = $output;
}
}
// PHP (4.3.2) str_replace appears to use some internal order
// rather than array index when finding replace elements
// corresponding to search elements. To make sure that the
// order of the arrays correspond, we make new copies here.
foreach ($match[1] as $key => $value) {
$mtch[] = $match[0][$key];
$repl[] = $matches_html[$key];
}
$text = str_replace($mtch, $repl, $text);
}
}
return $text;
}
/**
* Generates a thumbnail view of a node.
*/
function theme_link_node_thumbnail($variables) {
$node = $variables['node'];
if ($node->type == "image") {
// offer theme developers and module developers to override this
if (function_exists("image_link_node_thumbnail")) {
return image_link_node_thumbnail($node);
}
}
$output = l($node->title, "node/" . $node->nid, array(
'class' => 'link-node',
));
return $output;
}
/**
* Puts a box around the attached_node's content
*/
function theme_link_node_format($variables) {
return '<div class="attached_node">' . $variables['content'] . "</div>\n";
}
/**
* Generates help text for this module
*/
function link_node_help($path, $arg) {
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>
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 $output;
}
}
/**
* Implementation of hook_filter_tips().
*/
function link_node_filter_link_node_tips($filter, $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_type_get_types() as $type) {
$txt .= "<tr><td>" . t('Allowed parameters for <strong>' . $type->type . '</strong> nodes: ') . "</td><td>" . $filter->settings['filter_link_node_allowed_vars_' . $type->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"]');
}
}
Functions
Name![]() |
Description |
---|---|
link_node_filter_info | Implementation of hook_filter_info(). |
link_node_filter_link_node_process | Actually execute filter on given text. Parse text for all meta tags. Lookup each specified node. Parse all parameters specified in each meta tag and apply them to the corresponding node. Replace meta tag with themed thumbnail view of the referrent node. |
link_node_filter_link_node_settings | Function used to discover what filters are supplied by this module. |
link_node_filter_link_node_tips | Implementation of hook_filter_tips(). |
link_node_help | Generates help text for this module |
link_node_theme | Implementation of hook_theme(). |
theme_link_node_format | Puts a box around the attached_node's content |
theme_link_node_thumbnail | Generates a thumbnail view of a node. |