Contentment data list get current node
s

Sean Thorne

over 1 year ago
Why isn't this working 😦
cs
public class DiscordChannelDataList : IDataListSource
{
    private readonly IContentmentContentContext _contentContext;
    private readonly IDiscordService _discordService;

    public DiscordChannelDataList(IContentmentContentContext contentContext, IDiscordService discordService)
    {
        _contentContext = contentContext;
        _discordService = discordService;
    }

    public string Name => "Discord Server Channels";
    public string Description => "A list of channels from a Discord server";
    public string Icon => "icon-badge";
    public Dictionary<string, object> DefaultValues { get; } = new();
    public IEnumerable<ConfigurationField> Fields { get; } = new List<ConfigurationField>();
    public string Group => "Discord";
    public OverlaySize OverlaySize => OverlaySize.Large;

    public IEnumerable<DataListItem?> GetItems(Dictionary<string, object> config)
    {
        try
        {
            var currentNode = _contentContext.GetCurrentContent(out bool isParent);
            if (currentNode is null)
                return Enumerable.Empty<DataListItem>();
            
            var server = currentNode.Ancestor<Server>();
            if (server?.ServerId == null)
                return Enumerable.Empty<DataListItem>();

            var serverId = ulong.Parse(server.ServerId);

            var channels = Task.Run(async () => await _discordService.GetTextChannelsAsync(serverId));

            var rolesResult = channels.Result.ToList();
            if (rolesResult.Count == 0)
                return Enumerable.Empty<DataListItem>();

            return rolesResult.Select(x => new DataListItem
            {
                Value = x.Id.ToString(),
                Name = x.Name,
            });
        }
        catch
        {
            // intentionally empty
        }
        
        return Enumerable.Empty<DataListItem>();
       
    }
}
I have very simliar code in another project and it works fine. I want to get the current node id, to access the root node property. My tree is: DiscordServer -> node DiscordServer -> node I am on "node" and want "discord server" property. I've tried the above code, and also some weirdness:
cs
var gotContext = _umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext);
            if (!gotContext || umbracoContext?.Content == null)
                return Enumerable.Empty<DataListItem>();

            var gotId = int.TryParse(_requestAccessor.GetQueryStringValue("id"), out var id);
            if (!gotId)
                return Enumerable.Empty<DataListItem>();

            var publishedContent = umbracoContext.Content.GetById(id);
            if (publishedContent == null)
                return Enumerable.Empty<DataListItem>();
Umbraco Commerce: Documentation confusion about product adapters
s

Sean Thorne

11 months ago
I'm following along the Umbraco Commerce documentation (13.latest) and registering a product adaptor does not actually work as described. The docs express:
cs
public static class UmbracoCommerceUmbracoBuilderExtensions
{
    public static IUmbracoCommerceBuilder AddMyServices(IUmbracoCommerceBuilder builder)
    {
        // Replacing the default Product Adapter implementation
        builder.Services.AddUnique<ProductAdapterBase, MyCustomProductAdapter>();

        // Return the builder to continue the chain
        return builder;
    }
}
Which is using the
ProductAdaptorBase
as the base class instead of
IProductAdaptor
(.. as is obsolete). But when inheriting from the base and trying to replace it doesn't actually allow me to register it, using the
IProductAdaptor
interface instead works as I expect it to. Switching up my CustomProductAdaptor to inherit base and implement interface then also allows it to work like so:
cs
builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddUmbracoCommerce(commerceBuilder =>
    {
        // works just fine
        commerceBuilder.Services.AddUnique<IProductAdapter, ProductAdaptor>();

        // does not work
        // commerceBuilder.Services.AddUnique<ProductAdaptorBase, ProductAdaptor>();

    })
So my final class impl looks like this:
cs
public class ProductAdaptor(IProductService productService, ICommerceApi commerceApi) : ProductAdapterBase, IProductAdapter
{
    public override IProductSnapshot GetProductSnapshot(Guid storeId, string productReference, string languageIsoCode)

// rest removed for brevity
}
I think the intent is to actually inherit from the ABC and replace that
cs
public abstract class ProductAdapterBase : IProductAdapter
Have I wildly missed something? I'm a little miffed. The obsolete warnings are trigerring. https://cdn.discordapp.com/attachments/1313875209203486771/1313875209828171836/image.png?ex=6751b924&is=675067a4&hm=c8327ccde8747128ed6cb98c671373708dc67923fbff054a2861e9ee1b46e415&