Developer forum

Forum » Development » Code first items, editors with parameters.

Code first items, editors with parameters.

Simon Nordahl
Simon Nordahl
Reply

Hi Guys

I'm somewhat new to codefirst items. I'm trying to have a field with a user group selector (UserEditor), but i cant find any documentation of how to add parameters to editors when used in combination with code first items. The UserEditor is sat to default to use users and not groups, how do I change that for my codefirst item?

I cant find any ProductGroup selector for code first items, does that mean that there are none ?

 

The only documentation that i could find on the subject is http://doc.dynamicweb.com/training-certification/t3-platform-developer/t3-platform-developer/3-4-items#sideNavTitle1-2-3

 

 

Regards Simon.


Replies

 
Nicolai Pedersen
Reply

Could this help?

https://doc.dynamicweb.com/api/html/d6a14638-4580-a1c9-3328-09ce262762f1.htm

BR Nicolai

 
Simon Nordahl
Simon Nordahl
Reply

Hi Nikolai

Quick answer !

But saly no, I looked at that too but still cant see how I apply the ShowType parameter to UserEditor or how to make a product group selector.

The example only makes a product selector:

        [Dynamicweb.Ecommerce.Content.Items.Annotations.Product("Product Link")]
        [Field("Product Link", typeof(Dynamicweb.Ecommerce.Content.Items.Editors.ProductEditor))]
        public string ProductLink { get; set; }

I can live without the product group selector, but the usergroup editor is vital if we are to use codefirst items.

 

 
Simon Nordahl
Simon Nordahl
Reply

I've attempted with variattions of the code below, but no matter I do it does not register parameter values.

        [Field("Users", EditorType = typeof(UserEditor)), AddInParameter("ShowTypes=1")]
        public  Dynamicweb.Security.UserManagement.Group Ggp { get; set; }

Have anybody successfully managed to make a codefirst item with a usergroup selector?

 
Simon Nordahl
Simon Nordahl
Reply

Hmm I've investigated the issue and it seems to be a bug, whenever we hit Render function in the AddInConfigurator the ConfigurableAddIn is always Nothing and thus it defaults to a default instance of the object.

 

 

       Public Property ConfigurableAddIn() As ConfigurableAddIn
            Get
                If _configurableAddIn Is Nothing Then It seems the object is always null
                    _configurableAddIn = CType(AddInManager.CreateAddInInstance(AddInType), ConfigurableAddIn) We just get the default object without any parameters.
                    _configurableAddIn.UpdateFromPost(Dynamicweb.Context.Current?.Request?.Form)
                End If
                Return _configurableAddIn
            End Get
            Set(ByVal Value As ConfigurableAddIn)
                _configurableAddIn = Value
            End Set
        End Property

 
Nicolai Pedersen
Reply

Hi Simon

Thanks - will have someone look into this.
BR Nicolai

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply
This post has been marked as an answer

Hi Simon,

there is a way to add parameters to editor in overrided InitializeEditorConfiguration method:

 
 <AttributeUsage(AttributeTargets.Property, AllowMultiple:=False, Inherited:=False)>
    Public Class MyUserFieldAttribute
        Inherits FieldAttribute

        Public Enum ShowTypes
            Users
            Groups
            GroupsAndUsers
        End Enum

        Public Property ShowType As ShowTypes

        Public Sub New()
            Me.New(String.Empty)
        End Sub

        Public Sub New(name As String)
            MyBase.New(name, GetType(Editors.UserEditor))
        End Sub

        Protected Friend Overrides Sub InitializeEditorConfiguration(itemField As Metadata.ItemField)
            Dim userEditor As New Editors.UserEditor()
            If ShowType = ShowTypes.GroupsAndUsers Then
                userEditor.ShowTypes = "1,2"
            ElseIf ShowType = ShowTypes.Groups Then
                userEditor.ShowTypes = "1"
            Else
                userEditor.ShowTypes = "2"
            End If

            itemField.EditorConfiguration = userEditor.GetParametersToXml(False)
        End Sub

    End Class

And then it could be used:

        [MyUserField(Name ="User")]
        public string FieldUser { get; set; }

        [MyUserField(Name = "Group", ShowType = UserAttribute.ShowTypes.Groups)]
        public string FieldGroup { get; set; }

        [MyUserField(Name = "Group and users", ShowType = UserAttribute.ShowTypes.GroupsAndUsers)]
        public string FieldGroupsAndUsers { get; set; }

Probably it has sense to add this code as part of default functionality in the next releases

Best regards, 
Vladimir

Votes for this answer: 1
 
Simon Nordahl
Simon Nordahl
Reply

Hi Vladimir

Thanks for the reply, I definitely think it’s a great idea to have a way of setting the parameter values. I was sure that it was an error on my part, as I was convinced there would be a way to send the parameter.

Perhaps we could have a general attribute, so it would be possible to set parameters on all the field types?

I have made a temporary workaround by making a new editor inherited from UserEditor like so:

    public class CodeFirstItem : ItemEntry
    {
        [Field("User groups", EditorType = typeof(UserGroupEditor))]
        public IEnumerable<string> UserGroups { get; set; }
    }

    public sealed class UserGroupEditor : UserEditor
    {
        public UserGroupEditor()
        {
            ShowTypes = "1";
        }
    }

Next up, for me, is a ProductGroup editor :)

 

Kind regards 

Simon

 

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

Hi Simon,

sorry for delay, we discussed a lot the group selecting feature :)

We will try to add it to the next release / hotfix. The ProductEditor wil acquire 2 checkboxes: Groups and Products

btw could you say how you going to use these values? Do you need rendering of products inside selected groups (when an item is rendered on frontend)?

Best regards,
Vladimir

 

 
Simon Nordahl
Simon Nordahl
Reply

Hi Vladimir

 

Guess I missed the notification for your post. 

The group selector value will be used to togehter with a macro to only show the product of the product group in any page under the item with groupselector.

So the page with the item acts as a brand section, and instead of having to the settings spread on a ecom catalog etc. all settings are handled in the item.

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

Hi Simon,
Thank you for your franknesssmiley

Btw as I know, the feature already was released in 9.3.13

Best regards,
Vladimir

 
Simon Nordahl
Simon Nordahl
Reply

Hi Vladimir
Was  9.3.13 rewoked? Cant seem to find it anywhere.

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

hmm... I also could'n find..
However it seems 9.3.12 already has these changes

Kind regards,
Vladimir

 
Simon Nordahl
Simon Nordahl
Reply

Hi Vladimir

Sorry that i keep necroing the thread.

The changes to UserAttribute works great in 9.3.12, but there's an error in the ProductAttribute in the InitializeEditorConfiguration implementation.

It currently passes name of the ShowType enum instead of the acctual number:

editor.ShowType = ShowType.ToString()

It should pass the enum number as a string to the editor:

        Protected Overrides Sub InitializeEditorConfiguration(ByVal itemField As ItemField)
            Dim editor As New ProductEditor()
            editor.ParagraphID = ShowOnParagraph
            'editor.ShowType = ShowType.ToString() // Incorrect

            If ShowType = ProductEditor.ShowTypes.Groups Then
                editor.ShowType = "1"
            ElseIf ShowType = ProductEditor.ShowTypes.GroupsAndProducts Then
                editor.ShowType = "2"
            Else
                editor.ShowType = "0"
            End If

            editor.RenderProductsFromGroups = RenderProductsFromGroups
            itemField.EditorConfiguration = editor.GetParametersToXml(False)
        End Sub

Also, I don't know if it is intentional, but the value of the editor is prefix with "g_" if a ProductEditor.ShowTypes.Groups was chosen, which means that you have to remember to trim the values before trying to fetch the group based on the value selected.

Regards

Simon Nordahl

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

Hi Simon,
Sorry, I will fix this bug till the next hotfix

The prefix "g_" is needed for work of mixed mode - when products and groups are rendered together
Btw products have "p_" prefix

Best regards,
Vladimir

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Hi,

I also have a question about editors with parameters. 

How do  I set the "Show as image selector" in a field of type "File"?

Regards / Aki

ShowAsImageSelector.png
 
Nicolai Pedersen
Reply

The attribute is missing the ShowAsImageSelector property.

Just added it - ready with 9.7.2 due today. TFS#70255

BR Nicolai

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Nice timing. smiley

/Aki 

 

You must be logged in to post in the forum