#4477 - Changed the saved value from the radio button list to the text value. + Migration of old values

This commit is contained in:
Bjarke Berg
2019-02-08 09:45:36 +01:00
parent 73ce546f0c
commit 065cc69f51
6 changed files with 115 additions and 5 deletions

View File

@@ -127,6 +127,7 @@ namespace Umbraco.Core.Migrations.Upgrade
To<ConvertRelatedLinksToMultiUrlPicker>("{ED28B66A-E248-4D94-8CDB-9BDF574023F0}");
To<UpdatePickerIntegerValuesToUdi>("{38C809D5-6C34-426B-9BEA-EFD39162595C}");
To<RenameUmbracoDomainsTable>("{6017F044-8E70-4E10-B2A3-336949692ADD}");
To<RadioButtonPropertyEditorsMigration>("{940FD19A-00A8-4D5C-B8FF-939143585726}");
//FINAL

View File

@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Dtos;
using Umbraco.Core.PropertyEditors;
namespace Umbraco.Core.Migrations.Upgrade.V_8_0_0
{
public class RadioButtonPropertyEditorsMigration : MigrationBase
{
public RadioButtonPropertyEditorsMigration(IMigrationContext context)
: base(context)
{
}
public override void Migrate()
{
//need to convert the old drop down data types to use the new one
var dataTypes = Database.Fetch<DataTypeDto>(Sql()
.Select<DataTypeDto>()
.From<DataTypeDto>()
.Where<DataTypeDto>(x => x.EditorAlias == "Umbraco.RadioButtonList"));
foreach (var dataType in dataTypes)
{
ValueListConfiguration config;
if (!dataType.Configuration.IsNullOrWhiteSpace())
{
// parse configuration, and update everything accordingly
try
{
config = (ValueListConfiguration) new ValueListConfigurationEditor().FromDatabase(
dataType.Configuration);
}
catch (Exception ex)
{
Logger.Error<DropDownPropertyEditorsMigration>(
ex,
"Invalid drop down configuration detected: \"{Configuration}\", cannot convert editor, values will be cleared",
dataType.Configuration);
continue;
}
// get property data dtos
var propertyDataDtos = Database.Fetch<PropertyDataDto>(Sql()
.Select<PropertyDataDto>()
.From<PropertyDataDto>()
.InnerJoin<PropertyTypeDto>()
.On<PropertyTypeDto, PropertyDataDto>((pt, pd) => pt.Id == pd.PropertyTypeId)
.InnerJoin<DataTypeDto>()
.On<DataTypeDto, PropertyTypeDto>((dt, pt) => dt.NodeId == pt.DataTypeId)
.Where<PropertyTypeDto>(x => x.DataTypeId == dataType.NodeId));
// update dtos
var updatedDtos = propertyDataDtos.Where(x => UpdatePropertyDataDto(x, config));
// persist changes
foreach (var propertyDataDto in updatedDtos) Database.Update(propertyDataDto);
}
}
}
private bool UpdatePropertyDataDto(PropertyDataDto propData, ValueListConfiguration config)
{
//Get the INT ids stored for this property/drop down
int[] ids = null;
if (propData.IntegerValue.HasValue) ids = new[] {propData.IntegerValue.Value};
//if there are INT ids, convert them to values based on the configuration
if (ids == null || ids.Length <= 0) return false;
//map the ids to values
var values = new List<string>();
var canConvert = true;
foreach (var id in ids)
{
var val = config.Items.FirstOrDefault(x => x.Id == id);
if (val != null)
values.Add(val.Value);
else
{
Logger.Warn<DropDownPropertyEditorsMigration>(
"Could not find associated data type configuration for stored Id {DataTypeId}", id);
canConvert = false;
}
}
if (!canConvert) return false;
propData.VarcharValue = string.Join(",", values);
propData.TextValue = null;
propData.IntegerValue = null;
return true;
}
private class ValueListConfigurationEditor : ConfigurationEditor<ValueListConfiguration>
{
}
}
}

View File

@@ -10,14 +10,14 @@ namespace Umbraco.Core.PropertyEditors.ValueConverters
=> propertyType.EditorAlias.InvariantEquals(Constants.PropertyEditors.Aliases.RadioButtonList);
public override Type GetPropertyValueType(PublishedPropertyType propertyType)
=> typeof (int);
=> typeof (string);
public override PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType)
=> PropertyCacheLevel.Element;
public override object ConvertSourceToIntermediate(IPublishedElement owner, PublishedPropertyType propertyType, object source, bool preview)
{
var intAttempt = source.TryConvertTo<int>();
var intAttempt = source.TryConvertTo<string>();
if (intAttempt.Success)
return intAttempt.Result;

View File

@@ -52,6 +52,9 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Umbraco.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\Umbraco.Tests\bin\Debug\Umbraco.Web.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<!-- note: NuGet deals with transitive references now -->
@@ -385,6 +388,7 @@
<Compile Include="Migrations\Upgrade\V_8_0_0\PropertyEditorsMigration.cs" />
<Compile Include="Migrations\Upgrade\V_8_0_0\RefactorMacroColumns.cs" />
<Compile Include="Migrations\Upgrade\V_8_0_0\RefactorVariantsModel.cs" />
<Compile Include="Migrations\Upgrade\V_8_0_0\RadioButtonPropertyEditorsMigration.cs" />
<Compile Include="Migrations\Upgrade\V_8_0_0\RenameUmbracoDomainsTable.cs" />
<Compile Include="Migrations\Upgrade\V_8_0_0\SuperZero.cs" />
<Compile Include="Migrations\Upgrade\V_8_0_0\TablesForScheduledPublishing.cs" />

View File

@@ -3,10 +3,10 @@
<li ng-repeat="item in configItems track by item.id">
<label class="radio">
<input type="radio" name="{{htmlId}}"
value="{{item.id}}"
value="{{item.value}}"
ng-model="model.value" />
{{item.value}}
</label>
</li>
</ul>
</div>
</div>

View File

@@ -8,7 +8,7 @@ namespace Umbraco.Web.PropertyEditors
/// <summary>
/// A property editor to allow the individual selection of pre-defined items.
/// </summary>
[DataEditor(Constants.PropertyEditors.Aliases.RadioButtonList, "Radio button list", "radiobuttons", ValueType = ValueTypes.Integer, Group="lists", Icon="icon-target")]
[DataEditor(Constants.PropertyEditors.Aliases.RadioButtonList, "Radio button list", "radiobuttons", ValueType = ValueTypes.String, Group="lists", Icon="icon-target")]
public class RadioButtonsPropertyEditor : DataEditor
{
private readonly ILocalizedTextService _textService;