Skip to content

Instantly share code, notes, and snippets.

@htuscher
Last active July 31, 2018 12:03
Show Gist options
  • Save htuscher/9012d455eab52658bbf4 to your computer and use it in GitHub Desktop.
Save htuscher/9012d455eab52658bbf4 to your computer and use it in GitHub Desktop.
Create frontend URIs from neos backend (CLI)
<?php
namespace Vendor\Site\Command;
use Vendor\Site\Service\SocialSharesService;
use TYPO3\Eel\FlowQuery\FlowQuery;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Cli\CommandController;
use TYPO3\Flow\Configuration\ConfigurationManager;
use TYPO3\Flow\Http\Request;
use TYPO3\Flow\Http\Response;
use TYPO3\Flow\Http\Uri;
use TYPO3\Flow\Mvc\ActionRequest;
use TYPO3\Flow\Mvc\Controller\Arguments;
use TYPO3\Flow\Mvc\Controller\ControllerContext;
use TYPO3\Flow\Mvc\Routing\UriBuilder;
use TYPO3\Neos\Service\LinkingService;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
use TYPO3\TYPO3CR\Domain\Service\ContextFactoryInterface;
/**
* Class SharesCommandController
*
* @package Vendor\Site\Command
* @Flow\Scope("singleton")
*/
class UrlCommandController extends CommandController {
/**
* @var array
*/
protected $settings;
/**
* @var string
*/
protected $baseDomain;
/**
* @var string
*/
protected $siteName;
/**
* @var SocialSharesService
* @Flow\Inject
*/
protected $socialSharesService;
/**
* @var ContextFactoryInterface
* @Flow\Inject
*/
protected $contextFactory;
/**
* @var LinkingService
* @Flow\Inject
*/
protected $linkingService;
/**
* @var UriBuilder
*/
protected $uriBuilder;
/**
* @var SiteRepository
* @Flow\Inject
*/
protected $siteRepository;
/**
* Inject the settings
*
* @param array $settings
* @return void
*/
public function injectSettings(array $settings) {
$this->settings = $settings;
$this->baseDomain = $settings['baseDomain'];
$this->siteName = $settings['siteName'];
}
public function doSomethingCommand() {
$site = $this->siteRepository->findOneByName($this->siteName);
$rootNode = $this->contextFactory->create(['workspaceName' => 'live', 'currentSite' => $site])->getRootNode();
$rootNode->getChildNodes('Vendor.Site:News');
$flowQuery = new FlowQuery([$rootNode]);
$newsNodes = $flowQuery->find('[instanceof Vendor.Site:News]')->get();
/** @var NodeInterface $newsNode */
foreach($newsNodes as $newsNode) {
echo $this->getUrlToNode($newsNode) . PHP_EOL;
}
}
/**
* The injection of the faked UriBuilder is necessary to generate frontend URLs from the backend
*
* @param ConfigurationManager $configurationManager
*/
public function injectUriBuilder(ConfigurationManager $configurationManager) {
$_SERVER['FLOW_REWRITEURLS'] = 1;
$httpRequest = Request::createFromEnvironment();
$httpRequest->setBaseUri(new Uri($this->baseDomain));
$request = new ActionRequest($httpRequest);
$uriBuilder = new UriBuilder();
$uriBuilder->setRequest($request);
$uriBuilder->setCreateAbsoluteUri(TRUE);
$this->uriBuilder = $uriBuilder;
}
/**
* Create the frontend URL to the node
*
* @param NodeInterface $node
* @return string The URL of the node
* @throws \TYPO3\Neos\Exception
*/
protected function getUrlToNode(NodeInterface $node) {
$uri = $this->linkingService->createNodeUri(
new ControllerContext(
$this->uriBuilder->getRequest(),
new Response(),
new Arguments(array()),
$this->uriBuilder
),
$node,
$node->getContext()->getRootNode(),
'html',
TRUE
);
return $uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment