linkit_node.module in Linkit 6
Same filename and directory in other branches
Extend Linkit with node links.
File
plugins/linkit_node/linkit_node.moduleView source
<?php
/**
* @file
* Extend Linkit with node links.
*/
/**
* Implementation of hook_linkit_load_plugins().
*/
function linkit_node_linkit_load_plugins($string) {
$matches = array();
$settings = variable_get('linkit_node', array());
// Prevent "PHP notice: Undefined variable"
_linkit_node_get_default_settings($settings);
$fields = array(
'n.nid',
'n.title',
);
// default fields
$from = array(
'{node} n',
);
// default from
$where = array();
// default where
if ($settings['display_settings']['content_type']) {
$from[] = 'INNER JOIN {node_type} nt ON nt.type = n.type';
$fields[] = 'nt.name AS content_type';
}
if ($settings['display_settings']['status']) {
$fields[] = 'n.status';
}
if ($settings['display_settings']['language']) {
$fields[] = 'n.language';
}
if ($settings['display_settings']['created']) {
$fields[] = 'n.created';
}
if ($settings['display_settings']['changed']) {
$fields[] = 'n.changed';
}
if ($settings['display_settings']['show_books'] && module_exists('book')) {
$from[] = 'LEFT JOIN {book} b ON b.nid = n.nid';
$fields[] = 'b.bid';
}
if (!$settings['display_settings']['show_unpublished']) {
$where[] = "AND n.status = 1";
}
// Prebuild the SQL query
$sql = array();
$sql[] = "SELECT %s";
$sql[] = "FROM " . implode(" ", $from);
$sql[] = "WHERE LOWER(n.title) LIKE LOWER('%%%s%%') %s";
// Content type check
if ($allowed_content_types = array_filter($settings['content_types'])) {
$allowed_content_types = implode("', '", $allowed_content_types);
// Special treatment!!
// If we push this to the $where array, the single-quotes will be
// escaped and result in a SQL error.
// I asked chx if its ok to use the content type machine name
// "as it is" in this query and he said ok and I trust him.
$sql[] = " AND n.type IN ('" . $allowed_content_types . "')";
}
// Get nodes
$result = db_query(db_rewrite_sql(implode(" ", $sql)), implode(",", $fields), $string, implode(" ", $where));
$i = 0;
while ($node = db_fetch_object($result)) {
$matches['node'][$i] = array(
'title' => $node->title,
'path' => 'internal:node/' . $node->nid,
'information' => array(
'type' => 'Node',
),
);
// Add the node nid
if ($settings['display_settings']['nid']) {
$matches['node'][$i]['information']['nid'] = $node->nid;
}
// Add the node content type
if ($settings['display_settings']['content_type']) {
$matches['node'][$i]['information']['content type'] = $node->content_type;
}
// Add the node status
if ($settings['display_settings']['status']) {
$matches['node'][$i]['information']['status'] = $node->status ? t('published') : t('unpublished');
}
// Add the node language
if ($settings['display_settings']['language']) {
$matches['node'][$i]['information']['language'] = $node->language ? $node->language : '-';
}
// Add the node created time
if ($settings['display_settings']['created']) {
$matches['node'][$i]['information']['created'] = $node->created ? format_date($node->created, 'small') : '-';
}
// Add the node changed time
if ($settings['display_settings']['changed']) {
$matches['node'][$i]['information']['changed'] = $node->changed ? format_date($node->changed, 'small') : '-';
}
// Add Title of the book a node belong to
if ($settings['display_settings']['show_books'] && $node->bid) {
$book_node = node_load($node->bid);
$matches['node'][$i]['information']['book'] = $book_node->title;
}
$i++;
}
return $matches;
}
/**
* Implementation of hook_linkit_get_search_styled_link().
*/
function linkit_node_linkit_get_search_styled_link($string) {
// Node links created with Linkit will always begin with "internal:"
if (strpos($string, 'internal:') === FALSE) {
return;
}
// Check to see that the link really is a node link
$splitted_string = explode('/', str_replace('internal:', '', $string));
if ($splitted_string[0] != 'node') {
return;
}
// This is a node link created with Linkit, try to grab the title and path now.
$result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.nid = %d"), $splitted_string[1]);
$node = db_fetch_object($result);
// No reault or no node was found
if (!$result || !$node) {
return;
}
return check_plain($node->title) . ' [path:internal:node/' . $node->nid . ']';
}
/**
* Implementation of hook_linkit_info_plugins().
*
* This is used by linkit_permissions
*/
function linkit_node_linkit_info_plugins() {
$return['linkit_node'] = array(
'type' => 'node',
);
return $return;
}
/**
* Prevent "PHP notice: Undefined variable" by merging the settings
* with the defule values
*/
function _linkit_node_get_default_settings(&$settings) {
// Merge settings with the default settings
$settings += array(
'display_settings' => array(),
'content_types' => array(),
);
$settings['display_settings'] += array(
'nid' => 0,
'content_type' => 0,
'status' => 0,
'language' => 0,
'created' => 0,
'changed' => 0,
'show_unpublished' => 0,
'show_books' => 0,
);
}
Functions
Name | Description |
---|---|
linkit_node_linkit_get_search_styled_link | Implementation of hook_linkit_get_search_styled_link(). |
linkit_node_linkit_info_plugins | Implementation of hook_linkit_info_plugins(). |
linkit_node_linkit_load_plugins | Implementation of hook_linkit_load_plugins(). |
_linkit_node_get_default_settings | Prevent "PHP notice: Undefined variable" by merging the settings with the defule values |