3

I'm building a page where, somewhere down in the middle of it, there's a pager of some stuff. The pager is working fine, but, when you click on one of the pager items, the page is redrawn showing the top of the page, which usually does not include the (now updated) contents of the pager. This is because the pager item URLs simply refer to the page as a whole. Is it possible to get a reference to an anchor inserted into the URLs of the pager items, so that the browser will redraw the page with the pager contents in view?

4

Have you looked at theme_pager_link in Drupal 7. Your site's theme can customize any pager link by implementing YOURTHEME_pager_link and then add additional logic into a copied version of that function to embed anchors as needed.

You can probably embed the anchor as a parameter to the link to be inserted into the querystring:

parameters: An associative array of query string parameters to append to the pager link.

  • very interesting -- I'll check that out and report back. Thanks! – Jim Miller Aug 2 '13 at 21:56
  • Well, this does seem like the right area, but I can't figure out (nor find any refs to) how to get parameters into the MYTHEME_pager_link function. I can get a "fragment" anchor into the links produced by MYTHEME_pager_link, but I need to know what part of my code is making the pager, or I'll have the anchors attached to every pager on my site, which is obviously wrong. I could try to dissect the page's path from $_GET['q'] and do something based on that, but that's troublesome, and won't handle the case of two pagers on a page. So: tips on passing stuff into MYTHEME_pager_link? V. mysterious. – Jim Miller Aug 2 '13 at 23:47
  • Dull ax approach, I would look at overloading the $element argument, eg, initially creating this paged content with that set to something not found elsewhere on your site (like some outrageous number like 228 or something) and then keying in on that here. – Jimajamma Aug 3 '13 at 0:40
  • So, @Jimajamma, there's a DullAxes'R'Us near you, too? :) I was thinking about that same thing -- setting $element to 100 or something -- but the URLs on the links got SO ugly (add one hundred "%2C1"s to the URL) that I had a temporary outbreak of taste, and couldn't do it. But I may have to retreat to it, perhaps with a smaller value... :) – Jim Miller Aug 3 '13 at 3:55
0

The answer by @tenken although in the right direction, you don’t have any context within that function to target ONLY the comment pagers. Actually as far as I know you cannot only affect certain pagers period.

The way you could go about it is by using js, and target only the comment pagers:

// add js to insert anchor links to #comments in comment pagers
drupal_add_js("
  Drupal.behaviors.comment_pager_anchors = function (context) {
    var comment_pager_links = $('#comments .pager a');
    comment_pager_links.each(function(){
      this.href = this.href + '#comments';
    });
  }
", 'inline', 'footer');

You could add this for example within theme_pager, theme_pager_link, or in hook_nodeapi if you have comments for example.

  • note this comment is for Drupal 6... :s – Manuel Garcia Nov 22 '13 at 14:08
0

Based on Manuel Garcia's answer I was able to make this work in Drupal 7 with the following JavaScript:

Drupal.behaviors.pager_fragments = {
  attach: function (context, settings) {
    var pager_links = jQuery('#my-wrapper-id .pager a');
    pager_links.each(function(){
      this.href = this.href + '#myfragment';
    });
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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