Skip to content

Commit

Permalink
Add option to add links programmatically from extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingajda committed Jun 3, 2018
1 parent a8eb784 commit 26c0626
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
25 changes: 25 additions & 0 deletions README.md
Expand Up @@ -44,6 +44,31 @@ sitemapXml:
Note, if you have a ContentType with the property `searchable: false`, that
content type will be ignored.

## Advanced links list control

If you have your own bundled extension you can add, remove or change links
before the sitemap is rendered. You need to subscribe to the
`SitemapEvents::AFTER_COLLECTING_LINKS` event. The object you will get is
an instance of `SitemapEvent` class which has a `getLinks` method that returns
a `MutableBag` object. The last one is an array-like list of links. See example:

```php
protected function subscribe($dispatcher)
{
$dispatcher->addListener(SitemapEvents::AFTER_COLLECTING_LINKS,
function ($event) {
/** @var SitemapEvent $event */
$links = $event->getLinks();
$links->add([
'link' => '/lorem-ipsum',
'title' => 'Hello World!',
'depth' => 1,
]);
}
);
}
```

## Sitemap stylesheets

You can customize the sitemap with an xslt stylesheet if you copy the `templates/sitemap_xml.twig`
Expand Down
17 changes: 17 additions & 0 deletions src/SitemapEvent.php
@@ -0,0 +1,17 @@
<?php

namespace Bolt\Extension\Bolt\Sitemap;

use Bolt\Collection\MutableBag;
use Symfony\Component\EventDispatcher\GenericEvent;

class SitemapEvent extends GenericEvent
{
/**
* @return MutableBag
*/
public function getLinks()
{
return $this->subject;
}
}
17 changes: 17 additions & 0 deletions src/SitemapEvents.php
@@ -0,0 +1,17 @@
<?php

namespace Bolt\Extension\Bolt\Sitemap;

/**
* Definitions for all possible SitemapEvents.
*
* * @codeCoverageIgnore
*/
final class SitemapEvents
{
private function __construct()
{
}

const AFTER_COLLECTING_LINKS = 'sitemapAfterCollectingLinks';
}
40 changes: 27 additions & 13 deletions src/SitemapExtension.php
Expand Up @@ -4,6 +4,7 @@

use Bolt\Asset\Snippet\Snippet;
use Bolt\Asset\Target;
use Bolt\Collection\MutableBag;
use Bolt\Controller\Zone;
use Bolt\Extension\SimpleExtension;
use Bolt\Legacy\Content;
Expand Down Expand Up @@ -123,7 +124,7 @@ protected function registerFrontendControllers()
/**
* Get an array of links.
*
* @return array
* @return MutableBag
*/
private function getLinks()
{
Expand All @@ -136,13 +137,14 @@ private function getLinks()
$contentTypes = $app['config']->get('contenttypes');
$contentParams = ['limit' => 10000, 'order' => 'datepublish desc', 'hydrate' => false];

$links = [
[
'link' => $app['url_generator']->generate('homepage'),
'title' => $app['config']->get('general/sitename'),
],
$homepageLink = [
'link' => $app['url_generator']->generate('homepage'),
'title' => $app['config']->get('general/sitename'),
];

$links = new MutableBag();
$links->add($homepageLink);

foreach ($contentTypes as $contentType) {
$searchable = (isset($contentType['searchable']) && $contentType['searchable']) || !isset($contentType['searchable']);
$isIgnored = in_array($contentType['slug'], $config['ignore_contenttype']);
Expand All @@ -153,30 +155,30 @@ private function getLinks()
if (!$config['ignore_listing']) {
$baseDepth = 1;
if ($isIgnoredURL) {
$links[] = [
$links->add([
'link' => '',
'title' => $contentType['name'],
'depth' => 1,
];
]);
} else {
$link = $this->getListingLink($contentType['slug']);
$links[] = [
$links->add([
'link' => $link,
'title' => $contentType['name'],
'depth' => 1,
];
]);
}
}
$content = $app['storage']->getContent($contentType['slug'], $contentParams);
/** @var Content $entry */
foreach ($content as $entry) {
$links[] = [
$links->add([
'link' => $entry->link(),
'title' => $entry->getTitle(),
'depth' => $baseDepth + 1,
'lastmod' => Carbon::createFromTimestamp(strtotime($entry->get('datechanged')))->toW3cString(),
'record' => $entry,
];
]);
}
}
}
Expand All @@ -187,7 +189,7 @@ private function getLinks()
}
}

return $links;
return $this->transformByListeners($links);
}

/**
Expand Down Expand Up @@ -238,4 +240,16 @@ private function linkIsIgnored($link)
// No absolute match & no regex match
return false;
}

/**
* @param MutableBag $links
* @return MutableBag
*/
private function transformByListeners($links)
{
$event = new SitemapEvent($links);
$this->getContainer()['dispatcher']->dispatch(SitemapEvents::AFTER_COLLECTING_LINKS, $event);

return $event->getLinks();
}
}

0 comments on commit 26c0626

Please sign in to comment.