Drupal Answers Asked on December 6, 2021
In Drupal 7 I am trying to add rel=”nofollow” to ALL of the pager links in views. The closest I have got is to add the nofollow to next links, but I want it to include previous pages as well as “First” and “Last”.
P.S. We specifically want the rel=”nofollow” attribute to all pager links, we are not looking for other solutions (noindex, block with robots.txt, or rel=next/prev).
This is the code I am currently using:
function mytheme_pager_next($variables) {
$text = $variables['text'];
$element = $variables['element'];
$interval = $variables['interval'];
$parameters = $variables['parameters'];
global $pager_page_array, $pager_total;
$output = '';
// If we are anywhere but the last page
if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
$page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
// If the next page is the last page, mark the link as such.
if ($page_new[$element] == ($pager_total[$element] - 1)) {
$output = theme('pager_last', array('text' => $text, 'element' => $element, 'parameters' => $parameters,'attributes'=>array('rel'=>'nofollow')));
}
// The next page is not the last page.
else {
$output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters,'attributes'=>array('rel'=>'nofollow')));
}
}
return $output;
}
After trying several suggestions here and on Drupal forums I found they were all overly complicated and caused the 'white screen of death. Our final solution was to simply make a minor hack to drupal core includes/pager.inc found here: http://bizuns.com/drupal/rel-nofollow-views-pager-links We backed up the file before making the change and it's working great so far, if we upgrade drupal we'll just have to make a note to modify this later. Thanks again for the feedback. If anyone has a cleaner (and simple) method I would gladly try the snippet if you provide it.
Answered by Haz on December 6, 2021
Not sure of a hook right now for something cleaner. An alternative that will allow you to do what you want,
function hook_views_post_render(&$view, &$output, &$cache) {
dpm($output);
}
You can use a regex, something like,
(<li.*pager-item.*><a)(.*)
That will give you two groups. - Group one with every list of items and the opening tag of the anchor. - Group two with the rest of the line
You can use this to iterate over and add in between both groups,
rel="nofollow"
Of course, you post an answer and find some information. You may want to see if you find these hooks useful, https://drupal.stackexchange.com/a/140867
Answered by rovr138 on December 6, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP