[SOLVED] Load a specific workspace in the front-end


(George Dimitriadis) #1

Hello,

I have a custom content builder in the front-end for users to add content to the website.

I got a base working, also through a workspace for the user, but I can only see the changes in the front-end when they are published to the live workspace.

I would like to implement a “Preview mode”, so that the user in the frontend will see his changes without publishing them and he will be able to publish them through a button.

So, my question is: How do I tell Neos to load the current page from a specific workspace ?


(Soren Malling) #2

Go and have a look at the ContextFactory class or search for it here on the forum - there is some example on how it’s used.

The basic about it, is that you can add a number of parameters like this

		$contentContext = $this->contextFactory->create(array(
			'workspaceName' => 'live',
			'currentSite' => $node->getContext()->getCurrentSite()
		));

and for example pass a workspace after your choice


(George Dimitriadis) #3

Hey Soren,

thanks for the example !
Is there a way to specify the desired workspace when fetching the content in fusion ?

For example:

content {
     main = PrimaryContent {
        nodePath = 'main'
        # The next line is imaginary code and doesn't work
        workspace = 'my-preview-workspace-identifier'
    }
}

Or do I have to fetch the desired content manually from a PHP service ?


(Soren Malling) #4

Perhaps something like this could be a start

q(site).context({'workspaceName': 'whatever'}).find(...)

and if not, you might create your own Collection implementation


(George Dimitriadis) #5

Interesting, thanks for your fast replies !

I’ll look into it and update with my solution.


(George Dimitriadis) #6

UPDATE

This is what I ended up with in fusion:

main = Neos.Neos:ContentCollection {
    workspacedDocumentNode = ${q(documentNode).context({'workspaceName': 'my-workspace-of-interest'}).get(0)}
    @context.node = ${Neos.Node.nearestContentCollection(this.workspacedDocumentNode, 'main')}
    @ignoreProperties = ${['workspacedDocumentNode']}
}

I checked how the ContentCollection picks the node and replaced it like so. So, instead of passing the “node” to the NearestContentCollection, I am passing the node from the selected workspace instead. Not sure if I did this properly, please correct me if I can improve this further.

Thanks a lot for your help !


(Soren Malling) #7

Great work! :rocket: