Media selection control and alternate text

   —   
By default the Media selection control contains an alternate text field, unfortunately the purpose of this field is to enter alternate text directly in combination with CKEditor in an Editable region.  Since this alternate text is stored in the CMS_Document table with the document's content, the text is rendered directly on the page and it cannot be used within transformations.
The Alternative text should only be used when you are inserting the media library file directly into the Editable region. But if you want to add a Media selection control to any document type and allow users to enter some alternate text, here is the solution:
 
1. Create a custom form control which will be copy of \CMSFormControls\Basic\MediaSelectionControl.ascx (+ .cs file) with name e.g. MediaImageSelectionControl.ascx
 
2. Open this control in Visual Studio and add a Text field where alternative text will be entered:
<asp:TextBox ID="txtAlt" runat="server" /> <cms:MediaSelector ID="mediaSelector" runat="server" />

3. Make some changes in the code-behind (.cs) file to handle alternative text correctly:
at the beginning of your class create a private variable:
#region "variables" private string altText = ""; #endregion

modify the Value object:
public override object Value { get { return mediaSelector.Value + "|" + txtAlt.Text; } set { string myValue = ValidationHelper.GetString(value, null); mediaSelector.Value = myValue.Split('|')[0]; altText = myValue.Split('|')[1]; } }

at the beginning of Page_Load method assign this variable:
protected void Page_Load(object sender, EventArgs e) { txtAlt.Text = altText;

...
 
You can also modify a dialog to accept only images as media files for this selector:
mediaConfig.SelectableContent = SelectableContentEnum.OnlyImages;
 
4. In the CMS Site Manager -> Development -> Form controls, create a new Form Control -> Media Image Selection
 
5. Add a new field to your document type with this new control, the alternative text will then be then parsed automatically and you will be able to work with this field in transformations.


Applies to: Kentico CMS 7.x
Share this article on   LinkedIn

Martin Danko

Let me guide you on your journey of success. I will show you how to optimize your content production with Kentico Draft. Check out www.kenticocloud.com