@@ -12,10 +12,10 @@ namespace Umbraco.Docs.Samples.Web.Property_Editors_Add_Values
12
12
{
13
13
public class CreateImageCropperValuesController : UmbracoApiController
14
14
{
15
- private IContentService _contentService ;
16
- private IMediaService _mediaService ;
17
- private MediaUrlGeneratorCollection _mediaUrlGeneratorCollection ;
18
- private IPublishedSnapshotAccessor _publishedSnapshotAccessor ;
15
+ private readonly IContentService _contentService ;
16
+ private readonly IMediaService _mediaService ;
17
+ private readonly MediaUrlGeneratorCollection _mediaUrlGeneratorCollection ;
18
+ private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor ;
19
19
20
20
21
21
public CreateImageCropperValuesController ( IContentService contentService , IMediaService mediaService , MediaUrlGeneratorCollection mediaUrlGeneratorCollection , IPublishedSnapshotAccessor publishedSnapshotAccessor )
@@ -48,10 +48,16 @@ public ActionResult<string> CreateImageCropperValues()
48
48
// Serialize the image cropper value
49
49
var cropperValue = JsonConvert . SerializeObject ( cropper ) ;
50
50
51
+ var testCropperAlias = Product . GetModelPropertyType ( _publishedSnapshotAccessor , x => x . TestCropper ) ? . Alias ;
52
+
51
53
// Set the value of the property with alias 'cropper'
52
- content . SetValue ( "testCropper" , cropperValue , "en-US" ) ;
54
+ if ( content != null && ! string . IsNullOrWhiteSpace ( testCropperAlias ) )
55
+ {
56
+ content . SetValue ( "testCropper" , cropperValue , "en-US" ) ;
53
57
54
- content . SetValue ( Product . GetModelPropertyType ( _publishedSnapshotAccessor , x => x . TestCropper ) . Alias , cropperValue , "en-US" ) ;
58
+ content . SetValue ( testCropperAlias , cropperValue , "en-US" ) ;
59
+ }
60
+
55
61
56
62
return _contentService . Save ( content ) . Success . ToString ( ) ;
57
63
}
@@ -64,10 +70,21 @@ internal Dictionary<string, string> GetCropUrls(IPublishedContent image)
64
70
if ( image . HasValue ( "umbracoFile" ) )
65
71
{
66
72
var imageCropper = image . Value < ImageCropperValue > ( "umbracoFile" ) ;
67
- foreach ( var crop in imageCropper . Crops )
73
+
74
+ if ( imageCropper != null && imageCropper . Crops != null )
68
75
{
69
- //Get the cropped URL and add it to the dictionary that I will return
70
- cropUrls . Add ( crop . Alias , image . GetCropUrl ( crop . Alias ) ) ;
76
+ foreach ( var crop in imageCropper . Crops )
77
+ {
78
+ //Get the cropped URL and add it to the dictionary that I will return
79
+ if ( ! string . IsNullOrWhiteSpace ( crop . Alias ) )
80
+ {
81
+ var cropUrl = image . GetCropUrl ( crop . Alias ) ;
82
+ if ( ! string . IsNullOrEmpty ( cropUrl ) )
83
+ {
84
+ cropUrls . Add ( crop . Alias , cropUrl ) ;
85
+ }
86
+ }
87
+ }
71
88
}
72
89
}
73
90
0 commit comments