Example #1
0
        /// <summary>
        ///     Returns the available composite content types for a given content type
        /// </summary>
        /// <param name="type"></param>
        /// <param name="filterContentTypes">
        ///     This is normally an empty list but if additional content type aliases are passed in, any content types containing
        ///     those aliases will be filtered out
        ///     along with any content types that have matching property types that are included in the filtered content types
        /// </param>
        /// <param name="filterPropertyTypes">
        ///     This is normally an empty list but if additional property type aliases are passed in, any content types that have
        ///     these aliases will be filtered out.
        ///     This is required because in the case of creating/modifying a content type because new property types being added to
        ///     it are not yet persisted so cannot
        ///     be looked up via the db, they need to be passed in.
        /// </param>
        /// <param name="contentTypeId"></param>
        /// <param name="isElement">Whether the composite content types should be applicable for an element type</param>
        /// <returns></returns>
        protected ActionResult <IEnumerable <Tuple <EntityBasic?, bool> > > PerformGetAvailableCompositeContentTypes(
            int contentTypeId,
            UmbracoObjectTypes type,
            string[]?filterContentTypes,
            string[]?filterPropertyTypes,
            bool isElement)
        {
            IContentTypeComposition?source = null;

            //below is all ported from the old doc type editor and comes with the same weaknesses /insanity / magic

            IContentTypeComposition[] allContentTypes;

            switch (type)
            {
            case UmbracoObjectTypes.DocumentType:
                if (contentTypeId > 0)
                {
                    source = ContentTypeService.Get(contentTypeId);
                    if (source == null)
                    {
                        return(NotFound());
                    }
                }

                allContentTypes = ContentTypeService.GetAll().Cast <IContentTypeComposition>().ToArray();
                break;

            case UmbracoObjectTypes.MediaType:
                if (contentTypeId > 0)
                {
                    source = MediaTypeService.Get(contentTypeId);
                    if (source == null)
                    {
                        return(NotFound());
                    }
                }

                allContentTypes = MediaTypeService.GetAll().Cast <IContentTypeComposition>().ToArray();
                break;

            case UmbracoObjectTypes.MemberType:
                if (contentTypeId > 0)
                {
                    source = MemberTypeService.Get(contentTypeId);
                    if (source == null)
                    {
                        return(NotFound());
                    }
                }

                allContentTypes = MemberTypeService.GetAll().Cast <IContentTypeComposition>().ToArray();
                break;

            default:
                throw new ArgumentOutOfRangeException("The entity type was not a content type");
            }

            ContentTypeAvailableCompositionsResults availableCompositions =
                ContentTypeService.GetAvailableCompositeContentTypes(source, allContentTypes, filterContentTypes,
                                                                     filterPropertyTypes, isElement);


            IContentTypeComposition[] currCompositions =
                source == null ? new IContentTypeComposition[] { } : source.ContentTypeComposition.ToArray();
            var compAliases = currCompositions.Select(x => x.Alias).ToArray();
            IEnumerable <string> ancestors = availableCompositions.Ancestors.Select(x => x.Alias);

            return(availableCompositions.Results
                   .Select(x =>
                           new Tuple <EntityBasic?, bool>(UmbracoMapper.Map <IContentTypeComposition, EntityBasic>(x.Composition),
                                                          x.Allowed))
                   .Select(x =>
            {
                //we need to ensure that the item is enabled if it is already selected
                // but do not allow it if it is any of the ancestors
                if (compAliases.Contains(x.Item1?.Alias) && ancestors.Contains(x.Item1?.Alias) == false)
                {
                    //re-set x to be allowed (NOTE: I didn't know you could set an enumerable item in a lambda!)
                    x = new Tuple <EntityBasic?, bool>(x.Item1, true);
                }

                //translate the name
                if (x.Item1 is not null)
                {
                    x.Item1.Name = TranslateItem(x.Item1.Name);
                }

                IContentTypeComposition?contentType = allContentTypes.FirstOrDefault(c => c.Key == x.Item1?.Key);
                EntityContainer[] containers = GetEntityContainers(contentType, type).ToArray();
                var containerPath =
                    $"/{(containers.Any() ? $"{string.Join("/", containers.Select(c => c.Name))}/" : null)}";
                if (x.Item1 is not null)
                {
                    x.Item1.AdditionalData["containerPath"] = containerPath;
                }

                return x;
            })
                   .ToList());
        }