Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting old Umbraco API Controller [Block Grid Editor] #6510

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ If you are using ModelsBuilder, you can make the property rendering strongly typ

Using the default rendering together with your layout stylesheet will provide what you need for rendering the layout.

If you like to use the Default Layout Stylesheet, you must copy the stylesheet to your frontend. You can download the default layout stylesheet from the link within the DataType, we recommend putting the file in the css folder, example: `wwwroot/css/umbraco-blockgridlayout.css`.
If you like to use the Default Layout Stylesheet, you must copy the stylesheet to your frontend. You can download the default layout stylesheet from the link within the DataType, we recommend putting the file in the `css` folder, example: `wwwroot/css/umbraco-blockgridlayout.css`.

```csharp
<link rel="stylesheet" href="@Url.Content("~/css/blockgridlayout.css")" />
Expand Down Expand Up @@ -366,7 +366,7 @@ To make additions or overwrite parts of the default layout stylesheet, import th
@import 'css/umbblockgridlayout.css';
```

You need to copy the Default Layout Stylesheet to your frontend. You can download the default layout stylesheet from the link within the DataType, we recommend putting the file in the css folder, example: `wwwroot/css/umbraco-blockgridlayout.css`.
You need to copy the Default Layout Stylesheet to your frontend. You can download the default layout stylesheet from the link within the DataType, we recommend putting the file in the `css` folder, example: `wwwroot/css/umbraco-blockgridlayout.css`.

### Write a new Layout Stylesheet

Expand Down Expand Up @@ -462,7 +462,7 @@ Building Custom Views for Block representations in Backoffice is based on the sa

In this example, we will be creating content programmatically for a "spot" Blocks in a Block Grid.

1. On a document type add a property called **blockGrid**.
1. On a Document Type add a property called **blockGrid**.
2. Then add as editor **Block Grid**.
3. In the Block Grid add a new block and click to **Create new Element Type**
4. Name this element type **spotElement** with the following properties:
Expand Down Expand Up @@ -610,11 +610,10 @@ public class BlockGridLayoutItem
// this represents an item in the block grid content or settings data collection
public class BlockGridElementData
{
public BlockGridElementData(Guid contentTypeKey, Udi udi, Dictionary<string, object> data)
public BlockGridElementData(Guid contentTypeKey, Udi udi)
{
ContentTypeKey = contentTypeKey;
Udi = udi;
Data = data;
}

[JsonPropertyName("contentTypeKey")]
Expand All @@ -624,17 +623,13 @@ public class BlockGridElementData
public Udi Udi { get; }

[JsonExtensionData]
public Dictionary<string, object> Data { get; }
public Dictionary<string, object>? Data { get; }
}
```
{% endcode %}

9. By injecting [ContentService](https://apidocs.umbraco.com/v14/csharp/api/Umbraco.Cms.Core.Services.ContentService.html) and [ContentTypeService](https://apidocs.umbraco.com/v14/csharp/api/Umbraco.Cms.Core.Services.ContentTypeService.html) into an API controller, we can transform the raw data into Block Grid JSON. It can then be saved to the target content item. Create a class called **BlockGridTestController.cs** containing the following:

{% hint style="warning" %}
The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15.
{% endhint %}

{% code title="BlockGridTestController.cs" lineNumbers="true" %}
```csharp
using Microsoft.AspNetCore.Mvc;
Expand All @@ -643,11 +638,12 @@ using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Controllers;

namespace My.Site.Controllers;

public class BlockGridTestController : UmbracoApiController
[ApiController]
[Route("/umbraco/api/blockgridtest")]
public class BlockGridTestController : Controller
{
private readonly IContentService _contentService;
private readonly IContentTypeService _contentTypeService;
Expand All @@ -661,11 +657,11 @@ public class BlockGridTestController : UmbracoApiController
}

// POST: /umbraco/api/blockgridtest/create
[HttpPost]
[HttpPost("create")]
public ActionResult Create()
{
// get the item content to modify
IContent? content = _contentService.GetById(1203);
IContent? content = _contentService.GetById(Guid.Parse("efba7b97-91b6-4ddf-b2cc-eef89ff48c3b"));
if (content == null)
{
return NotFound("Could not find the content item to modify");
Expand Down Expand Up @@ -700,17 +696,23 @@ public class BlockGridTestController : UmbracoApiController
layoutItems.Add(new BlockGridLayoutItem(contentUdi, settingsUdi, data.ColumnSpan, data.RowSpan));

// create new content data
spotContentData.Add(new BlockGridElementData(spotContentType.Key, contentUdi, new Dictionary<string, object>
spotContentData.Add(new BlockGridElementData(spotContentType.Key, contentUdi)
{
{ "title", data.Title },
{ "text", data.Text },
}));
Data = new Dictionary<string, object>
{
{ "title", data.Title },
{ "text", data.Text },
}
});

// create new settings data
spotSettingsData.Add(new BlockGridElementData(spotSettingsType.Key, settingsUdi, new Dictionary<string, object>
spotSettingsData.Add(new BlockGridElementData(spotSettingsType.Key, settingsUdi)
{
{ "featured", data.Featured ? "1" : "0" },
}));
Data = new Dictionary<string, object>
{
{ "featured", data.Featured ? "1" : "0" },
}
});
}

// construct the block grid data from layout, content and settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ If you are using ModelsBuilder, you can make the property rendering strongly typ

Using the default rendering together with your layout stylesheet will provide what you need for rendering the layout.

If you like to use the Default Layout Stylesheet, you must copy the stylesheet to your frontend. You can download the default layout stylesheet from the link within the DataType, we recommend putting the file in the css folder, example: `wwwroot/css/umbraco-blockgridlayout.css`.
If you like to use the Default Layout Stylesheet, you must copy the stylesheet to your frontend. You can download the default layout stylesheet from the link within the DataType, we recommend putting the file in the `css` folder, example: `wwwroot/css/umbraco-blockgridlayout.css`.

```csharp
<link rel="stylesheet" href="@Url.Content("~/css/blockgridlayout.css")" />
Expand Down Expand Up @@ -366,7 +366,7 @@ To make additions or overwrite parts of the default layout stylesheet, import th
@import 'css/umbblockgridlayout.css';
```

You need to copy the Default Layout Stylesheet to your frontend. You can download the default layout stylesheet from the link within the DataType, we recommend putting the file in the css folder, example: `wwwroot/css/umbraco-blockgridlayout.css`.
You need to copy the Default Layout Stylesheet to your frontend. You can download the default layout stylesheet from the link within the DataType, we recommend putting the file in the `css` folder, example: `wwwroot/css/umbraco-blockgridlayout.css`.

### Write a new Layout Stylesheet

Expand Down Expand Up @@ -462,7 +462,7 @@ Building Custom Views for Block representations in Backoffice is based on the sa

In this example, we will be creating content programmatically for a "spot" Blocks in a Block Grid.

1. On a document type add a property called **blockGrid**.
1. On a Document Type add a property called **blockGrid**.
2. Then add as editor **Block Grid**.
3. In the Block Grid add a new block and click to **Create new Element Type**
4. Name this element type **spotElement** with the following properties:
Expand Down Expand Up @@ -610,11 +610,10 @@ public class BlockGridLayoutItem
// this represents an item in the block grid content or settings data collection
public class BlockGridElementData
{
public BlockGridElementData(Guid contentTypeKey, Udi udi, Dictionary<string, object> data)
public BlockGridElementData(Guid contentTypeKey, Udi udi)
{
ContentTypeKey = contentTypeKey;
Udi = udi;
Data = data;
}

[JsonPropertyName("contentTypeKey")]
Expand All @@ -624,17 +623,13 @@ public class BlockGridElementData
public Udi Udi { get; }

[JsonExtensionData]
public Dictionary<string, object> Data { get; }
public Dictionary<string, object>? Data { get; }
}
```
{% endcode %}

9. By injecting [ContentService](https://apidocs.umbraco.com/v14/csharp/api/Umbraco.Cms.Core.Services.ContentService.html) and [ContentTypeService](https://apidocs.umbraco.com/v14/csharp/api/Umbraco.Cms.Core.Services.ContentTypeService.html) into an API controller, we can transform the raw data into Block Grid JSON. It can then be saved to the target content item. Create a class called **BlockGridTestController.cs** containing the following:

{% hint style="warning" %}
The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15.
{% endhint %}

{% code title="BlockGridTestController.cs" lineNumbers="true" %}
```csharp
using Microsoft.AspNetCore.Mvc;
Expand All @@ -643,10 +638,11 @@ using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Controllers;

namespace My.Site.Controllers;

[ApiController]
[Route("/umbraco/api/blockgridtest")]
public class BlockGridTestController : UmbracoApiController
{
private readonly IContentService _contentService;
Expand All @@ -661,11 +657,11 @@ public class BlockGridTestController : UmbracoApiController
}

// POST: /umbraco/api/blockgridtest/create
[HttpPost]
[HttpPost("create")]
public ActionResult Create()
{
// get the item content to modify
IContent? content = _contentService.GetById(1203);
IContent? content = _contentService.GetById(Guid.Parse("efba7b97-91b6-4ddf-b2cc-eef89ff48c3b"));
if (content == null)
{
return NotFound("Could not find the content item to modify");
Expand Down Expand Up @@ -700,17 +696,23 @@ public class BlockGridTestController : UmbracoApiController
layoutItems.Add(new BlockGridLayoutItem(contentUdi, settingsUdi, data.ColumnSpan, data.RowSpan));

// create new content data
spotContentData.Add(new BlockGridElementData(spotContentType.Key, contentUdi, new Dictionary<string, object>
spotContentData.Add(new BlockGridElementData(spotContentType.Key, contentUdi)
{
{ "title", data.Title },
{ "text", data.Text },
}));
Data = new Dictionary<string, object>
{
{ "title", data.Title },
{ "text", data.Text },
}
});

// create new settings data
spotSettingsData.Add(new BlockGridElementData(spotSettingsType.Key, settingsUdi, new Dictionary<string, object>
spotSettingsData.Add(new BlockGridElementData(spotSettingsType.Key, settingsUdi)
{
{ "featured", data.Featured ? "1" : "0" },
}));
Data = new Dictionary<string, object>
{
{ "featured", data.Featured ? "1" : "0" },
}
});
}

// construct the block grid data from layout, content and settings
Expand Down
Loading