bstrilziw
(Benjamin S)
March 1, 2021, 3:53pm
1
Hi there,
I’ve got a CommandController that fetches some data from an API. Now I want to update a property on my root page ${q(site)}
.
In my CommanController I’ve got something like this:
$contentContext = $this->contextFactory->create(array(
'workspaceName' => 'live',
//'currentSite' => $node->getContext()->getCurrentSite()
));
\Neos\Flow\var_dump($contentContext->getRootNode());
Since I’m not really on a node, there is no $node
. Also $contentContext->getRootNode()
is not the correct node, so $contentContext->getRootNode()->getProperties()
returns not the correct properties.
What am I missing here?
Thanks in advance and have a nice day
kdambekalns
(Karsten Dambekalns)
March 1, 2021, 7:38pm
2
Since your command controller has no request to determine the “current site” from, this won’t work automagically. But you can fetch the node by path from /sites/sitenodename
. That should work…
grizooo
(Christian Schwahn)
March 2, 2021, 7:26am
3
protected function createContext(string $workspaceName): ?ContentContext
{
$contextProperties = [
'workspaceName' => $workspaceName,
'invisibleContentShown' => true,
'inaccessibleContentShown' => true
];
$currentDomain = $this->domainRepository->findOneByActiveRequest();
if ($currentDomain !== null) {
$contextProperties['currentSite'] = $currentDomain->getSite();
$contextProperties['currentDomain'] = $currentDomain;
} else {
$contextProperties['currentSite'] = $this->siteRepository->findFirstOnline();
}
return $this->contextFactory->create($contextProperties);
}
bstrilziw
(Benjamin S)
December 3, 2021, 11:57am
4
It’s been a while… this is my solution, thanks to Christian!
<?php
namespace ...;
use ...;
class MyCommandController extends \Neos\Flow\Cli\CommandController {
/**
* @Flow\Inject
* @var \Neos\Neos\Domain\Service\ContentContextFactory
*/
protected $contextFactory;
/**
* @Flow\Inject
* @var DomainRepository
*/
protected $domainRepository;
/**
* @Flow\Inject
* @var SiteRepository
*/
protected $siteRepository;
public function updateCommand() {
$contentContext = $this->createContext('live');
// get value
$this->optionFromSite = $contentContext->getNode($this->options['siteNodeProperties']['path'])->getProperty('myPropertyName');
// set value
$contentContext->getNode($this->options['siteNodeProperties']['path'])->setProperty('myPropertyName', 'myValue');
}
protected function createContext(string $workspaceName): ?ContentContext {
$contextProperties = [
'workspaceName' => $workspaceName,
'invisibleContentShown' => TRUE,
'inaccessibleContentShown' => TRUE
];
$currentDomain = $this->domainRepository->findOneByActiveRequest();
if ($currentDomain !== NULL) {
$contextProperties['currentSite'] = $currentDomain->getSite();
$contextProperties['currentDomain'] = $currentDomain;
} else {
$contextProperties['currentSite'] = $this->siteRepository->findFirstOnline();
}
return $this->contextFactory->create($contextProperties);
}
}