Umbraco-CMS: $site token replacement no longer working in custom MNTP (for new documents)
Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)
11.2.0
Bug summary
$site token does not work in MNTP XPath queries for new documents, although it does work for existing documents. In Umbraco 10, it worked equally well for new and existing documents
Specifics
We recently upgraded our application from Umbraco 10 to 11. We have a number of custom MNTPs in our application. All of these select a folder in the current site’s dataRepository as a starting point using the $site token
Here is an example of one Xpath query we use, a combination of the $site token and a number of document aliases
$site/dataRepository/taxonomyRoot/expertiseTaxonomy
This worked fine in Umbraco 10, both for existing documents and new documents. However, since upgrading to Umbraco 11, the starting folder is set correctly in the MNTP for existing documents, but not for new documents. For new documents, the starting folder is set as the root folder which indicates, I think, that the Xpath query has failed to locate the start folder
Perhaps this is something to do with the transient nature of a new document, which of course has no id until it is saved. But as I mentioned before, it did work in Umbraco 10
Not sure if this new behaviour is intended, or a bug.
Steps to reproduce
- In the content tree, create a folder one or more levels deep
- Create a custom MNTP data type
- Use an Xpath query to select the starting folder for the new MNTP. The Xpath query should start with
$siteand be able to locate the folder created in step 1, - Create a new Document type and have one of its fields use the new custom MNTP
- In the Content tree, create an instance of the new Document Type
- Before saving the new document, click on the field with the custom MNTP to select one (or more) items
Expected result / actual result
The popup for the MNTP should have as its start folder the folder created in step 1 above, as was the case in Umbraco 10
What actually happens is that the site root is the start folder
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 1
- Comments: 20 (12 by maintainers)
Another possibilty would be to pass both the parent id (as int, always non-zero) and the current id (either nullable int? or zeroable int). These would be calculated from
editorstate.current.idandeditorstate.current.parentIdin the js example aboveIf the current Id is null or zero, we know we’re creating. This would allow us to expand the variables with thte following rules:
$site: always expands, calculated from parentid parameter $parent: always expands, calculated from parentId parameter $current: only expands if currentId parameter is not null or zero
Granted this would involve changing both .net and javascript code, but it would ensure accuracy of $parent expansion and still keep $site available
The other place in Umbraco.Core where this is called is in
NotFoundHandlerHelper. Not exactly sure how it would pass both parent and current idHi @karlmacklin thanks for piping in on this 😃 I had looked at the source code too and saw what you saw, i.e. that
nodeContextIdseemed to not have a value while the item being created was transient. Also like you I don’t know the Umbraco Core codebase well enough to start making changesHowever, I know for sure that this worked in 10.1.0, and your post gave me an idea.
$siteshould always evaluate the same in a content tree under a site node (to that site node). However$parentwould evaluate differently depending on how deep in the content tree you are starting from.So, I created a new MNTP data type with an xpath query of
$parentto calculate the starting node, and added a field called “Random Page”. I created a tree of content with 5 levels, each containing a single item. For each one of these, when selecting the random page field, no matter at what level of depth, the starting point would be its parent, and the only selection available would be the page itselfHowever the behaviour is slightly different when creating a new item, while the new item is still transient. In this case
$parentevaluates as the parent of the page creating the new item. For example, following on from the previous image I now try to create a level 6 item. This is what it looks like selecting Random Page whilst Level 6 is still transientWhen I save Level 6 and go to select Random page again this is what we see. Note that it has reverted to the standard behaviour
What this tells us is that in Umbraco 10.1.0, whilst an item is transient,
nodeContextIdas passed toUmbracoXPathPathSyntaxParser.ParseXPathQuery()contains the id of the node from where the new item is being created. Somewhere after that this behaviour has changed so thatnodeContextIdis now null whilst an item is still transient. It seems that this change was made by v10.4.0 according to @elit0451 's video aboveThoughts @elit0451 ?
Cheers Paul