Unverified Commit e614903a authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #1370474 by Dave Reid, WidgetsBurritos, Udhaya Kumar, chadedge,...

Issue #1370474 by Dave Reid, WidgetsBurritos, Udhaya Kumar, chadedge, carinadigital, pobster, Martin., Jon Nunan, eojthebrave, evanjenkins: Added new hooks for altering alter generated link elements and root XML attributes.
parent 9d7dd1fc
......@@ -2,11 +2,12 @@
namespace Drupal\xmlsitemap;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Component\Utility\Bytes;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Psr\Log\LoggerInterface;
......@@ -71,6 +72,13 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
*/
protected $logger;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a XmlSitemapGenerator object.
*
......@@ -78,16 +86,19 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
* The config factory object.
* @param \Drupal\Core\State\StateInterface $state
* The state handler.
* @param Drupal\Core\Language\LanguageManagerInterface $language_manager
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language Manager.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, LanguageManagerInterface $language_manager, LoggerInterface $logger) {
public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, LanguageManagerInterface $language_manager, LoggerInterface $logger, ModuleHandlerInterface $module_handler) {
$this->config = $config_factory->getEditable('xmlsitemap.settings');
$this->state = $state;
$this->languageManager = $language_manager;
$this->logger = $logger;
$this->moduleHandler = $module_handler;
}
/**
......@@ -221,7 +232,7 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
$query = db_select('xmlsitemap', 'x');
$query->fields('x', [
'loc', 'lastmod', 'changefreq', 'changecount', 'priority', 'language', 'access', 'status',
'loc', 'type', 'subtype', 'lastmod', 'changefreq', 'changecount', 'priority', 'language', 'access', 'status',
]);
$query->condition('x.access', 1);
$query->condition('x.status', 1);
......@@ -277,6 +288,10 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
// per the sitemaps.org specification.
$element['priority'] = number_format($link['priority'], 1);
}
// @todo Should this be moved to XMLSitemapWriter::writeSitemapElement()?
$this->moduleHandler->alter('xmlsitemap_element', $element, $link, $sitemap);
$writer->writeSitemapElement('url', $element);
}
......
......@@ -25,6 +25,8 @@ class XmlSitemapIndexWriter extends XmlSitemapWriter {
/**
* {@inheritdoc}
*
* @todo Should this call parent::getRootAttributes()?
*/
public function getRootAttributes() {
$attributes['xmlns'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
......@@ -32,6 +34,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::moduleHandler()->alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
return $attributes;
}
......
......@@ -137,7 +137,7 @@ class XmlSitemapWriter extends \XMLWriter {
$context = $this->sitemap->getContext();
$base_url = $scheme . '://' . $url_settings['domains'][$context['language']];
$xls_url = Url::fromRoute('xmlsitemap.sitemap_xsl');
$xls_url = $base_url . "/". $xls_url->getInternalPath();
$xls_url = $base_url . '/' . $xls_url->getInternalPath();
}
}
$this->writePi('xml-stylesheet', 'type="text/xsl" href="' . $xls_url . '"');
......@@ -156,6 +156,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::moduleHandler()->alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
return $attributes;
}
......
......@@ -7,8 +7,6 @@
* @ingroup xmlsitemap
*/
use Drupal\xmlsitemap\XmlSitemapInterface;
/**
* @addtogroup hooks
* @{
......@@ -181,6 +179,59 @@ function hook_xmlsitemap_context_url_options_alter(array &$options, array $conte
}
/**
* 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 \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
* The sitemap that is currently being generated.
*/
function hook_xmlsitemap_element_alter(array &$element, array $link, \Drupal\xmlsitemap\XmlSitemapInterface $sitemap) {
if ($link['subtype'] === 'video') {
$video = video_load($link['id']);
$element['video:video'] = [
'video:title' => \Drupal\Component\Utility\Html::escape($video->title),
'video:description' => \Drupal\Component\Utility\Html::escape($video->description),
'video:live' => 'no',
];
}
}
/**
* Alter the attributes used for the root element of the XML sitemap.
*
* For example add an xmlns:video attribute:
*
* @code
* <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="https://www.google.com/schemas/sitemap-video/1.1">
* @endcode
*
* @param array $attributes
* An associative array of attributes to use in the root element of an XML
* sitemap.
* @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
* The sitemap that is currently being generated.
*/
function hook_xmlsitemap_root_attributes_alter(array &$attributes, \Drupal\xmlsitemap\XmlSitemapInterface $sitemap) {
$attributes['xmlns:video'] = 'https://www.google.com/schemas/sitemap-video/1.1';
}
/**
* Alter the query selecting data from {xmlsitemap} during sitemap generation.
*
......@@ -217,10 +268,10 @@ function hook_xmlsitemap_sitemap_operations() {
* This hook is invoked from xmlsitemap_sitemap_delete_multiple() after the XML
* sitemap has been removed from the table in the database.
*
* @param Drupal\xmlsitemap\XmlSitemapInterface $sitemap
* @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap
* The XML sitemap object that was deleted.
*/
function hook_xmlsitemap_sitemap_delete(XmlSitemapInterface $sitemap) {
function hook_xmlsitemap_sitemap_delete(\Drupal\xmlsitemap\XmlSitemapInterface $sitemap) {
db_query("DELETE FROM {mytable} WHERE smid = '%s'", $sitemap->smid);
}
......
......@@ -93,6 +93,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',
......
services:
xmlsitemap_generator:
class: Drupal\xmlsitemap\XmlSitemapGenerator
arguments: ['@config.factory', '@state', '@language_manager', '@logger.channel.xmlsitemap']
arguments: ['@config.factory', '@state', '@language_manager', '@logger.channel.xmlsitemap', '@module_handler']
tags:
- { name: xmlsitemap_generator }
xmlsitemap.link_storage:
......
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