Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Autonaming block

Vote:
 

Have somebody done something about of to auto-name a Block depending from the properties selected?

#120002
Apr 08, 2015 19:27
Vote:
 

Hi Alfredo,

I'm not sure I understand your question. Could you be a bit more specific?

#120004
Apr 08, 2015 20:57
Vote:
 

Sure, I have block type class like this:

using System.ComponentModel.DataAnnotations;

<pre>
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Web;
using EPiServer;
using NextGen.CMS.Models.Media;
using EPiServer.Shell.ObjectEditing;
using EPiServer.ServiceLocation;
using System;

namespace NextGen.CMS.Models.Blocks.Silverpop
{
        [ContentType(DisplayName = "Blog Offer Type Block"
        , GUID = "a7a0a0a7-e644-447b-9605-6996b387f89d"
        , GroupName = "Silverpop Offer Types"
        , Description = "The widget displays a blog offer.")]
    public class BlogOfferBlock : ContentData
    {

        /// <summary>
        /// Gets or sets ProductInterestType.   
        /// </summary>
        /// <value>
        /// ProductInterestType.
        /// </value>
        [Display(
           Name = "Product Interest",
           Description = "Product Interest",
           GroupName = SystemTabNames.PageHeader,
           Order = 101)]
        [SelectMany(SelectionFactoryType = typeof(ProductInterestsFactory))]
        public virtual string ProductInterestType { get; set; }

        /// <summary>
        /// Gets or sets RegionType.
        /// </summary>
        /// <value>
        /// RegionType.
        /// </value>
        [Display(
           Name = "Region",
           Description = "Region",
           GroupName = SystemTabNames.PageHeader,
           Order = 102)]
        [SelectMany(SelectionFactoryType = typeof(MemberRegionsFactory))]
        public virtual string RegionType { get; set; } 

        /// <summary>
        /// Gets or sets PreferenceType.
        /// </summary>
        /// <value>
        /// PreferenceType.
        /// </value>
        [Display(
           Name = "Preferred Destination",
           Description = "PreferredDestination",
           GroupName = SystemTabNames.PageHeader,
           Order = 103)]
        [SelectMany(SelectionFactoryType = typeof(PreferredDestinationFactory))]
        public virtual string PreferredDestinationType { get; set; }
    }
}

</pre>

Very simple, right?, well when the content editor (the user) creates a new block from this block type class he/she lets the default name, "New Block 1" by example, then when the content editor changes/sets the property values, I must read the property values and change, to build a string, already I have the process to do it, after, when the content editor will publish the block I must change the block name (if we remember the block was created with the "New Block 1" name) insted the new name, by example:

Content editor's selection:

ProductInterestType: 1,3

RegionType: 4

PreferredDestinationType: 5,7

And the property values corresponds to

ProductInterestType: Cruise Interest(1),(Hotel Interest)3

RegionType: (Southeast)4

PreferredDestinationType: Caribbean Exotic(7)

Then, the block name will be:

"Blog Offer for Prod: Cruise, Hotel - Rg: Southeast Dest: Caribbean"

To do it I need to catch the publish event (I think), hope to be clear

#120006
Apr 08, 2015 21:21
Vote:
 

Hook to SavingContent event and set the built-in Name (from IContent) to your name. If you do it in publishing event - you may still get dialog window in UI asking you to consider renaming default block name to something more meaningful :)

#120009
Apr 08, 2015 21:45
Vote:
 

Sorry, must The SavingContent event hook in the model or the controller?

#120011
Apr 08, 2015 21:49
Vote:
 

You need to use an initializable module:

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class InitializationModule1 : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var contentEvents = context.Locate.Advanced.GetInstance<IContentEvents>();

        contentEvents.SavingContent += this.SavingContent;
    }

    public void Uninitialize(InitializationEngine context)
    {
            
    }

    private void SavingContent(object sender, ContentEventArgs e)
    {
        var block = e.Content as BlogOfferBlock;

        if (block != null)
        {
            e.Content.Name = "Custom name"
        }
    }
}
#120020
Apr 08, 2015 22:05
Vote:
 

Thanks a lot! I owe you a coffe with chocolate cake!

#120022
Apr 08, 2015 22:08
Vote:
 

but I guess that you will still receive this annoying dialog window - as saving and publishing events are triggered on the server, but check for default name happens in client-side. haven't tested. just speculations.

#120024
Apr 08, 2015 22:09
Vote:
 

Lucky Johan! :D

#120028
Apr 08, 2015 22:34
Vote:
 

Hehe. When and where?

#120029
Apr 08, 2015 22:35
Vote:
 

The magic was done, Valdis, I did several test because I have a lot of blocks where I had to applied the change and there was no problem with any message box, I can to share the code if anybody asks me for it, thanks again to Johan and Valdis.

#120030
Apr 09, 2015 1:28
Vote:
 

I'm from Mexico City, where can I know your office addresss? Valdis and Johan, I'll find the way to express my thankful

#120031
Apr 09, 2015 1:34
Vote:
 

Thanks anyway, no need. Sending something from Mexico to Europe would be overkill :)

#120032
Apr 09, 2015 6:32
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.