I'm building a site with drafts/revisions as a major component. For now I'm using hook_url_outbound_alter to check if "/draft" is at the end of the current URL, and if so to append "/draft" to all paths. The problem is that it's also altering the Menu URLs. Is there any way that I can explicitly target links in the node body using hook_url_outbound_alter?
1 Answer
For the googlers:
Module:
/**
* Implements hook_url_outbound_alter().
*/
function draft_links_url_outbound_alter(&$path, &$options, $original_path) {
// Internal URLs only
if (!$options['external']) {
// Check if current page is draft page
if (preg_match('|/draft(/?)$|', current_path())) {
$options['alias'] = $path;
}
}
}
/**
* Implements hook_preprocess_page().
*/
function draft_links_preprocess_page(&$variables) {
if (preg_match('|/draft/?$|', current_path())) {
drupal_add_js(drupal_get_path('module', 'draft_links') .'/draft_links.js');
}
}
JavaScript:
(function ($) {
$(window).load(function() {
$('.node__content a').each(function(){
href = $(this).attr('href');
// Internal URLs only.
if (href.match(/^\/?[0-9a-zA-Z]*/) && !href.match(/^(https?:)?\/{2}/)) {
$(this).attr('href', href+'/draft');
};
});
});
})(jQuery);
hook_url_outbound_alter
responds to a request, it has no idea how that was request was invoked, much less have access to any other context...appending parameters to the URLs contained in the node body is about the only way you'd be able to pass that context through. Or do it with javascript maybe