Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
S
simple_sitemap
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
3
Merge Requests
3
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
simple_sitemap
Commits
da5c237a
Commit
da5c237a
authored
Jul 13, 2019
by
Pawel G
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#3062820
by nadavoid, gbyte.co: Enable addition of custom XML nodes to default sitemap
parent
53203d1b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
48 deletions
+89
-48
src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php
...mple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php
+89
-48
No files found.
src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php
View file @
da5c237a
...
...
@@ -39,6 +39,7 @@ class DefaultSitemapGenerator extends SitemapGeneratorBase {
/**
* DefaultSitemapGenerator constructor.
*
* @param array $configuration
* @param string $plugin_id
* @param mixed $plugin_definition
...
...
@@ -108,8 +109,18 @@ class DefaultSitemapGenerator extends SitemapGeneratorBase {
$this
->
writer
->
writeGeneratedBy
();
$this
->
writer
->
startElement
(
'urlset'
);
$this
->
addSitemapAttributes
();
$this
->
addLinks
(
$links
);
$this
->
writer
->
endElement
();
$this
->
writer
->
endDocument
();
return
$this
->
writer
->
outputMemory
();
}
// Add attributes to document.
/**
* Adds attributes to the sitemap.
*/
protected
function
addSitemapAttributes
()
{
$attributes
=
self
::
$attributes
;
if
(
!
$this
->
isHreflangSitemap
())
{
unset
(
$attributes
[
'xmlns:xhtml'
]);
...
...
@@ -119,75 +130,105 @@ class DefaultSitemapGenerator extends SitemapGeneratorBase {
foreach
(
$attributes
as
$name
=>
$value
)
{
$this
->
writer
->
writeAttribute
(
$name
,
$value
);
}
}
// Add URLs to document.
/**
* Adds URL elements to the sitemap.
*
* @param array $links
*/
protected
function
addLinks
(
array
$links
)
{
$sitemap_variant
=
$this
->
sitemapVariant
;
$this
->
moduleHandler
->
alter
(
'simple_sitemap_links'
,
$links
,
$sitemap_variant
);
foreach
(
$links
as
$link
)
{
// Add each translation variant URL as location to the sitemap.
foreach
(
$links
as
$url_data
)
{
$this
->
writer
->
startElement
(
'url'
);
$this
->
writer
->
writeElement
(
'loc'
,
$link
[
'url'
]);
// If more than one language is enabled, add all translation variant URLs
// as alternate links to this location turning the sitemap into a hreflang
// sitemap.
if
(
isset
(
$link
[
'alternate_urls'
])
&&
$this
->
isHreflangSitemap
())
{
foreach
(
$link
[
'alternate_urls'
]
as
$language_id
=>
$alternate_url
)
{
$this
->
writer
->
startElement
(
'xhtml:link'
);
$this
->
writer
->
writeAttribute
(
'rel'
,
'alternate'
);
$this
->
writer
->
writeAttribute
(
'hreflang'
,
$language_id
);
$this
->
writer
->
writeAttribute
(
'href'
,
$alternate_url
);
$this
->
writer
->
endElement
();
}
}
$this
->
addUrl
(
$url_data
);
$this
->
writer
->
endElement
();
}
}
// Add lastmod if any.
if
(
isset
(
$link
[
'lastmod'
]))
{
$this
->
writer
->
writeElement
(
'lastmod'
,
$link
[
'lastmod'
]);
}
/**
* Adds a URL element to the sitemap.
*
* @param array $url_data
* The array of properties for this URL.
*/
protected
function
addUrl
(
array
$url_data
)
{
$this
->
writer
->
writeElement
(
'loc'
,
$url_data
[
'url'
]);
// If more than one language is enabled, add all translation variant URLs
// as alternate links to this link turning the sitemap into a hreflang
// sitemap.
if
(
isset
(
$url_data
[
'alternate_urls'
])
&&
$this
->
isHreflangSitemap
())
{
$this
->
addAlternateUrls
(
$url_data
[
'alternate_urls'
]);
}
// Add changefreq
if any.
if
(
isset
(
$link
[
'changefreq
'
]))
{
$this
->
writer
->
writeElement
(
'changefreq'
,
$link
[
'changefreq
'
]);
}
// Add lastmod
if any.
if
(
isset
(
$url_data
[
'lastmod
'
]))
{
$this
->
writer
->
writeElement
(
'lastmod'
,
$url_data
[
'lastmod
'
]);
}
// Add priority
if any.
if
(
isset
(
$link
[
'priority
'
]))
{
$this
->
writer
->
writeElement
(
'priority'
,
$link
[
'priority
'
]);
}
// Add changefreq
if any.
if
(
isset
(
$url_data
[
'changefreq
'
]))
{
$this
->
writer
->
writeElement
(
'changefreq'
,
$url_data
[
'changefreq
'
]);
}
// Add images if any.
if
(
!
empty
(
$link
[
'images'
]))
{
foreach
(
$link
[
'images'
]
as
$image
)
{
$this
->
writer
->
startElement
(
'image:image'
);
$this
->
writer
->
writeElement
(
'image:loc'
,
$image
[
'path'
]);
if
(
strlen
(
$image
[
'title'
])
>
0
)
{
$this
->
writer
->
writeElement
(
'image:title'
,
$image
[
'title'
]);
}
if
(
strlen
(
$image
[
'alt'
])
>
0
)
{
$this
->
writer
->
writeElement
(
'image:caption'
,
$image
[
'alt'
]);
}
$this
->
writer
->
endElement
();
// Add priority if any.
if
(
isset
(
$url_data
[
'priority'
]))
{
$this
->
writer
->
writeElement
(
'priority'
,
$url_data
[
'priority'
]);
}
// Add images if any.
if
(
!
empty
(
$url_data
[
'images'
]))
{
foreach
(
$url_data
[
'images'
]
as
$image
)
{
$this
->
writer
->
startElement
(
'image:image'
);
$this
->
writer
->
writeElement
(
'image:loc'
,
$image
[
'path'
]);
if
(
strlen
(
$image
[
'title'
])
>
0
)
{
$this
->
writer
->
writeElement
(
'image:title'
,
$image
[
'title'
]);
}
if
(
strlen
(
$image
[
'alt'
])
>
0
)
{
$this
->
writer
->
writeElement
(
'image:caption'
,
$image
[
'alt'
]);
}
$this
->
writer
->
endElement
();
}
}
}
/**
* Adds all translation variant URLs as alternate URLs to a URL.
*
* @param array $alternate_urls
*/
protected
function
addAlternateUrls
(
array
$alternate_urls
)
{
foreach
(
$alternate_urls
as
$language_id
=>
$alternate_url
)
{
$this
->
writer
->
startElement
(
'xhtml:link'
);
$this
->
addAlternateUrl
(
$language_id
,
$alternate_url
);
$this
->
writer
->
endElement
();
}
$this
->
writer
->
endElement
();
$this
->
writer
->
endDocument
();
}
return
$this
->
writer
->
outputMemory
();
/**
* Adds a translation variant URL as alternate URL to a URL.
*
* @param $language_id
* @param $alternate_url
*/
protected
function
addAlternateUrl
(
$language_id
,
$alternate_url
)
{
$this
->
writer
->
writeAttribute
(
'rel'
,
'alternate'
);
$this
->
writer
->
writeAttribute
(
'hreflang'
,
$language_id
);
$this
->
writer
->
writeAttribute
(
'href'
,
$alternate_url
);
}
/**
* Checks if sitemap is hreflang compliant.
*
* @return bool
*/
protected
function
isHreflangSitemap
()
{
if
(
NULL
===
$this
->
isHreflangSitemap
)
{
$this
->
isHreflangSitemap
=
count
(
array_diff_key
(
$this
->
languageManager
->
getLanguages
(),
$this
->
settings
[
'excluded_languages'
])
array_diff_key
(
$this
->
languageManager
->
getLanguages
(),
$this
->
settings
[
'excluded_languages'
])
)
>
1
;
}
return
$this
->
isHreflangSitemap
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment