Giter Site home page Giter Site logo

abjerner / skybrud.umbraco.multinodetreepicker Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 559 KB

Custom multinode treepicker for Umbraco.

License: MIT License

Batchfile 1.43% HTML 1.57% JavaScript 5.13% C# 91.87%
skybrud umbraco umbraco-cms umbraco-v8 umbraco-packages umbraco-property-editor limbo package umbraco-package dotnet

skybrud.umbraco.multinodetreepicker's Introduction

Skybrud.Umbraco.MultiNodeTreePicker

Skybrud.Umbraco.MultiNodeTreePicker adds a special multinode treepicker to the Umbraco backoffice in which developers can select a custom item converter.

The purpose of an item converter is to control the C# type returned by the .Value() method or the corresponding property in a ModelsBuilder generated model. This is particular useful in a SPA/Headless Umbraco implementation, where the ModelsBuilder model can then be returned directly via a WebAPI endpoint.

Installation

The Umbraco 9 version of this package is only available via NuGet. To install the package, you can use either .NET CLI

dotnet add package Skybrud.Umbraco.MultiNodeTreePicker --version 2.0.0

or the older NuGet Package Manager:

Install-Package Skybrud.Umbraco.MultiNodeTreePicker -Version 2.0.0

Umbraco 8
For the Umbraco 8 version of this package, see the v2/main branch instead.

Examples

At @skybrud we typically use Umbraco as a headless CMS, and being able to control the generated models therefore makes a lot of sense. If a given page has some related content, it doesn't make sense for us to return the full model of a related model, so we instead have an item class with the needed properties - this class could be called TestItem.

Normally the property with the related content would return the full model for each page, but with the special multinode treepicker from this package, we can implement a custom item converter.

We can do this by implementing the IMntpItemConverter interface, but to get going a bit quicker, the package also contains the abstract MntpGenericItemConverter class we can use instead:

public class TestMntpItemConverter : MntpGenericItemConverter<TestItem> {

    public TestMntpItemConverter() : base("Default item converter", x => new TestItem(x)) { }

}
public class TestItem {
        
        public Guid Key { get; }

        public string Name { get; }

        public string Url { get; }
        
        public TestItem(IPublishedContent content) {
            Key = content.Key;
            Name = content.Name;
            Url = content.Url;
        }

    }

The MntpGenericItemConverter class requires us to specify a name for the converter, and then a callback function that will be used for converting each IPublishedContent to the desired type.

image

When the data type is configured to use our item converter (see screenshot above), properties of this type will now return List<TestItem> instead of List<IPublishedContent>.

With this special multinode treepicker, it's the datatype of the individual property, that determines the returned value. Another property could for instance be for selecting contact persons where we'd need a bit more information than what's available from the TestItem class, so we can create another item converter:

  public class TestMntpPersonItemConverter : MntpGenericItemConverter<TestPersonItem> {

      public TestMntpPersonItemConverter() : base("Person item converter", x => new TestPersonItem(x)) { }

  }
  public class TestPersonItem : TestItem {

      public string Phone { get; }

      public string Email { get; }

      public TestPersonItem(IPublishedContent content) : base(content) {
          Phone = content.Value<string>("phone");
          Email = content.Value<string>("email");
      }

  }

image

Inspiration

The item converters in this package was inspired by a similar concept in the Contentment package. Thanks for creating and sharing this with us @leekelleher ๐Ÿ‘

skybrud.umbraco.multinodetreepicker's People

Contributors

abjerner avatar nikcio avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

nikcio

skybrud.umbraco.multinodetreepicker's Issues

Change property editor cache level from `Snapshot` to `Elements`

Snapshot typically indicates that the property value is cached for the duration of a given request, whereas Elements would cache the property value until at node is updated in Umbraco.

Alternatively, the cache level could be configured from the data type options ๐Ÿ’ก ๐Ÿ˜Ž

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.