0

I have a problem with the way the Drupal 7 Ajax pager works on my website: when looking into the code, it creates links, then when one clicks on one of these links, the ajax.js catches this event and replaces "going to another page" with "loading a block with Ajax-retrieved content".

This causes an SEO problem. In fact, as the links for the Pager are declared like following:

<a href="current-page?page=1">1</a>

... it is therefore understood by crawlers as links to another page, even though they're not; during SEO analysis of my website, I get a Duplicate content between page "A" and page "A?page=1" notice.

Is there is a way to alter this behavior?
For example, how could I replace these links with elements that would have a dedicated attribute? I thought of replacing "a href=" with "span data-target=", for example, but I have no clue how to do this and didn't find any easy solution yet.

Would the amount of work be worth the gain in SEO?

~Stéphane

0

The way I fixed this was to take advantage of the way the Drupal/jQuery script for the "Views PHP" module's Ajax loading is working:

  • It looks for elements in the DOM with such structure : ul.pager > li > a
  • it listens to a click event and calculates the data to be retrieved based on the "href" tag.
  • It operates its magic on the DOM.

My tweak is to modify line 8 of the "ajax_view.js" file so it also looks for ul.pager > li > span elements. Then, I add a hook in my theme's "template.php" file with the following code:

function THEME_pager($variables) {
  $output = "";
  $items = array();
  $tags = $variables['tags'];
  $element = $variables['element'];
  $parameters = $variables['parameters'];
  $quantity = $variables['quantity'];

  global $pager_page_array, $pager_total;

  $pager_middle = ceil($quantity / 2);
  $pager_current = $pager_page_array[$element] + 1;
  $pager_first = $pager_current - $pager_middle + 1;
  $pager_last = $pager_current + $quantity - $pager_middle;
  $pager_max = $pager_total[$element];

  $i = $pager_first;
  if ($pager_last > $pager_max) {
    $i = $i + ($pager_max - $pager_last);
    $pager_last = $pager_max;
  }
  if ($i <= 0) {
    $pager_last = $pager_last + (1 - $i);
    $i = 1;
  }
  $li_previous = ($pager_current > 1) ? '<span class="progress-enabled" href="?page=' . ($pager_current - 2) . '%2C' . ($pager_current - 2) . '">' . (isset($tags[1]) ? $tags[1] : t('previous')) . '</span>' : NULL;
  $li_next = ($pager_current < $pager_max) ? '<span class="progress-enabled" href="?page=' . ($pager_current) . '%2C' . ($pager_current) . '">' . (isset($tags[3]) ? $tags[3] : t('next')) . '</span>' : NULL;
  if ($pager_total[$element] > 1) {
    if ($li_previous) {
      $items[] = array(
        'class' => array('prev'),
        'data' => $li_previous,
      );
    }
    if ($i != $pager_max) {
      if ($i > 1) {
        $items[] = array(
          'class' => array('pager-ellipsis', 'disabled'),
          'data' => '<span>…</span>',
        );
      }
      for (; $i <= $pager_last && $i <= $pager_max; $i++) {
        if ($i < $pager_current) {
          $items[] = array(
            'data' => '<span class="progress-enabled" href="?page=' . ($i - 1) . '%2C' . ($i - 1) . '">' . $i . '</span>'
          );
        }
        if ($i == $pager_current) {
          $items[] = array(
            'class' => array('active'),
            'data' => l($i, '#', array('fragment' => '', 'external' => TRUE)),
          );
        }
        if ($i > $pager_current) {
          $items[] = array(
            'data' => '<span class="progress-enabled" href="?page=' . ($i - 1) . '%2C' . ($i - 1) . '">' . $i . '</span>'
            );
        }
      }
      if ($i < $pager_max) {
        $items[] = array(
          'class' => array('pager-ellipsis', 'disabled'),
          'data' => '<span>…</span>',
        );
      }
    }
    if ($li_next) {
      $items[] = array(
        'class' => array('next'),
        'data' => $li_next,
      );
    }
    return '<div class="text-center">' . theme('item_list', array(
      'items' => $items,
      'attributes' => array('class' => array('pagination')),
    )) . '</div>';
  }
  return $output;
}

It works perfectly for my usage.

Note : I know a "href" tag is technically illegal in a "span" element, but I didn't want to dig too deep in Drupal's JS ;)

~Stéphane

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.