Filtering dopdown prevalue items based on user
# help-with-umbraco
a
We need to filter a dropdown's prevalue items based on the current user. The old way of doing this was to use a
SendingContentNotification
to tweak the values. Is that still the preferred approach? The issue we're having is that the data in the
ContentNullable
field which we are supposed to use now is an anonymous datatype that we can't cast or use reflection on easily, where the
Configuration
field was a nicely typed
ValueListConfiguration
. If there is a doc page or blog post someone can point me to of the "current" way of doing this that would be helpful.
j
I think the way that I would do this is use Contentment to build a custom data list and that can be filtered in the code by the user
a
Perfect! This was absolutely the right way to go. We've changed it into a Contentment Dropdown list with a custom IDataListSource that is context aware. Way WAY easier.
My only issue now though - how do you detect the location of a content item when it's a NEW item that hasn't been saved yet?
@leekelleher Any ideas? Is it even possible?
l
There is an
IContentmentContentContext
which can be injected, this has a method
.GetCurrentContent(out bool isParent)
... is
isParent
is true, it could be assumed that it's a newly/unsaved document. (It's also
true
for Block List items too though, 🤔 ) https://github.com/leekelleher/umbraco-contentment/blob/develop/docs/editors/data-list.md#accessing-contextual-content Hope this helps.
a
Wow! That was fast! Almost like you are sitting there with Discord open all day 😆
Yes - I read the docs and have the context injected
Here's the code:
Copy code
var home = _contentmentContentContext.GetCurrentContent(out bool isParent)?.Ancestor<Home>();
The problem is that the
GetCurrentContent
returns NULL for a NEW item
This is a dropdown property directly on a documentType - not inside a blockList or anything like that
Ah ha - I think I know what the issue is. The parent is published, but one of the ancestors further up the chain isn't. So the
GetCurrentContent
method returns null.
Not sure what can be done about that - but it's an interesting issue. Would you like me to raise a github issue for you?
l
Sure thing. 👍
2 Views