function smart_paging_html_head_alter in Smart Paging 7.2
Same name and namespace in other branches
- 7 smart_paging.module \smart_paging_html_head_alter()
Implements hook_html_head_alter(). Smart Paging as better SEO friendly targeting meta tags
File
- ./
smart_paging.module, line 1159 - Provides smart paging capability to Drupal contents.
Code
function smart_paging_html_head_alter(&$head_elements) {
global $base_root;
if (isset($head_elements['smart_paging_link_canonical'])) {
// Process the canonical.
$smart_paging_canonical['smart_paging_link_canonical'] = $head_elements['smart_paging_link_canonical'];
unset($head_elements['smart_paging_link_canonical']);
$current_alias = function_exists('path_alias_xt_get_path_alias') ? path_alias_xt_get_path_alias($_GET['q']) : drupal_get_path_alias($_GET['q']);
$current_absolute_alias = url($_GET['q'], array(
'absolute' => TRUE,
));
$override_canonical = array();
// Collect other existing canonical element(s)
foreach ($head_elements as $head_element_key => $head_element) {
$canonical_tag = isset($head_element['#tag']) && $head_element['#tag'] == 'link' && isset($head_element['#attributes']) && $head_element['#attributes']['rel'] == 'canonical';
$canonical_metatag = $head_element_key == 'metatag_canonical';
// Drupal standard canonical link style plus metatag edge case - does not overwrite standard drupal
if (strpos($head_element_key, 'drupal_add_html_head_link:canonical') === 0 || $canonical_tag || $canonical_metatag) {
// If other canonicals are different from the base url (page1), collect them.
// Check absolute url alias for page as well, this should allow Domain Source, or other absolute, provided canonical to be authoratative.
if ($canonical_tag && $head_element['#attributes']['href'] != $current_alias && $head_element['#attributes']['href'] != $current_absolute_alias || $canonical_metatag && $head_element['#value'] != $current_alias && $head_element['#value'] != $current_absolute_alias) {
$override_canonical[$head_element_key] = $head_element;
}
unset($head_elements[$head_element_key]);
}
}
// If there are existing canonicals that don't match the base url, use them
// We are erring on the side that if another module is specifying a canonical different from this page's root (page1) then we shouldn't change it.
if (!empty($override_canonical)) {
$head_elements = array_merge($head_elements, $override_canonical);
}
else {
// Otherwise use the smart_paging canonical
$head_elements = array_merge($head_elements, $smart_paging_canonical);
// Remove any other canonicals already in the http response
// Here we are assuming that if the canonicals in the <head> element are ok to override, so is the http header
$http_header_link = drupal_get_http_header('Link');
$http_header_link = explode(',', $http_header_link);
foreach ($http_header_link as $key => $link) {
if (preg_match('#rel="canonical"#i', $link)) {
unset($http_header_link[$key]);
}
}
drupal_add_http_header('Link', implode(',', $http_header_link));
}
}
if (isset($head_elements['smart_paging_seo'])) {
$smart_paging_seo = json_decode($head_elements['smart_paging_seo']['#attributes']['content']);
$tags = array();
if (!empty($smart_paging_seo)) {
foreach ($smart_paging_seo as $tag => $tag_content) {
$target_tag = explode('_', $tag);
if (isset($target_tag[0]) && isset($target_tag[1]) && isset($target_tag[2]) && isset($target_tag[3])) {
$tags[$target_tag[2]] = array(
'attr_name' => $target_tag[1],
'tag' => $target_tag[0],
'content_attr' => $target_tag[3],
'content' => $tag_content,
);
}
}
}
if (!empty($tags)) {
foreach ($head_elements as $head_element_key => $head_element) {
foreach ($tags as $target_name => $attr) {
if ($head_element['#tag'] == $attr['tag'] && (isset($head_element['#attributes'][$attr['attr_name']]) && $head_element['#attributes'][$attr['attr_name']] == $target_name)) {
if (!empty($attr['content'])) {
$head_elements[$head_element_key]['#attributes'][$attr['content_attr']] = $attr['content'];
}
else {
unset($head_elements[$head_element_key]);
}
unset($tags[$target_name]);
}
}
}
}
unset($head_elements['smart_paging_seo']);
if (!empty($tags)) {
foreach ($tags as $target_name => $attr) {
$head_elements["smart_paging_{$target_name}"] = array(
'#type' => 'html_tag',
'#tag' => $attr['tag'],
'#attributes' => array(
$attr['attr_name'] => $target_name,
$attr['content_attr'] => $attr['content'],
),
);
unset($tags[$target_name]);
}
}
}
}