Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • trfletch 598 posts 604 karma points
    Jul 25, 2011 @ 13:32
    trfletch
    0

    Auto-populating the create "Name" field

    Hi All,

    I have an Umbraco V4.7 website, the website is going to be used to add some listings and I want the nodeName of these listings to be a unique reference number. I realise that the best number to use is likely to be the nodeId because this is going to alway be unique. Which brings me to the question, is it possible to auto populate the "Name" field that appears when you create a new node?

    Ideally I would like to populate this field with the node Id but I realise that this may not be possible because it does not have a node Id yet. Therefore if this is not possible, would it be possible to populate it with something generic such as "Listing" and then as soon as I click create the nodeName is changed to the Node ID?

    So the ideal solution would be for the user to click create, and if the document type is "Listing" then the "Name" field populates with "Listing" so all they need to do is click "Create" again and the node will be created with the nodeName being the nodeId.

    Hopefully this makes sense and I appreciate any feedback.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 25, 2011 @ 18:06
    Sebastiaan Janssen
    1

    I haven't investigated very well so I don't know for sure, but I don't think it's possible to prepopulate the name field.

    However, in /umbraco/config/create/UI.xml there's a reference to (/umbraco)/create/content.ascx - You could try and change the codebehind for this usercontrol to use your own logic. However, be aware that if you ever upgrade your site, you will have to re-implement these changes as this file will get overwritten!

    What I do currently is tell the editor to just enter something (anything, it really doesn't matter) and then in a Document.AfterSave event I detect which documenttype it is and only for the "Listing" types I would just always change the name to something (like the NodeId).

  • trfletch 598 posts 604 karma points
    Jul 25, 2011 @ 18:14
    trfletch
    0

    Hi Sebastiaan,

    Thank you for the response, I thought that I might need to use an event handler. Is the AfterSave on going to be the best one to use or is there one that would change it as soon as you click the "Create" button? Are there any blogs etc that you know of that might give me some help in setting up an event handler to change the nodename? I am only just learning .net and I have used event handlers before but never to change a property of a node.

    Regards

    Tony

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 25, 2011 @ 18:28
    Sebastiaan Janssen
    2

    Not sure if changing it earlier would do you any good, you're not really going to see the name anyway unless you really go look for it.

    This should work for you:

     

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;

    namespace SomeCompany.EventHandlers
    {
        public class ListingTitleHandler : ApplicationBase
        {
            public ListingTitleHandler()
            {
                CMSNode.AfterSave += SetListingTitle;
            }
            private static void SetListingTitle(object sender, SaveEventArgs e)
            {
                if (sender is Document == false) return;

                var doc = sender as Document;
                if (doc == null || doc.ContentType == null || doc.ContentType.Alias != "Listing") return;
                doc.Text = doc.Id.ToString();
            }
        }
    }

    You could try handling the event CMSNode.AfterNew (change SaveEventArgs to NewEventArgs), but that would only change it once (after it is first created) and doesn't prevent people from changing it later on. So I think AfterSave might be better for your purposes.

     

  • trfletch 598 posts 604 karma points
    Jul 26, 2011 @ 10:30
    trfletch
    0

     

    Hi Sebastian,

    Thank you that is working perfectly, the only minor issue is that there is a slight delay when creating a node before the details appear. Is this normal because it is running the event handler? I am using the following code:

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Linq;

    using

     

    System.Web;

    using

     

    umbraco.BusinessLogic;

    using

     

    umbraco.cms.businesslogic;

    using

     

    umbraco.cms.businesslogic.web;

    namespace

     

    Umbraco_Lettings

    {

    publicclassEventHandlers :

    ApplicationBase

    {

    public

    EventHandlers()

    {

    CMSNode

    .AfterSave += Document_AfterSave;

     

    }

    #region

     

    AfterSave events

    privatestaticvoid Document_AfterSave(object sender, SaveEventArgs

    e)

    {

    if (sender isDocument == false) return

    ;

    var doc = sender asDocument

    ;

    if (doc == null || doc.ContentType == null || doc.ContentType.Alias != "Letting") return

    ;

    doc.Text = doc.Id.ToString();

     

    }

    #endregion

    }

    }

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 26, 2011 @ 11:00
    Sebastiaan Janssen
    0

    Nah, the delay must've been there already.
    Since you're using the AfterSave event handler, it shouldn't be causing delays during the creation of the new page because the eventhandler only kicks in after the page gets saved. ;)

  • trfletch 598 posts 604 karma points
    Jul 26, 2011 @ 11:03
    trfletch
    0

    It is kicking in when I create a new page though because the node appears with the NodeID as the name straight away before doing any saving. Which to be honest is exactly what I was looking for, I just wondered if it could be causing the delay.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 26, 2011 @ 11:05
    Sebastiaan Janssen
    1

    D'oh! Of course, because Umbraco itself needs to save the new node as well, yes, that would cause a slight extra delay then. :)

    Ps. Don't forget to mark a post as the solution, thanks!

  • trfletch 598 posts 604 karma points
    Jul 26, 2011 @ 11:07
    trfletch
    0

    Ok cheers, think part of the delay may also be caused by the Google maps datatype package that I am using on the nodes in question. Thank you for your help

Please Sign in or register to post replies

Write your reply to:

Draft