I've been spending a bit of time with Sitecore recently, and I noticed the site I am working on has a copyright date in a Rich Text field. Unfortunately, it is 2012. Now, the easy way to fix this problem is to simply go in and change the Rich Text field which has the copyright information, but I don't want to have to worry about changing it in 2014.
Is there a way to insert dynamic text into the Text control? Even if I could have a sigil which I could manually replace in C# that would be preferable to either switching the Text for a Literal or force manual updates every year.
You can add your own processor to the renderField pipeline, check whether current field is RichText field and replace a token (e.g. __YEAR__) with current year:
<renderField>
<!--... other processors -->
<processor type="My.Assembly.Namespace.ReplaceTokenProcessor, My.Assembly" />
<!--... other processors -->
</renderField>
and the code of the processor:
namespace My.Assembly.Namespace
{
public class ReplaceTokenProcessor
{
public virtual void Process(RenderFieldArgs args)
{
if (args.FieldTypeKey != "rich text")
return;
args.Result.FirstPart = (args.Result.FirstPart == null) ? null : args.Result.FirstPart.Replace("__YEAR__", DateTime.Now.Year.ToString());
args.Result.LastPart = (args.Result.LastPart == null) ? null : args.Result.LastPart.Replace("__YEAR__", DateTime.Now.Year.ToString());
}
}
}
The solution Maras proposed works, but to me it sounds like a bit overkill for just one date in a RTE field.
Besides that, every RTE field that is rendered on every single page in your website is processed.
If it is only for one Year-value I would use a Literal and fill and replace the Text property of the Literal in Codebehind:
Literal.Text = FieldRenderer.Render(Context.Item, "RTE_FieldName")
.Replace("__YEAR__", DateTime.Now.Year.ToString());
Then add caching to the sublayout and it is only rendered once after the cache has been cleared.
Related
Im new to vue.js, and Im trying to do a simple if with a date field within my ASP.NET MVC project.
I've seen examples on vue tutorials where the condition is done in the js file, but Im not convinced that this would help me.
I currently have a date field in my ViewModel that has two drop downs. This is for an application form that asks a person how long have they lived at their address. They can select up to 11 months and 6+ years. I have a button that allows them to add another address. When you click this button, the viewModel for the date fields are repeated so they can add another one (stored in a List).
However I want to wrap this around a condition that says only if the date entered is less than 6 years then the button would appear. Like I said, at the min it is there always.
my code for the address and its button is:
<fieldset>
<legend>Please provide all your addresses in the last 6 years</legend>
#Html.EditorFor(m => m.PreviousAddresses)
</fieldset>
<button class="button">Add another previous address</button>
What im trying to do is something like v-if="PreviousAddress < 6" but I dont think thats right. Can someone help with the syntax?
EDIT: The validation works on the back end. Just need the client side to work with it:
[Minimum(72, ErrorMessage = "Please enter 6 years address history")]
public int TotalAddressHistoryInMonths
{
get
{
int totalMonths = CurrentAddress?.Duration?.TotalPeriodInMonths ?? 0;
if (PreviousAddresses != null)
{
foreach (ResidencyInputViewModel residency in PreviousAddresses)
{
totalMonths += residency?.Duration?.TotalPeriodInMonths ?? 0;
}
}
return totalMonths;
}
}
The solution you have appears to be correct, but can you post the rest of your code. Seeing the data object should help.
I am working on a Vaadin (7) server-side application, and i need to use a TextArea or a RichTextArea that will analyze word-by-word the typed input, and will highlight words of a certain type, for example - dates and times.
My problem is that a RichTextArea does not have a TextChangeListener, and a regular TextArea does not have a highlighting option because it does not support HTML tags...
I tries using ShortcutKeyListener for RichTextArea and analyze the text after every Space key, but it was too slow and had also some other problems.
Is there anything else i can do?
Is there an option to analyze the text on real time when using RichTextArea? or is there any add-on youre familiar with that can do that?
Or is there a way to highlight text in TextArea after analyzing it?
Thank you!
My suggestion is a bit strange, but anyway, take a look on Vaadin AceEditor. It supports text mode and SelectionChangeListener:
ed.addSelectionChangeListener(new SelectionChangeListener() {
#Override
public void selectionChanged(SelectionChangeEvent e) {
int cursor = e.getSelection().getCursorPosition();
Notification.show("Cursor at: " + cursor);
}
});
See details here: Vaadin AceEditor
When I'm binding text to an input on iOS, my setter is called each time a character is added, on iOS, but not on Android.
If I put a breakpoint on a property that is binded to a TextField in iOS, each time a character is entered, the property setter will be called, but not on an Android EditText.
It makes more complex ViewModels with several input attached to getter/setter tested on iOS completely useless on Android since it cannot be used.
Is there a way to make the "MvxBind="Text SomeProperty" acting like iOS on Android?
Events like "AfterTextChanged" (any binding to a command) aren't property-friendly, and would break my ViewModel. I don't want to have a platform-dependent workaround.
[Edit]
// Droid. It calls the TotalAmount setter once the editing is done.
<EditText local:MvxBind="Text TotalAmount,
Mode=OneWayToSource; Text TotalAmountString, Mode=OneWay" />
// Touch. It calls the TotalAmount setter on key press.
set.Bind(MyTotalAmountTextField)
.For(v => v.Text)
.To(vm => vm.TotalAmount).OneWayToSource();
set.Bind(MyTotalAmountTextField)
.For(v => v.Text)
.To(vm => vm.TotalAmountString).OneWay();
By the way, the displayed property is always formatted with a dollar sign, that's why I'm using an half-duplex approach for binding.
Appart from this live (iOS) versus after-edit (Droid) problem, the bindings are working well.
The default behaviour for TwoWay Text binding on both Android and iOS is to do per character binding.
You can see this behaviour in, for example, the N=0 video at 18:43 - http://youtu.be/_DHDMNB_IeY?t=18m43s
If you are not seeing this behaviour in your EditText then I guess it might be down in some way to your app or perhaps to a bug (e.g. perhaps in OneWayToSource binding somehow - this certainly isn't as commonly used as other binding modes).
To workaround this, I can only think to suggest:
Log it as an issue with a reproducible case (github repo) on GitHub/MvvmCross - someone there might be able to help - or you might be able to fix it yourself.
Try TwoWay binding instead
Try creating your own custom binding or your own custom control - this is actually very easy to do - see the tutorials N=28 and n=18 on http://mvvmcross.blogspot.com - for example you could try inheriting from EditText to create something like;
public class MyEditText : EditText {
public MyEditText(Context c, IAttributeSet a) {
this.AfterTextChanged += (s,e) => MyTextChanged.Raise(this);
}
public event EventHandler MyTextChanged;
public string MyText {
get { return Text; }
set { Text = value; }
}
}
My custom control contains a repeater that adds a dynamic control into a placeholder on ItemDatabound.
I'm having an issue accessing the updated value of the dynamic control, I am already taking care of rebuilding the dynamic controls on Load but I first need to get to the changes made by the user. I'm just having some trouble understanding where in the Lifecycle is the best place to have access to the updated dynamic control value.
<Repeater>
<ItemTemplate>
<Label /><PlaceHolder />
If anyone who stumbles across this page this is the website that got me on the right track:
http://www.learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx
In my case the control I was putting inside my placeholder was also dynamic (data-driven) based on a enum type which determined if a CheckBox, ListBox, Textbox, RadDatePicker, ect. would be inserted in the placeholder.
Since I had a repeater with multiple placeholders instead of just one placeholder containing all of my dynamic controls like the link provided, I implemented my solution as follows.
On the method that adds your dynamic controls to the placeholder (ItemDataBound):
1. Give the dynamic control a unique ID (string)
2. Add the unique ID & enum type to the Dictionary<enum, string> that will be stored in the ViewState
Override the LoadViewState method as follows (this will load your Dictionary<enum, string> array):
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
}
Override the OnLoad method to add the dynamic controls that were cleared on postback:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (IsPostBack)
AddDynamicControlsToPlaceholder();
}
private void AddDynamicControlsToPlaceholder()
{
foreach (RepeaterItem item in reapeater.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var keyValue = DynamicDictValues.ElementAt(item.ItemIndex);
var plhDynControl = item.FindControl("plhDynControl") as PlaceHolder;
//CreateDynamicControl method uses the key to build a specific typeof control
//and the value is assigned to the controls ID property.
var dynamicControl = CreateDynamicControl(keyValue);
plhItemValue.Controls.Add(dynamicControl);
}
}
}
You still have to implement the code that loops through the repeater and pulls the updated client-side values from the dynamic controls. I hope this helps, it really took a lot of work getting this one solved.
So, after much research on whether or not we should the CEWP or the HTML Field Control on an external facing SharePoint site, we settled on using the Field Control (much thanks to AC). Now, we are having an issue that all the blogs I read say should not be an issue.
When we put a relative URL into the HTML Editor and hit OK, it is automatically changed to an absolute URL. This is apparently a "feature" of Internet Explorer from some of the research I have been doing. TinyMCE has a work around for this. I was wondering if there was some work around for the SharePoint control that I am missing.
This is kind of a big issue for us because we have an authoring site and the www site. So, when the authoring is done on the authoring site and all the links get migrated to the www site, they are http:// authoring.domain.com/en-us/Pages/... instead of /en-us/Pages/...
I encountered this issue as well. We had custom site fields and content types deployed via feature. The RichText property of the HTML Field is properly as true in caml, but once deployed the SPField in the root web fields collection and every Pages list the RichText attribute becomes false.
I was able to successfully resolve the issue by using a feature receiver on the feature that deploys the site columns and content types. My code loops every web in the site and then iterates over the fields to update them.
code snippet:
private void processweb(SPWeb web)
{
SPList list = web.Lists["Pages"];
SPField field;
for (int i = 0; i < list.Fields.Count; i++)
{
field = list.Fields[i];
//to work around a sharepoint defect ... make html fields work in richtext mode
if (field != null && string.Compare(field.TypeAsString, "HTML", true) == 0 && (field as SPFieldMultiLineText).RichText == false)
{
(field as SPFieldMultiLineText).RichText = true;
field.Update(true);
}
}
foreach (SPWeb w in web.Webs)
{
processweb(w);
}
}