src/Sulu/Component/Content/Document/Subscriber/RouteSubscriber.php
public function handlePersist(PersistEvent $event)
    {
        $document = $event->getDocument();

        if (!$document instanceof RouteBehavior) {
            return;
        }

        $node = $event->getNode();
        $node->setProperty(self::NODE_HISTORY_FIELD, $document->isHistory());

        $targetDocument = $document->getTargetDocument();

        if ($targetDocument instanceof HomeDocument
            || !$targetDocument instanceof WebspaceBehavior
            || !$targetDocument instanceof ResourceSegmentBehavior
        ) {
            return;
        }

        // copy new route to old position
        $webspaceKey = $targetDocument->getWebspaceName();
        $locale = $this->documentInspector->getLocale($document);

        $routePath = $this->sessionManager->getRoutePath($webspaceKey, $locale, null)
            . $targetDocument->getResourceSegment();

        // create a route node if it is not a new document and the path changed
        $documentPath = $this->documentInspector->getPath($document);
        if ($documentPath && $documentPath != $routePath) {
            /** @var RouteDocument $newRouteDocument */
            $newRouteDocument = $this->documentManager->create('route');
            $newRouteDocument->setTargetDocument($targetDocument);
            $this->documentManager->persist(
                $newRouteDocument,
                $locale,
                [
                    'path' => $routePath,
                    'auto_create' => true,
                ]
            );
            $this->documentManager->publish($newRouteDocument, $locale);

            // change routes in old position to history
            $this->changeOldPathToHistoryRoutes($document, $newRouteDocument);
        }
    }
public function handlePersist(PersistEvent $event)
    {
        $document = $event->getDocument();

        if (!$document instanceof RouteBehavior) {
            return;
        }

        $node = $event->getNode();
        $node->setProperty(self::NODE_HISTORY_FIELD, $document->isHistory());

        $targetDocument = $document->getTargetDocument();

        if ($targetDocument instanceof HomeDocument
            || !$targetDocument instanceof WebspaceBehavior
            || !$targetDocument instanceof ResourceSegmentBehavior
        ) {
            return;
        }

        // copy new route to old position
        $webspaceKey = $targetDocument->getWebspaceName();
        $locale = $this->documentInspector->getLocale($document);

        $routePath = $this->sessionManager->getRoutePath($webspaceKey, $locale, null)
            . $targetDocument->getResourceSegment();

        // create a route node if it is not a new document and the path changed
        $documentPath = $this->documentInspector->getPath($document);
        if ($documentPath && $documentPath != $routePath) {
            /** @var RouteDocument $newRouteDocument */
            $newRouteDocument = $this->documentManager->create('route');
            $newRouteDocument->setTargetDocument($targetDocument);
            $this->documentManager->persist(
                $newRouteDocument,
                $locale,
                [
                    'path' => $routePath,
                    'auto_create' => true,
                ]
            );
            $this->documentManager->publish($newRouteDocument, $locale);

            // change routes in old position to history
            $this->changeOldPathToHistoryRoutes($document, $newRouteDocument);
        }
    }
public function handlePersist ( PersistEvent $ event ) { $ document = $ event -> getDocument ( ) ; if ( ! $ document instanceof RouteBehavior ) { return ; } $ node = $ event -> getNode ( ) ; $ node -> setProperty ( self :: NODE_HISTORY_FIELD , $ document -> isHistory ( ) ) ; $ targetDocument = $ document -> getTargetDocument ( ) ; if ( $ targetDocument instanceof HomeDocument || ! $ targetDocument instanceof WebspaceBehavior || ! $ targetDocument instanceof ResourceSegmentBehavior ) { return ; } // copy new route to old position $ webspaceKey = $ targetDocument -> getWebspaceName ( ) ; $ locale = $ this -> documentInspector -> getLocale ( $ document ) ; $ routePath = $ this -> sessionManager -> getRoutePath ( $ webspaceKey , $ locale , null ) . $ targetDocument -> getResourceSegment ( ) ; // create a route node if it is not a new document and the path changed $ documentPath = $ this -> documentInspector -> getPath ( $ document ) ; if ( $ documentPath && $ documentPath != $ routePath ) { /** @var RouteDocument $newRouteDocument */ $ newRouteDocument = $ this -> documentManager -> create ( 'route' ) ; $ newRouteDocument -> setTargetDocument ( $ targetDocument ) ; $ this -> documentManager -> persist ( $ newRouteDocument , $ locale , [ 'path' => $ routePath , 'auto_create' => true , ] ) ; $ this -> documentManager -> publish ( $ newRouteDocument , $ locale ) ; // change routes in old position to history $ this -> changeOldPathToHistoryRoutes ( $ document , $ newRouteDocument ) ; } }
Updates the route for the given document and creates history routes if necessary.

@param PersistEvent $event
Updates the route for the given document and creates history routes if necessary .