I wrote a basic "Label" fieldtype the other day for a project, with an option to hide the caption, so it should work for both of your needs. Here is the code below. Will look at getting this added in the Contour Contrib package. Let me know if you have any input or suggestions to improve it. Thinking about letting it read the text from properties on a page eventually.
public class Label : FieldType { [Umbraco.Forms.Core.Attributes.Setting("Text To Show",prevalues="", description="Enter the text to show in the label.", control="Umbraco.Forms.Core.FieldSetting.TextArea")] public string TextToShow {get;set;}
[Umbraco.Forms.Core.Attributes.Setting("Hide Field Caption", prevalues = "", description = "Select to hide the label and only show the text", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")] public string HideFieldCaption { get; set; }
public List<Object> Value;
public Label() { Id = new Guid("57ADD3AB-C3CD-464D-BCC2-3D1C05D3BF67"); Name = "Label"; Description = "A read-only field used to display blocks of text"; Icon = "textfield.png"; DataType = FieldDataType.String; Value = new List<object>();
}
public override bool HideLabel { get { return Convert.ToBoolean(HideFieldCaption); } set { base.HideLabel = value; } }
public override System.Web.UI.WebControls.WebControl Editor { get { System.Web.UI.WebControls.Panel pnl = new System.Web.UI.WebControls.Panel(); pnl.ID = "pnlHolder"; pnl.CssClass = "contourlabelfield"; pnl.Controls.Add(new System.Web.UI.LiteralControl("<p>" + umbraco.library.ReplaceLineBreaks(TextToShow) + "</p>")); return pnl; } set { base.Editor = value; } }
public override bool SupportsRegex { get { return false; } }
public override bool SupportsPrevalues { get { return false; } }
Hi Tom and all, thanks a lot for your code, it's really usefull for my last need. But I have a problem with the email received, without the 'TextToShow'. Whatdid I do wrong?
Label1 should be hidden (HideLabel = true) and the TextToShow should be visible (but no value is sended).
Hi Tom. Now the field is sent (and saved). My need was to clarify the email that the customer receives. Notnecessarilyto save,but justsend. Sometimes 'TextToShow' may contains information about the fields below it. Example split shipping address with the billing or similar. It would be interesting to be able to hide the caption (required by default) from email if we decided to hide it on the page (but this is a minor problem). Thanks a lot.
I see what you mean. I don't think there's any built in way to hide the caption from the email. However you could do this by sending your own XSLT Transformed email and skipping that field. I could possibly add an option for this and maybe adjust the Extended Workflow Emails in Contour Contrib to adhere to it, but it might be misleading because it won't affect the default email workflows..
While playing around with the custom field type, I got into an error and the backend UI stopped loading for one form.
Object reference not set to an instance of an object, this was working fine till yesterday.
The issue is in "umbraco.library.ReplaceLineBreaks(TextToShow)". Any inputs? It seems that TextToShow is null at the time of loading the backend UI.
Will this cause any problems for a Field Group (No name provided) with the label as the only Field Type added in that. This was the only change I have made in the working form. The save was failing in between. I will be restoring the db back and trying this. If you have any inputs please let me know.
Any inputs appreciated. I have restored the db back and everything started working again. But I am not able to Save the form after Moving the Label position to top and changing the text. I am still getting the same error. The database connection is bit slow. Will that cause this problem?
{"Message":"Object reference not set to an instance of an object.","StackTrace":" at umbraco.library.ReplaceLineBreaks(String text)\r\n at Contour.SharedSource.Providers.FieldTypes.Label.RenderPreview()\r\n at UmbracoContour.Webservices.Designer.RenderFieldPreview(String fieldTypeGuid, String[] prevalues)","ExceptionType":"System.NullReferenceException"}
Not sure about the internal working of contour . It seems that that the TextToShow in the Code getting null values, I am not sure about why this is happening. Please let me know. Appreciate your help in this regard.
I think the issue was related to the db connectivity and not the field type. I think, it made the prev value error for not getting values. Once the db is restored and database connectivity up to norml, this issue didn't arise.
Wondering if anyone can shed any light on why the public properties are rendering fine in the Contour UI but they are returning null when trying to access their values?
[global::Umbraco.Forms.Core.Attributes.Setting("Text To Show", prevalues = "",
description = "Enter the text to show in the label.", control = "Umbraco.Forms.Core.FieldSetting.TextArea")]
public string TextToShow { get; set; }
[global::Umbraco.Forms.Core.Attributes.Setting("Hide Field Caption", prevalues = "",
description = "Select to hide the label and only show the text",
control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
public string HideFieldCaption { get; set; }
How/where are you trying to access these? The class itself accesses them fine to determine whether to show/hide the label etc. I might be misunderstanding though :)
Also make sure you are using the latest source - I probably made some updates along the way after this was posted
Hi Tom, I've grabbed the latest source but still when saving the field displays "failed to get preview" and stepping into the code the following line throws a null reference exception because "TextToShow" is null:
While debugging the following was also throwing an exception because the value returned was "checked" and therefore not valid for parsing as a boolen:
public override bool HideLabel
{
get
{
return Convert.ToBoolean(HideFieldCaption);
}
set
{
base.HideLabel = value;
}
}
Perhaps things have changed in the latest version of Contour? The text I enter into the TextToShow field is saved in the form but I get no output on the clientside.
You're right, something does appear to have changed with the checkbox. As a quick workaround you could change the HideLabel getter to return HideFieldCaption == "checked"; and the check in the editor to if (this.HideLabel). This seems to work although the checkbox doesn't maintain it's state in the field properties - when you edit the field again it's unchecked. I'll need to look more into this soon.
The "failed to get preview" is normal - it should show the real text once you click save on the form. I think it's a Contour limitation.
With the above change it seems to work for me...again I'll look into a permanent fix when I get a few minutes, or let me know if you find anything :)
heres my C# version - Umbraco 4.7.2 Contour 3.0.9. Prob a few people out there like me still looking for a solution.
Firstly, I'm a VB programmer and first time using Contour so feel free to make this code better, but after a lot of reading this is a combination of Tom's work above and others. It seems to work ok and covers most of the requests. Label Titles and Label Text can optionally be hidden and different classes allow for the text or title to be styled independantly, and/or vary if one or the other are hidden.
So from the beginning (incase you want to duplicate):
Start a new C# class project using VS2010 (or VC#2010) or later as it must support .NET 4, otherwise the reference to Umbraco.Forms.Core, and any other referenced dll's compiled using .NET4, wont work. Microsoft offer the Express versions of Visual Studio 2010 and 2012 for free at this time. VC# 2010 Express with VS2010 Sp1 installed is what I used to create this project. VS2012 uses .NET4.5 so I stuck with VC#2010.
The project must include references to: "System", "System.Web", "umbraco" and "Umbraco.Forms.Core". The Umbraco/Contour dll's are usually in the website /bin folder.
Then place all the code below in the clsContourLabel.cs file
Finally compile the project and copy the .dll to your website /bin folder or copy the .cs file to the app_code directory. The website will now restart and your type will be registered automatically, no configuration needed. I used the dll.
My .cs file:
using System; using System.Collections.Generic; using System.Web.UI.WebControls; using Umbraco.Forms.Core;
namespace nsContourLabel { public class ContourLabel : FieldType { [Umbraco.Forms.Core.Attributes.Setting("Text To Display", prevalues = "", description = "Enter the text to display.", control = "Umbraco.Forms.Core.FieldSetting.TextArea")] public string TextToShow { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Hide Title", prevalues = "", description = "Select to hide the title", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")] public string HideFieldCaption { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Hide Text", prevalues = "", description = "Select to hide the text", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")] public string HideFieldValue { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Is Heading", prevalues = "", description = "Select to display the label and/or text as a heading", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")] public string IsHeading { get; set; }
public System.Web.UI.WebControls.Label _lb; public List<Object> _value;
public ContourLabel() { //Provider this.Id = new Guid("3F92E01B-29E2-4a30-BF33-9DF5580ED52D"); this.Name = "Label"; this.Description = "Renders a paragraph of text"; this.Icon = "textfield.png"; this.DataType = FieldDataType.LongString; _value = new List<object>(); }
public override WebControl Editor { get { _lb = new System.Web.UI.WebControls.Label(); _lb.CssClass = "label-text";
Label without text custom field and text without label custom field... for nice forms
Hi i have been looking in the forum to get examples of the code to use or to put in contour, and make custom fieldtypes for a:
Label without text (just the label) fieldstype
and
Text without the Label (like a paragraph to explain how to fill a section in the form)
but i dont have any luck yet,
do anyone has some working code of clases example whit this information, or something to start to build a custom class?
Thanks a lot for any help!
Hi Andres,
I wrote a basic "Label" fieldtype the other day for a project, with an option to hide the caption, so it should work for both of your needs. Here is the code below. Will look at getting this added in the Contour Contrib package. Let me know if you have any input or suggestions to improve it. Thinking about letting it read the text from properties on a page eventually.
Thanks,
Tom
Hi Tom, wonderfull works perfect, i hope you can make it part of Contour Contrib Package,
Thanks a lot for the help!
Hi Tom and all, thanks a lot for your code, it's really usefull for my last need.
But I have a problem with the email received, without the 'TextToShow'. What did I do wrong?
Label1 should be hidden (HideLabel = true) and the TextToShow should be visible (but no value is sended).
Really thanks
Name: aaa Country: bbb
Label1:
EMPTY...
Comments: CCC
Hi,
The code above doesn't actually save the value of the label, which is why you wouldnt see anything in the email or entries viewer.
You should be able to make it save the value by changing Value.Add(string.Empty); to Value.Add(TextToShow);
Perhaps I will add an option to save or not save the value when I add it to the Contrib project - I've added a few other things also.
Thanks,
Tom
Hi Tom.
Now the field is sent (and saved). My need was to clarify the email that the customer receives. Not necessarily to save, but just send.
Sometimes 'TextToShow' may contains information about the fields below it. Example split shipping address with the billing or similar.
It would be interesting to be able to hide the caption (required by default) from email if we decided to hide it on the page (but this is a minor problem).
Thanks a lot.
Hi,
I see what you mean. I don't think there's any built in way to hide the caption from the email. However you could do this by sending your own XSLT Transformed email and skipping that field. I could possibly add an option for this and maybe adjust the Extended Workflow Emails in Contour Contrib to adhere to it, but it might be misleading because it won't affect the default email workflows..
-Tom
Hi Tom,
While playing around with the custom field type, I got into an error and the backend UI stopped loading for one form.
Object reference not set to an instance of an object, this was working fine till yesterday.
The issue is in "umbraco.library.ReplaceLineBreaks(TextToShow)". Any inputs? It seems that TextToShow is null at the time of loading the backend UI.
Will this cause any problems for a Field Group (No name provided) with the label as the only Field Type added in that. This was the only change I have made in the working form. The save was failing in between. I will be restoring the db back and trying this. If you have any inputs please let me know.
Thanks,
Rajeev
Hello Tom,
Any inputs appreciated. I have restored the db back and everything started working again. But I am not able to Save the form after Moving the Label position to top and changing the text. I am still getting the same error. The database connection is bit slow. Will that cause this problem?
{"Message":"Object reference not set to an instance of an object.","StackTrace":" at umbraco.library.ReplaceLineBreaks(String text)\r\n at Contour.SharedSource.Providers.FieldTypes.Label.RenderPreview()\r\n at UmbracoContour.Webservices.Designer.RenderFieldPreview(String fieldTypeGuid, String[] prevalues)","ExceptionType":"System.NullReferenceException"}
Not sure about the internal working of contour . It seems that that the TextToShow in the Code getting null values, I am not sure about why this is happening. Please let me know. Appreciate your help in this regard.
thanks,
Rajeev
Hi,
I think the issue was related to the db connectivity and not the field type. I think, it made the prev value error for not getting values. Once the db is restored and database connectivity up to norml, this issue didn't arise.
Thank you,
Rajeev
Wondering if anyone can shed any light on why the public properties are rendering fine in the Contour UI but they are returning null when trying to access their values?
Hi Simon,
How/where are you trying to access these? The class itself accesses them fine to determine whether to show/hide the label etc. I might be misunderstanding though :)
Also make sure you are using the latest source - I probably made some updates along the way after this was posted
-Tom
Hi Tom, I've grabbed the latest source but still when saving the field displays "failed to get preview" and stepping into the code the following line throws a null reference exception because "TextToShow" is null:
While debugging the following was also throwing an exception because the value returned was "checked" and therefore not valid for parsing as a boolen:
Perhaps things have changed in the latest version of Contour? The text I enter into the TextToShow field is saved in the form but I get no output on the clientside.
Cheers, Simon
Hi Simon,
You're right, something does appear to have changed with the checkbox. As a quick workaround you could change the HideLabel getter to return HideFieldCaption == "checked"; and the check in the editor to if (this.HideLabel). This seems to work although the checkbox doesn't maintain it's state in the field properties - when you edit the field again it's unchecked. I'll need to look more into this soon.
The "failed to get preview" is normal - it should show the real text once you click save on the form. I think it's a Contour limitation.
With the above change it seems to work for me...again I'll look into a permanent fix when I get a few minutes, or let me know if you find anything :)
-Tom
Thanks Tom, managed to get it working ok now.
heres my C# version - Umbraco 4.7.2 Contour 3.0.9. Prob a few people out there like me still looking for a solution.
Firstly, I'm a VB programmer and first time using Contour so feel free to make this code better, but after a lot of reading this is a combination of Tom's work above and others. It seems to work ok and covers most of the requests. Label Titles and Label Text can optionally be hidden and different classes allow for the text or title to be styled independantly, and/or vary if one or the other are hidden.
So from the beginning (incase you want to duplicate):
My .cs file:
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Umbraco.Forms.Core;
namespace nsContourLabel
{
public class ContourLabel : FieldType
{
[Umbraco.Forms.Core.Attributes.Setting("Text To Display", prevalues = "", description = "Enter the text to display.", control = "Umbraco.Forms.Core.FieldSetting.TextArea")]
public string TextToShow { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Hide Title", prevalues = "", description = "Select to hide the title", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
public string HideFieldCaption { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Hide Text", prevalues = "", description = "Select to hide the text", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
public string HideFieldValue { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Is Heading", prevalues = "", description = "Select to display the label and/or text as a heading", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
public string IsHeading { get; set; }
public System.Web.UI.WebControls.Label _lb;
public List<Object> _value;
public ContourLabel()
{
//Provider
this.Id = new Guid("3F92E01B-29E2-4a30-BF33-9DF5580ED52D");
this.Name = "Label";
this.Description = "Renders a paragraph of text";
this.Icon = "textfield.png";
this.DataType = FieldDataType.LongString;
_value = new List<object>();
}
public override WebControl Editor
{
get
{
_lb = new System.Web.UI.WebControls.Label();
_lb.CssClass = "label-text";
if (IsHeading == "checked") _lb.CssClass +=" label-heading";
if (HideFieldCaption == "checked" && HideFieldValue != "checked") _lb.CssClass += " label-nocaption";
if (HideFieldCaption != "checked" && HideFieldValue == "checked") _lb.CssClass += " label-novalue";
if (HideFieldCaption == "checked" && HideFieldValue == "checked") _lb.CssClass = "label-hidden";
_lb.Text = TextToShow;
return _lb;
}
set
{
base.Editor = value;
}
}
public override bool HideField
{
get
{
return HideFieldValue == "checked";
}
set
{
base.HideField = value;
}
}
public override bool HideLabel
{
get
{
return HideFieldCaption == "checked";
}
set
{
base.HideLabel = value;
}
}
public override bool SupportsRegex
{
get { return false; }
}
public override bool SupportsPrevalues
{
get { return false; }
}
public override string RenderPreview()
{
if (IsHeading == "checked")
{
return umbraco.library.ReplaceLineBreaks(TextToShow);
}
return "<p>" + this.TextToShow + "</p>";
}
public override string RenderPreviewWithPrevalues(List<object> prevalues)
{
return RenderPreview();
}
}
}
is working on a reply...