0

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?

2
  • 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
    – Clive
    Commented Aug 11, 2014 at 17:10
  • 1
    Thanks for the insight @Clive. I ended up using a combination of the hook and javascript: the hook to force non-aliased outbound URLs for pages with "/draft" at the end of the URL, and Javascript to append "/draft" to the appropriate links in the node content.
    – Ginosuave
    Commented Aug 11, 2014 at 19:29

1 Answer 1

0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.