Skip to content

Instantly share code, notes, and snippets.

@nezaniel
Last active November 6, 2018 16:12
Show Gist options
  • Save nezaniel/e776667af8947dc32de620377a4a5054 to your computer and use it in GitHub Desktop.
Save nezaniel/e776667af8947dc32de620377a4a5054 to your computer and use it in GitHub Desktop.
<?php
namespace My\Package\Application;
/* *
* This script belongs to the Neos Flow package "My.Package". *
* */
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\Doctrine\PersistenceManager;
use Neos\Media\Domain\Model\ImageInterface;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Model\NodeType;
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
use Neos\Fusion\FusionObjects\TemplateImplementation;
/**
* XML Sitemap implementation
*/
class XmlSitemapImplementation extends TemplateImplementation
{
/**
* @Flow\Inject
* @var NodeTypeManager
*/
protected $nodeTypeManager;
/**
* @Flow\Inject
* @var PersistenceManager
*/
protected $persistenceManager;
/**
* @var SeoHelper
*/
protected $seoHelper;
/**
* @var array
*/
protected $assetPropertiesByNodeType;
/**
* @return void
*/
public function initializeObject()
{
$relevantPropertyTypes = [
'array<Neos\Media\Domain\Model\Asset>' => true,
'Neos\Media\Domain\Model\Asset' => true,
'Neos\Media\Domain\Model\ImageInterface' => true
];
foreach ($this->nodeTypeManager->getNodeTypes(false) as $nodeType) {
/** @var NodeType $nodeType */
foreach ($nodeType->getProperties() as $propertyName => $propertyConfiguration) {
if (isset($relevantPropertyTypes[$nodeType->getPropertyType($propertyName)])) {
$this->assetPropertiesByNodeType[$nodeType->getName()][] = $propertyName;
}
}
}
$this->seoHelper = new SeoHelper();
}
/**
* @return array
* @throws \Neos\ContentRepository\Exception\NodeException
*/
public function getItems()
{
$items = [];
/** @var NodeInterface $site */
$site = $this->runtime->getCurrentContext()['site'];
$this->appendItems($items, $site);
return $items;
}
/**
* @param array & $items
* @param NodeInterface $documentNode
* @return void
* @throws \Neos\ContentRepository\Exception\NodeException
*/
protected function appendItems(array &$items, NodeInterface $documentNode)
{
if ($this->seoHelper->isDocumentNodeToBeIndexed($documentNode)) {
$item = [
'node' => $documentNode,
'lastmod' => $documentNode->getNodeData()->getLastModificationDateTime(),
'priority' => $documentNode->getProperty('xmlSitemapPriority') ?: '0.5'
];
if ($documentNode->getProperty('xmlSitemapChangeFrequency')) {
$item['changefreq'] = $documentNode->getProperty('xmlSitemapChangeFrequency');
}
$this->resolveImages($documentNode, $item);
$items[] = $item;
}
if(!$documentNode->hasChildNodes()){
return;
}
foreach ($documentNode->getChildNodes('Neos.Neos:Document') as $childDocumentNode) {
$this->appendItems($items, $childDocumentNode);
}
}
/**
* @param NodeInterface $node
* @param array & $item
* @throws \Neos\ContentRepository\Exception\NodeException
*/
protected function resolveImages(NodeInterface $node, array& $item)
{
if (isset($this->assetPropertiesByNodeType[$node->getNodeType()->getName()]) && !empty($this->assetPropertiesByNodeType[$node->getNodeType()->getName()])) {
foreach ($this->assetPropertiesByNodeType[$node->getNodeType()->getName()] as $propertyName) {
if (is_array($node->getProperty($propertyName)) && !empty($node->getProperty($propertyName))) {
foreach ($node->getProperty($propertyName) as $asset) {
if ($asset instanceof ImageInterface) {
$item['images'][$this->persistenceManager->getIdentifierByObject($asset)] = $asset;
}
}
} elseif ($node->getProperty($propertyName) instanceof ImageInterface) {
$item['images'][$this->persistenceManager->getIdentifierByObject($node->getProperty($propertyName))] = $node->getProperty($propertyName);
}
}
}
foreach ($node->getChildNodes('Neos.Neos:ContentCollection,Neos.Neos:Content') as $childNode) {
$this->resolveImages($childNode, $item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment