Drupal SEO: implementare rel next e prev

  • 10 Ago 2014
  • Drupal 7

Nei casi in cui un determinato contenuto di Drupal venga visualizzato in tante pagine, si consiglia di implementare gli elementi link di tipo "next" e "prev" per indicare la relazione tra gli URL dei componenti in una serie composta da più pagine.

In questi scenari la funzione theme_pager_link genera il codice HTML per ogni link di ogni pagina.

Ad esempio, se i link delle pagine di una vista sono questi:

http://www.example.com/viewpath?str=
http://www.example.com/viewpath?str=&page=1
http://www.example.com/viewpath?str=&page=2
http://www.example.com/viewpath?str=&page=3

Nella prima pagina si include dentro la sezione <head> il link alla seconda pagina

<link rel="next" href="http://www.example.com/viewpath?str=&page=1" />

Nella seconda pagina si include dentro la sezione <head> i link alla prima e terza pagina

<link rel="prev" href="http://www.example.com/viewpath?str=" />
<link rel="next" href="http://www.example.com/viewpath?str=&page=2" />

Nell'ultima pagina si include dentro la sezione <head> il link alla pagina precedente

<link rel="prev" href="http://www.example.com/viewpath?str=&page=3" />

Implementazione in Drupal

Per implementare i link dentro la sezione <head> bisogna aggiungere la funzione theme_pager_link (che si può copiare dal file includes/pager.inc) dentro il file template.php del theme attivo e cambiare il nome a nometemplate_pager_link. Dopo di che, con una piccola modifica sarà posibile aggiungere i link utilizzando la funzione drupal_add_html_head_link.

function ilnomedeltuotheme_pager_link($variables) {
  $text = $variables['text'];
  $page_new = $variables['page_new'];
  $element = $variables['element'];
  $parameters = $variables['parameters'];
  $attributes = $variables['attributes'];

  $page = isset($_GET['page']) ? $_GET['page'] : '';
  
  if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
    $parameters['page'] = $new_page;
  }

  $query = array();
  if (count($parameters)) {
    $query = drupal_get_query_parameters($parameters, array());
  }
  if ($query_pager = pager_get_query_parameters()) {
    $query = array_merge($query, $query_pager);
  }

  // Set each pager link title
  if (!isset($attributes['title'])) {
    static $titles = NULL;
    if (!isset($titles)) {
      $titles = array(
        t('« first') => t('Go to first page'),
        t('‹ previous') => t('Go to previous page'),
        t('next ›') => t('Go to next page'),
        t('last »') => t('Go to last page'),
      );
    }
    if (isset($titles[$text])) {
      $attributes['title'] = $titles[$text];
    }
    elseif (is_numeric($text)) {
      $attributes['title'] = t('Go to page @number', array('@number' => $text));
    }
  }

  $attributes['href'] = url($_GET['q'], array('query' => $query));
  if ($page+1 == $new_page){
    drupal_add_html_head_link(array('rel' => 'next', 'href' => $attributes['href']));
  }
  if ($page-1 == $new_page){
    drupal_add_html_head_link(array('rel' => 'prev', 'href' => $attributes['href']));
  }  
  return '<a' . drupal_attributes($attributes) . '>' . check_plain($text) . '</a>';
}