Access page property by a content node

Hi,

the Page prototype has some properties for adding css/js to a site, e.g. page.head.javascripts which is an array. Using this array I can define a set of *.js/css files which are added to my page by default.

page = Page
page.head {
        javascripts {
                jquery = '<script src="URI_A"></script>'
                someScript = '<script src="URI_B"></script>'
        }
}
otherPage < page
# add js
otherPage.head.javascripts.additionalScript = '<script src="URI_C"></script>'

This works perfectly fine to adjust css/js on sites as needed.

Is it possible to access the head.javascripts/head.stylesheets property from a child node in order to add js/css files to a page?
For example: Somewhere in my page.body.content.main ContentCollection I add/insert a node (from backend, e.g.) which needs a specific stylesheet/script in order to render the html content correctly.
It is defined by the following prototype:

prototype(Vendor.Site:CustomContent) < prototype(TYPO3.Neos:Content) {
    # […] do some TypoScript
    # add js file
    ${documentNode}.head.javascripts.prototypeScript =  = '<script src="URI_D"></script>'
}

Is something like this possible?

Since fusion-Rendering is meant to be side effect free there is no good way to decide such stuff from the code that is rendered in the main collection.

The Neos way to do this is to add the script to the head section but make sure that it is included only if the content that needs it is present.

javascripts {
  contentScript = ...
  contentScript.@if.contentIsPresent = ${q(documentNode).children('main').find('[instanceof Vendor.Site:Content]').is()}
}

Hi,

thanks for the quick reply and the solution. Since this approach is the other way around as I thought of it:
Does this work if the ContentNode is not a direct child of main? The ContentNode could appear e.g. in one of the predefined multicolumn ContentCollections.

Is there a way to tell .children() that it is ought to look recursively through all its children (and children children, etc.) nodes?

Try this;

javascripts {
  contentScript = ...
  contentScript.@if.contentIsPresent = ${q(documentNode).children('[instanceof TYPO3.Neos:ContentCollection]').find('[instanceof Vendor.Site:Content]').is()}
}
1 Like