This project is read-only.

Creating custom attributes

Topics: Developer Forum
Mar 27, 2009 at 2:43 PM
Hey,

i have the following "todo" on my list. Whenever i create a new page based on my own derieved "ContactPage"-class

Public Class ContactPage
    Inherits N2.ContentItem ...

My "Todo" is that i must add by code an AddressItem-control to a zone defined in the page

Public MustInherit Class AddressItem
    Inherits ContentItem
    Public Overloads Overrides ReadOnly Property IsPage() As Boolean
        Get
            Return False
        End Get
    End Property ....

To do this i wrote a class MyEventInitializer

<AutoInitialize()> _
Public Class MyEventInitializer
    Implements IPluginInitializer

    Public Sub Initialize(ByVal engine As N2.Engine.IEngine) Implements N2.Plugin.IPluginInitializer.Initialize
        AddHandler engine.Persister.ItemSaved, AddressOf N2ItemSaved
    End Sub

    Public Sub N2ItemSaved(ByVal sender As Object, ByVal args As N2.ItemEventArgs)
        If TypeOf args.AffectedItem Is ContactPage Then
            If args.AffectedItem.Updated.ToString = args.AffectedItem.Created.ToString Then
            ' Here i added code here, which works fine

Now, i look for a method to define the type and zone at/by my ContactPage-Class and write the function "N2ItemSaved" more globaly.
I would like to use an "new, custom " attribute which could be attached to the n2.ContentItem derived classes.
The attribute should take a double-value list with the type of Control which to add and the zonename

something like
<CreateSubItemsWhenAdded("typeName1", "ZoneName1")> _
Public Class ContactPage
    Inherits N2.ContentItem ..

in the N2ItemSaved-function i query the attribute via reflection

my questions are now:
What do i have to look for?
Do i need to implement special or need "n2"-interfaces?
Can i create a attribute class and throw it into my website and it runs fine?

BR
Oliver

Mar 27, 2009 at 5:25 PM
Have you checked the ensure child attribute in n2contrib source code repository? There is an obsolete flag on the class so there might be a better solution.

What you could do with your CreateSubItemsWhenAdded is to to inherit from AbstractEditableAttribute (if you do that it will be called each time the item is saved through the standard editor) and check if an item with a certain name is present in the child collection.
Mar 27, 2009 at 6:09 PM
Hey,
First, thank for quick reply. My problem is, that the childcontrols should not be present for ever. They should only be created when the ContactPage is "added". Later they might be deleted by the user.

Is it right, that the AbstractEditableAttribute  is a "property" attribute? I'm looking for a "Class"-attribute
Mar 27, 2009 at 10:39 PM
There's another event you can use: N2.Context.Current.Definitions.ItemCreated

It's invoked the first time an item is created.