Commit 571e79a9 authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #1370474 by carinadigital, chadedge, Dave Reid, Jon Nunan,...

Issue #1370474 by carinadigital, chadedge, Dave Reid, Jon Nunan, udhayykumar@gmail.com, eojthebrave: Added new hooks for altering alter generated link elements and root XML attributes.
parent c3fe4c26
......@@ -178,6 +178,56 @@ function hook_xmlsitemap_context_url_options(array $context) {
function hook_xmlsitemap_context_url_options_alter(array &$options, array $context) {
}
/**
* Alter the content added to an XML sitemap for an individual element.
*
* This hooks is called when the module is generating the XML content for the
* sitemap and allows other modules to alter existing or add additional XML data
* for any element by adding additional key value paris to the $element array.
*
* The key in the element array is then used as the name of the XML child
* element to add and the value is the value of that element. For example:
*
* @code $element['video:title'] = 'Big Ponycorn'; @endcode
*
* Would result in a child element like <video:title>Big Ponycorn</video:title>
* being added to the sitemap for this particular link.
*
* @param array $element
* The element that will be converted to XML for the link.
* @param array $link
* An array of properties providing context about the link that we are
* generating an XML element for.
* @param object $sitemap
* The sitemap that is currently being generated.
*/
function hook_xmlsitemap_element_alter(array &$element, array $link, $sitemap) {
if ($link['subtype'] === 'video') {
$node = node_load($link['id']);
$element['video:video'] = array(
'video:title' => check_plain($node->title),
'video:description' => isset($node->body[LANGUAGE_NONE][0]['summary']) ? check_plain($node->body[LANGUAGE_NONE][0]['summary']) : check_plain($node->body[LANGUAGE_NONE][0]['value']),
'video:live' => 'no',
);
}
}
/**
* Alter the attributes used for the root element of the XML sitemap.
*
* For example add an xmlns:video attribute:
* <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
*
* @param array $attributes
* An associative array of attributes to use in the root element of an XML
* sitemap.
* @param object $sitemap
* The sitemap that is currently being generated.
*/
function hook_xmlsitemap_root_attributes_alter(&$attributes, $sitemap) {
$attributes['xmlns:video'] = 'http://www.google.com/schemas/sitemap-video/1.1';
}
/**
* Alter the query selecting data from {xmlsitemap} during sitemap generation.
*
......
......@@ -150,7 +150,7 @@ function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer,
$link_count = 0;
$query = db_select('xmlsitemap', 'x');
$query->fields('x', array('loc', 'lastmod', 'changefreq', 'changecount', 'priority', 'language', 'access', 'status'));
$query->fields('x', array('id', 'type', 'subtype', 'loc', 'lastmod', 'changefreq', 'changecount', 'priority', 'language', 'access', 'status'));
$query->condition('x.access', 1);
$query->condition('x.status', 1);
$query->orderBy('x.language', 'DESC');
......@@ -207,6 +207,10 @@ function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer,
// sitemaps.org specification.
$element['priority'] = number_format($link['priority'], 1);
}
// @todo Should this be moved to XMLSitemapWritier::writeSitemapElement()?
drupal_alter('xmlsitemap_element', $element, $link, $sitemap);
$writer->writeSitemapElement('url', $element);
}
......
......@@ -64,6 +64,8 @@ function xmlsitemap_hook_info() {
'xmlsitemap_context_info_alter',
'xmlsitemap_context_url_options',
'xmlsitemap_context',
'xmlsitemap_element_alter',
'xmlsitemap_root_attributes_alter',
'xmlsitemap_sitemap_insert',
'xmlsitemap_sitemap_update',
'xmlsitemap_sitemap_operations',
......
......@@ -73,6 +73,9 @@ class XMLSitemapWriter extends XMLWriter {
$attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
$attributes['xsi:schemaLocation'] = 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd';
}
drupal_alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
return $attributes;
}
......@@ -169,6 +172,9 @@ class XMLSitemapIndexWriter extends XMLSitemapWriter {
$attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
$attributes['xsi:schemaLocation'] = 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd';
}
drupal_alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
return $attributes;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment