[SOLVED] How to get current node

Hallo,

I’ve created my own «HttpComponent» class (PHP) to adjust the header (amongst other things) and I need to get the node (or identifier of this node) of the current site. How can I archive this?

Hi,

not a 100% sure how to get the node there. But you can also set header in Fusion where you have all the information. Maybe that’s easier?

Hi Sebastian,

great idea, but unfortunately it doesn’t work. I could create a new header attribute with the node identifier but it is not in the header information when the «handle» method of my HttpComponent will be executed. :slightly_frowning_face: Maybe it’s too early? Can I access other information at that point?


As I don’t know what you are trying to achieve one more option: Register your component as postprocess and there your header set via Fusion should be.

If you want it earlier you might need to use the FrontendRoutePartHandler to get a node.

Yeah would be good to get a bit more background about what you actually want to do there.

Our Marketing would like to have a language redirect. If the browser language of the visitor is not DE it should redirect to the english version (if it exists; some pages are not translated). I know how to redirect but the problem is that I can not just put «en/» in front of the relative URL because the speaking URLs are translated. So I somehow need the node object to generate the english link of the current page. But currently I’m failing at this point. Or is there another way to archive this?

Registering my component as «postprocess» did the trick to get the current node ID from the header. I’m also able to create the node object but not the link.

I know that there’s the package – «Axovis.Neos.LanguageDetection» – but the last changes were made three years ago and it does not work with Neos 4.3.

I just found out that you can get the link of the current page. Is it possible to get this link with another dimension? Something like this:

    httpResponseHead {
        headers.Node = ${q(node).property('_identifier')}
        headers.Link = Neos.Neos:NodeUri {
            node = ${node}
            absolute = false
            arguments = Neos.Fusion:RawArray {
                dimension = 'en_US'
            }
        }
    }

You can do this with the context eel helper:

# Get translated node (if available) - fallback to current node
node = ${q(node).context({targetDimensions: {language: 'en_US'}, dimensions: {language: ['en_US']}}).get(0) || node}

Thanks Benjamin, that’s it! :slight_smile:

BTW. The SEO package renders alternate language links, therefore you should have all of them already in the body. Maybe that‘s easier.