Skip to content
Snippets Groups Projects
Commit c74d2a34 authored by Tim Steiner's avatar Tim Steiner
Browse files

[gh-124] Add a input filter that allows the embedding of content from nodes...

[gh-124] Add a input filter that allows the embedding of content from nodes inside of nodes by using tags.
[gh-124] Add support for the <!--#include virtual="some/path"--> SSI directive as a filter.
parent ced5da35
No related branches found
No related tags found
No related merge requests found
......@@ -1513,3 +1513,130 @@ function unl_query_alter(QueryAlterableInterface $query) {
}
}
}
/**
* Implementation of hook_filter_info()
*/
function unl_filter_info()
{
return array(
'unl_embed' => array(
'title' => 'UNL Node Embed',
'description' => "Allow a node's body to be embedded into another node by using tags.",
'process callback' => 'unl_filter_embed_process',
'cache' => FALSE,
),
'unl_ssi' => array(
'title' => 'UNL SSI',
'description' => 'Implements the <!--#include virtual="some/path"--> Server Side Include directive.',
'process callback' => 'unl_filter_ssi_process',
'cache' => FALSE,
),
);
}
/**
* Implementation of hook_filter_FILTER_process pseudo-hook
*
* Replace any instances of [[node:X]] in the $text with the content of node X's body.
*/
function unl_filter_embed_process($text, $filter, $format, $langcode, $cache, $cache_id)
{
static $processed_hashes = array();
$text_hash = hash('sha256', $text);
if (in_array($text_hash, array_keys($processed_hashes))) {
// Possible recursion detected, return the cache result.
return $processed_hashes[$text_hash];
}
// In case of recursion, set the cached result to this until we have the real result.
$processed_hashes[$text_hash] = 'Error: Cannot embed a node in itself..';
$matches = NULL;
preg_match_all('/\[\[node:([0-9]+)\]\]/', $text, $matches);
$node_ids = $matches[1];
$nodes = entity_load('node', $node_ids);
$replace_array = array();
foreach ($node_ids as $node_id) {
$content = node_view($nodes[$node_id]);
$replace_array["[[node:$node_id]]"] = PHP_EOL . "<!-- Node $node_id start -->" . PHP_EOL
. render($content['body'])
. PHP_EOL . "<!-- Node $node_id end -->" . PHP_EOL;
}
$text = strtr($text, $replace_array);
// Set the cached result to the real result.
$processed_hashes[$text_hash] = $text;
return $text;
}
/**
* Implementation of hook_filter_FILTER_process pseudo-hook
*
* Replace any instances of <!--#include virtual="url"--> with the content
* found at that URL. If the url is in the unl.edu domain, format=partial
* will be added to the query string.
*/
function unl_filter_ssi_process($text, $filter, $format, $langcode, $cache, $cache_id) {
$matches = NULL;
preg_match_all('/<!-- *#include +virtual=((".*")|(\'.*\')) *-->/', $text, $matches);
$replacements = array();
foreach ($matches[1] as $match_index => $match) {
$full_match = $matches[0][$match_index];
// Break down the URL target then rebuild it as absolute.
$url = substr($match, 1, -1);
$parts = parse_url($url);
if (!isset($parts['scheme'])) {
$parts['scheme'] = $_SERVER['HTTPS'] ? 'https' : 'http';
}
if (!isset($parts['host'])) {
$parts['host'] = $_SERVER['HTTP_HOST'];
}
if (isset($parts['path']) && substr($parts['path'], 0, 1) != '/') {
if (variable_get('unl_use_base_tag')) {
$parts['path'] = $GLOBALS['base_path'] . $parts['path'];
} else {
$parts['path'] = $GLOBALS['base_path'] . request_path() . '/' . $parts['path'];
}
}
if (!isset($parts['path'])) {
$parts['path'] = '/';
}
$url = $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
// If this is a request to another UNL site, add format=partial to the query.
if (substr($parts['host'], -7) == 'unl.edu') {
if (isset($parts['query']) && $parts['query']) {
$parts['query'] .= '&';
} else {
$parts['query'] = '';
}
$parts['query'] .= 'format=partial';
}
// Finish rebuilding the URL.
if (isset($parts['query'])) {
$url .= '?' . $parts['query'];
}
if (isset($parts['fragment'])) {
$url .= '#' . $parts['fragment'];
}
$content = file_get_contents($url);
$replacements[$full_match] = PHP_EOL
. '<!-- Begin content from ' . $url . ' -->' . PHP_EOL
. $content . PHP_EOL
. '<!-- End content from ' . $url . ' -->' . PHP_EOL;
}
foreach ($replacements as $from => $to) {
$text = str_replace($from, $to, $text);
}
return $text;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment