There is a simple solution and a bit more advanced solution.
Simple:
- Go to for form controls
- New Form Control, call it - MyTagSelector
- Inherit From Existing - pick tag selector
- Set available for widgets and other settings
- Go to Properties - there is only one - TagGroupId
- Set default value to whatever group id you need (
select TagGroupID, TagGroupDisplayName from CMS_TagGroup
)
- Now go to you widget and use MyTagSelector instead of Tag Selector
Advanced:
You need to take a look at the code and debug the tag selector form control \CMSModules\Content\FormControls\Tags\TagSelector.ascx.cs
Clone it (copy/paste the code) as CustomTagSelector and customized for you needs. Then use it in your widget the same way instead of original one.
The code is pretty straight forward - You need to set correct GroupId. If it is 0 - selector is disabled.
protected void Page_PreRender(object sender, EventArgs e)
{
if (GroupId == 0)
{
Enabled = false;
btnSelect.ToolTip = ResHelper.GetString("tags.tagsselector.assigntaggroup");
txtTags.ToolTip = ResHelper.GetString("tags.tagsselector.assigntaggroup");
}
// Enable / Disable control
txtTags.Enabled = Enabled;
btnSelect.Enabled = Enabled;
if (Enabled)
{
autoComplete.ContextKey = GroupId.ToString();
}
}
Where property GroupId is defined like this:
public int GroupId
{
get
{
int mGroupId = ValidationHelper.GetInteger(GetValue("TagGroupID"), 0);
if ((mGroupId == 0) && (Form != null))
{
TreeNode node = (TreeNode)Form.EditedObject;
// When inserting new document
if (Form.IsInsertMode)
{
var parent = Form.ParentObject as TreeNode;
if (parent != null)
{
// Get path and groupID of the parent node
mGroupId = parent.DocumentTagGroupID;
// If nothing found try get inherited value
if (mGroupId == 0)
{
mGroupId = ValidationHelper.GetInteger(parent.GetInheritedValue("DocumentTagGroupID", false), 0);
}
}
}
// When editing existing document
else if (node != null)
{
// Get path and groupID of the parent node
mGroupId = node.DocumentTagGroupID;
// If nothing found try get inherited value
if (mGroupId == 0)
{
mGroupId = ValidationHelper.GetInteger(node.GetInheritedValue("DocumentTagGroupID", false), 0);
}
}
}
return mGroupId;
}
set
{
SetValue("TagGroupID", value);
}
}
My suggestion - clone it, so you always have the original code. You can add a new field to your widget call it tag group, so you will have something like this.
Make it as drop down based on query like
select TagGroupID, TagGroupDisplayName from CMS_TagGroup where ...
So you always have a first group selected by default.
And then you can access TagGroup value inside your ClonedTagSelector, i.e.
public int GroupId
{
get
{
if (Form.FieldControls["TagGroup"] != null)
{
if (ValidationHelper.GetString(Form.FieldControls["TagGroup"].Value,"") != "")
{
return ValidationHelper.GetInteger(Form.FieldControls["TagGroup"].Value, 0);
}
}
...
P.S. I cant really dig into details here, but it should give you an idea how to approach all this.