Set properties of site / rootNode in CommandController

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? :slight_smile:

Thanks in advance and have a nice day :sunny:

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…

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);
}

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);
    }
}