Created
September 18, 2017 06:41
-
-
Save wachterjohannes/b58f846f2f56e2453e9e84ab6ff74c1b to your computer and use it in GitHub Desktop.
Create article from website-kernel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Course; | |
use PHPCR\NodeInterface; | |
use PHPCR\SessionInterface; | |
use PHPCR\Util\UUIDHelper; | |
use Sulu\Bundle\ArticleBundle\Document\Behavior\DateShardingBehavior; | |
use Sulu\Component\DocumentManager\Event\FlushEvent; | |
use Sulu\Component\DocumentManager\Event\PersistEvent; | |
use Sulu\Component\DocumentManager\Events; | |
use Sulu\Component\DocumentManager\PathBuilder; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
class CourseSubscriber implements EventSubscriberInterface | |
{ | |
/** | |
* @var PathBuilder | |
*/ | |
private $pathBuilder; | |
/** | |
* @var SessionInterface | |
*/ | |
private $session; | |
/** | |
* @var SessionInterface | |
*/ | |
private $liveSession; | |
public function __construct(PathBuilder $pathBuilder, SessionInterface $session, SessionInterface $liveSession) | |
{ | |
$this->pathBuilder = $pathBuilder; | |
$this->session = $session; | |
$this->liveSession = $liveSession; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
Events::PERSIST => [ | |
['handleSetParentNode', 482], | |
['createNodeInPublicWorkspace', 479], | |
], | |
Events::FLUSH => [ | |
['handleFlush', 500], | |
], | |
]; | |
} | |
public function handleSetParentNode(PersistEvent $event) | |
{ | |
if (!$event->getDocument() instanceof DateShardingBehavior || $event->hasParentNode()) { | |
return; | |
} | |
$date = $event->getDocument()->getCreated(); | |
if (null === $date) { | |
$date = new \DateTime(); | |
} | |
$path = $this->pathBuilder->build(['%base%', '%articles%', $date->format('Y'), $date->format('m')]); | |
$event->setParentNode($this->createPath($path)); | |
} | |
public function createNodeInPublicWorkspace(PersistEvent $event) | |
{ | |
$node = $event->getNode(); | |
if (!$node->isNew()) { | |
return; | |
} | |
$node->addMixin('mix:referenceable'); | |
$node->setProperty('jcr:uuid', UUIDHelper::generateUUID()); | |
$this->createNodesWithUuid($node); | |
} | |
public function handleFlush(FlushEvent $event) | |
{ | |
$this->session->save(); | |
$this->liveSession->save(); | |
} | |
/** | |
* Create a path. | |
* | |
* @param string $path | |
* | |
* @return NodeInterface | |
*/ | |
public function createPath($path) | |
{ | |
$current = $this->session->getRootNode(); | |
$segments = preg_split('#/#', $path, null, PREG_SPLIT_NO_EMPTY); | |
foreach ($segments as $segment) { | |
if ($current->hasNode($segment)) { | |
$current = $current->getNode($segment); | |
} else { | |
$current = $current->addNode($segment); | |
$current->addMixin('mix:referenceable'); | |
$current->setProperty('jcr:uuid', UUIDHelper::generateUUID()); | |
} | |
} | |
return $current; | |
} | |
/** | |
* Creates every node on the path to the given node. Also uses the same UUIDs for these nodes. | |
* | |
* @param NodeInterface $node | |
*/ | |
private function createNodesWithUuid(NodeInterface $node) | |
{ | |
$path = $node->getPath(); | |
if ($this->liveSession->itemExists($path)) { | |
return; | |
} | |
$currentDefaultNode = $node->getSession()->getRootNode(); | |
$currentLiveNode = $this->liveSession->getRootNode(); | |
$pathSegments = explode('/', ltrim($path, '/')); | |
foreach ($pathSegments as $pathSegment) { | |
$currentDefaultNode = $currentDefaultNode->getNode($pathSegment); | |
if ($currentLiveNode->hasNode($pathSegment)) { | |
$currentLiveNode = $currentLiveNode->getNode($pathSegment); | |
continue; | |
} | |
$currentLiveNode = $currentLiveNode->addNode($pathSegment); | |
$currentLiveNode->setMixins(['mix:referenceable']); | |
$currentLiveNode->setProperty('jcr:uuid', $currentDefaultNode->getIdentifier()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
app.course.sharding: | |
class: App\Course\CourseSubscriber | |
arguments: | |
- '@sulu_document_manager.path_builder' | |
- '@doctrine_phpcr.default_session' | |
- '@doctrine_phpcr.live_session' | |
tags: | |
- {name: 'sulu.context', context: 'website'} | |
- {name: 'sulu_document_manager.event_subscriber'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to publish the sites directly you need to overwrite the workflow_stage subscriber service like this:
The Essential lines are