Beispiel #1
0
        public override void Initializing(InitializingContentContext context)
        {
            var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentType);

            if (contentTypeDefinition == null)
            {
                return;
            }

            foreach (var typePartDefinition in contentTypeDefinition.Parts)
            {
                var partName = typePartDefinition.PartDefinition.Name;
                var partType = _contentPartFactory.GetContentPartType(partName) ?? typeof(ContentPart);

                var part = context.ContentItem.Get(partType, partName) as ContentPart;
                _partHandlers.Invoke(handler => handler.Initializing(context, part), Logger);

                foreach (var partFieldDefinition in typePartDefinition.PartDefinition.Fields)
                {
                    var fieldTypeName = partFieldDefinition.FieldDefinition.Name;
                    var field         = _contentFieldFactory.CreateContentField(fieldTypeName);

                    if (field != null)
                    {
                        var fieldName = partFieldDefinition.Name;
                        part.Weld(fieldName, field);
                    }
                }

                context.ContentItem.Weld(partName, part);
            }
        }
        public override void Loading(LoadContentContext context)
        {
            // This method is called on Get()
            // Adds all the missing parts to a content item based on the content type definition.

            var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentItem.ContentType);

            if (contentTypeDefinition == null)
            {
                return;
            }

            foreach (var typePartDefinition in contentTypeDefinition.Parts)
            {
                var partName = typePartDefinition.PartDefinition.Name;
                if (!context.ContentItem.Has(partName))
                {
                    var part = _contentPartFactory.CreateContentPart(partName) ?? new ContentPart();
                    _partHandlers.Invoke(handler => handler.Loading(context, part), Logger);
                }

                foreach (var partFieldDefinition in typePartDefinition.PartDefinition.Fields)
                {
                    var part      = context.ContentItem.Get <ContentPart>(partName);
                    var fieldName = partFieldDefinition.Name;

                    if (!part.Has(fieldName))
                    {
                        var field = _contentFieldFactory.CreateContentField(partFieldDefinition.FieldDefinition.Name);

                        if (field != null)
                        {
                            context.ContentItem.Get <ContentPart>(partName).Weld(fieldName, field);
                        }
                    }
                }
            }
        }