Handling Start Date & End Date, with a 'To Be Confirmed' Flag?
Hi guys, I have the below document type:
Now as you can see, when someone adds something they can select whether the event doesn't have a date yet, which is fine, but if they want to add an event and the event has a date, I want that to be mandatory, so you cant add events that are neither TBC, or without a date. But that then means if the date is TBC, then you'd still have to pick a date.
How this was handled in the past was that the description would advise a rough date is required and the client could choose the rough date, then choose TBC, then that would be shown instead of the rough date.
But is there anything anyone can think of to do it any differently?
An option would be to create a custom property editor that combines the 3 fields. But I guess this an existing site. So that would bring other issues eg. you need to migrate the data from the existing 3 fields in to one.
If you don't want to go the custom property editor way, another option could be to run a check when a node is saved/published and to make sure it has appropriate properties filled out, and stop it from publishing if the fields aren't valid. Create a class that inherits from ApplicationEventHandler and append some checks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using Umbraco.Web;
public class RegisterEvents : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//append our method to the Saving event
ContentService.Saving += ContentService_Saving;
}
//Our custom checks
private void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
{
foreach(var content in e.SavedEntities)
{
//run your checks and throw the error if no value where needed.
//if the value for create date is null, etc.
if(content.GetValue("eventCreateDate") == null)
{
//cancel the event of saving and trigger a warning message
e.Cancel = true;
e.Messages.Add(new EventMessage("Warning", "Event needs either TBC enabled or an event start date.", EventMessageType.Error));
}
//add as many checks as needed
}
}
}
You can create a file in App_Code, call it RegisterEvents.cs or something like that and stick that guy in there.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'ApplicationEventHandler' could not be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 1: public class RegisterEvents : ApplicationEventHandler
Line 2: {
Line 3: protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
You'll need to add the appropriate using statements at the top of your file. I updated my previous example to reflect the using statements you may want to use. ApplicationEventHandler is part of Umbraco.Core. Here is an example of my using statements for one of my files for Umbraco Events.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using Umbraco.Web;
public class RegisterEvents : ApplicationEventHandler
{
...
}
If you're using Visual Studio, Intellisense will give you hints at using statements when you hover over squiggly lines and click on the light bulb or click on "Show Potential Fixes". I updated my first example to reflect the using statements.
Add those using statements to the top of your RegisterEvents.cs file and let us know how it goes!
Handling Start Date & End Date, with a 'To Be Confirmed' Flag?
Hi guys, I have the below document type:
Now as you can see, when someone adds something they can select whether the event doesn't have a date yet, which is fine, but if they want to add an event and the event has a date, I want that to be mandatory, so you cant add events that are neither TBC, or without a date. But that then means if the date is TBC, then you'd still have to pick a date.
How this was handled in the past was that the description would advise a rough date is required and the client could choose the rough date, then choose TBC, then that would be shown instead of the rough date.
But is there anything anyone can think of to do it any differently?
Hi Kieron,
I don't think can do that out of the box.
An option would be to create a custom property editor that combines the 3 fields. But I guess this an existing site. So that would bring other issues eg. you need to migrate the data from the existing 3 fields in to one.
Dave
It's a new site actually, so I could definitely do that, though I suspect it's not easy? D:
You probably can reuse the views or datatypes you already created for the seperate fields.
I wrote about this once : https://24days.in/umbraco-cms/2014/umbraco-angular-tips/
The article is now 4 years old. So it could be that some of the code is not really up to date anymore.
You can also have a look at the source code of my Tour Editor package to see how I reuse some built in editors (eg. slider, rte)
https://github.com/dawoe/umbraco-tour-editor/blob/develop/Source/Our.Umbraco.TourEditor/Web/App_Plugins/TourEditor/backoffice/toureditor/subviews/stepdetails.html
https://github.com/dawoe/umbraco-tour-editor/blob/develop/Source/Our.Umbraco.TourEditor/Web/App_Plugins/TourEditor/scripts/controllers/step-details-controller.js#L49
For the checkbox you could use the umb-toggle directive
Dave
Thanks for your response, upon looking through the resources it would appear im out of my depth so, for now, I will not be able to resolve this haha.
Thank you.
Not really a solution but you could add a message to help the CMS user get this correct, uEditorNotes is really good for that
https://our.umbraco.com/projects/backoffice-extensions/ueditornotes/
Hi Matthew
Not a solution indeed, but a nice work around. I use uEditorNotes a lot. Even that much they were part of 2 of my codegarden talks.
Once in make your editors happy (I think 2015) and one this year in my lightning talk about tours.
Dave
Hi Matthew,
Not a solution indeed. But a nice work around. I think I use uEditorNotes in 90% of my projects.
I like them so much that I mentioned it in 2 Codegarden talks.
Dave
Hi Kieron,
Update: added using statements as per below
If you don't want to go the custom property editor way, another option could be to run a check when a node is saved/published and to make sure it has appropriate properties filled out, and stop it from publishing if the fields aren't valid. Create a class that inherits from ApplicationEventHandler and append some checks.
You can create a file in App_Code, call it RegisterEvents.cs or something like that and stick that guy in there.
Let us know how it goes!
I'd like to try this method, but I don't have an App_Code folder, which is a good start lol.
Haha! Then that's easy to remedy, create one in your website's root folder ;)
Here's all the events reference
Hey, thanks that bit was easier than i thought :D
Ive added it in and currently facing this one:
https://gyazo.com/ca3b39f8f8dc91d0e0c8f09dbc7bd6d5
Thanks for your help so far!
Hi Kieron,
You'll need to add the appropriate using statements at the top of your file. I updated my previous example to reflect the using statements you may want to use. ApplicationEventHandler is part of Umbraco.Core. Here is an example of my using statements for one of my files for Umbraco Events.
If you're using Visual Studio, Intellisense will give you hints at using statements when you hover over squiggly lines and click on the light bulb or click on "Show Potential Fixes". I updated my first example to reflect the using statements.
Add those using statements to the top of your RegisterEvents.cs file and let us know how it goes!
is working on a reply...