Umbraco 15 - Custom Block Grid - Displaying Data Type values from a settings element in CMS
Hi,
I have recently been assisted on displaying the slots/areas in a custom Typescript block, which uses Vite and Lit.
I have not made any progress from this again this week.
In Umbraco 13, using Angular in the controller of a html document, you can query the $scope and using code, like in the following example, get the values I had made up from the data types to help style up and control the custom block grid html.
Code in Angular Controller:
angular.module("umbraco").controller("twoColumnSectionController", function ($scope, mediaResource) {
// get the values from settings and add to scope to display values on the form
var bg = $scope.block.settingsData.backgroundColour;
if (typeof bg === 'undefined' || bg === null) {
$scope.bgcolor = "cmt-bg-none";
} else {
console.log("backgroundColour :: ", bg[0]);
$scope.bgcolor = "cmt-bg-" + bg[0];
}
var cp = $scope.block.settingsData.containerHPadding;
if (typeof cp === 'undefined' || cp === null) {
$scope.containerHPadding = "";
} else {
console.log("container padding :: ", cp[0]);
$scope.containerHPadding = cp[0];
}
var ai = $scope.block.settingsData.alignItems;
if (typeof ai === 'undefined' || ai === null) {
$scope.alignItems = "";
} else {
console.log("align items :: ", ai[0]);
$scope.alignItems = ai[0];
}
var theContainer = "container-xxl";
var cf = $scope.block.settingsData.containerFluid;
if (typeof cf === 'undefined' || cf === null) {
$scope.theContainer = "container-xxl";
} else {
console.log("container class :: ", cf[0]);
if (parseInt(cf[0]) === 1) {
$scope.theContainer = "container-fluid";
}
if (parseInt(cf[0]) === 0) {
$scope.theContainer = theContainer;
}
}
});
This works a treat. The slots as well, can also be written as, for example:
<slot name=left"></slot><slot name="right"</slot>
Anyway, I have been assisted, with great thanks, on including the slots in the new .ts equivalent, but I am getting sometimes strange results or no results in getting block grid settings values and being able to use them in the same way in an Umbraco 15 block custom Grid.
Code of the typescript with the slots:
import type { UmbBlockEditorCustomViewElement } from '@umbraco-cms/backoffice/block-custom-view';
//import { UmbLitElement } from "@umbraco-cms/backoffice/lit-element";
import { html, customElement, LitElement, property, css, state } from '@umbraco-cms/backoffice/external/lit';
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
import type { UmbBlockDataType } from '@umbraco-cms/backoffice/block';
import { UmbBlockGridTypeModel } from '@umbraco-cms/backoffice/block-grid';
//import { UmbImagingRepository } from '@umbraco-cms/backoffice/imaging'; // Import the repository
//import type { UmbMediaPickerPropertyValue } from '@umbraco-cms/backoffice/media';
const elementName = "columns-66-custom-view";
// eslint-disable-next-line local-rules/enforce-umb-prefix-on-element-name
@customElement(elementName)
// eslint-disable-next-line local-rules/umb-class-prefix
export class Columns66
extends UmbElementMixin(LitElement)
implements UmbBlockEditorCustomViewElement
{
@property({ attribute: false })
content?: UmbBlockDataType;
@property({ attribute: false })
settings?: UmbBlockDataType;
_styleElement?: HTMLLinkElement;
@state()
rowId: Map<string, string> = new Map();
@state()
blockType?: UmbBlockGridTypeModel;
override render()
{
return html`
<div class="${this.settings?.rowId ? 'align-' + this.settings?.rowId : undefined}">
<h5>Row ID snippet</h5>
<p>Alignment: ${this.settings?.rowId}</p>
</div>
${this.renderSingleSettingWithType('hideSection', 'boolean')}
${this.renderSingleSettingWithType('containerFluid', 'boolean')}
${this.renderSingleSettingWithType('rowId', 'string')}
${this.renderSingleSettingWithType('rowBackgroundColour', 'string')}
${this.renderSingleSettingWithType('containerHPadding', 'string')}
${this.renderSingleSettingWithType('alignItems', 'string')}
<h5>My Custom View</h5>
<umb-block-grid-areas-container
.areas="${this.blockType?.areas ?? []}"
.areaGridColumns="${this.blockType?.areaGridColumns}">
</umb-block-grid-areas-container>
<h5>My Custom View - left</h5>
<umb-block-grid-entries
part="area"
class="umb-block-grid__area"
.areaKey="right"></umb-block-grid-entries>
<h5>My Custom View - right</h5>
<umb-block-grid-entries
part="area"
class="umb-block-grid__area"
.areaKey="right"></umb-block-grid-entries>
<umb-block-grid-entries
part="area"
class="umb-block-grid__area"
.areaKey=${this.blockType?.areas[0].key}
.layoutColumns=${this.blockType?.areas[0].columnSpan}></umb-block-grid-entries>
<umb-block-grid-entries
part="area"
class="umb-block-grid__area"
.areaKey=${this.blockType?.areas[1].key}
.layoutColumns=${this.blockType?.areas[1].columnSpan}></umb-block-grid-entries>
`;
}
private renderSingleSettingWithType(key: string, type: 'string' | 'boolean' | 'integer')
{
if (!this.settings) return html`<p>${key} is: undefined</p><p>${key} is value: undefined</p>`;
let value = this.settings[key];
console.log("settings key and value :: ", this.settings, key, value);
switch (type)
{
case 'boolean':
value = (value as boolean) ? 'true' : 'false';
break;
case 'integer':
value = parseInt(value as string, 10);
break;
case 'string':
default:
value = (value as string)?.toString() ?? 'undefined';
break;
}
return html`
<p><strong>${key} is value: ${value}</strong></p>
`;
}
//private renderSingleSetting(key: string)
//{
// if (!this.settings) return html`<p>${key} is: undefined</p><p>${key} is value: undefined</p>`;
// const value = this.settings[key];
// return html`
// <p>${key} is: ${value ?? 'undefined'}</p>
// <p>${key} is value: ${value}</p>
//`;
//}
//private renderSettings()
//{
// if (!this.settings) return html``;
// return Object.keys(this.settings ?? {}).map(key => html`
// <p>${key} is: ${this.settings?.[key] ?? 'undefined'}</p>
// <p>${key} is value: ${this.settings?.[key]}</p>
//`);
//}
static styles = [
css`
a {
display: block;
color: inherit;
text-decoration: inherit;
border: 1px solid transparent;
border-radius: 2px;
}
a:hover {
border-color: var(--uui-color-interactive-emphasis, #3544b1);
}
.preview-alert {
background-color: var(--uui-color-danger, #f0ac00);
border: 1px solid transparent;
border-radius: 0;
margin-bottom: 20px;
padding: 8px 35px 8px 14px;
position: relative;
&, a, h4 {
color: #fff;
}
pre {
white-space: normal;
}
}
.preview-alert-warning {
background-color: var(--uui-color-danger, #f0ac00);
border-color: transparent;
color: #fff;
}
.preview-alert-info {
background-color: var(--uui-color-default, #3544b1);
border-color: transparent;
color: #fff;
}
.preview-alert-danger, .preview-alert-error {
background-color: var(--uui-color-danger, #f0ac00);
border-color: transparent;
color: #fff;
}
`
]
}
export default Columns66;
declare global
{
interface HTMLElementTagNameMap
{
[elementName]: Columns66;
}
}
This is what is rendered. I use the alias of the settings value, but the same value is repeated for each!
I thought I would put in the Feature block on its own, and not within the Layout block to see if that would render the correct values, but no, that still repeated the first value I put in:
Thank you again for any feedback.
I think I need to start learning Typescript from the start to begin to understand this properly. Check out the new Backend Typescript Umbraco code to see how it is put together. It is a shame that the Umbraco documentation on this is as yet very shallow, in Our Umbraco, with no thorough how to to display rich text, strings, text areas and Umbraco url pickers in custom Views.
I also had an issue in my manifest, when, if I did not put in the forContentTypeAlias, the first block declared in the manifest would be applied to all the block grids.
So that has made me think that also maybe I simply do know know how to put in the name or alias to get the value - Just for this block grid, maybe there is required a block grid name - I wondered if the Uid, or Key of the block grid item being displayed was needed somewhere in the code that grabs the values so it know what to grab, not just the first value: grid Key or Uid or something > property alias/name to get that value for this item in the grid.
I am waffling a bit, but I hope you see what I mean. I may put in to their errors and bugs list myself. They have been very good in the past responding and fixing bugs and issues I have discovered.
I have taken a look at the codebase for Umbraco and forked a local copy with GIT.. This issue is still present in the 5.1.2 Contrib branch. I have pinpointed the issue to the following code for 'settingsValues' in 'block-entry-context.ts' at line 234.
I am not very familiar with the Umbraco codebase, or the process for proposing code changes, so I haven't been able perform rigorous testing. I've posted this comment to the issue I raised and I hope someone with more experience will pick this up, test and fix the error.
It is a major issue for anyone who wants to make use of the custom edit views in block grid, and it is impeding our attempts to upgrade our Umbraco sites to the latest version.
If you let me know what is the id of the issue you posted John, I will add a further comment to mine with a reference to the issue you post.
Kind regards,
Hi Alfreed and John,
I have upgraded to Umbraco 15.2.1 and looked at this issue to see if it has been resolved, and it looks like it has NOT been resolved.
Umbraco 15 - Custom Block Grid - Displaying Data Type values from a settings element in CMS
Hi, I have recently been assisted on displaying the slots/areas in a custom Typescript block, which uses Vite and Lit.
I have not made any progress from this again this week.
In Umbraco 13, using Angular in the controller of a html document, you can query the $scope and using code, like in the following example, get the values I had made up from the data types to help style up and control the custom block grid html.
Code in Angular Controller:
Code in html:
This works a treat. The slots as well, can also be written as, for example:
Anyway, I have been assisted, with great thanks, on including the slots in the new .ts equivalent, but I am getting sometimes strange results or no results in getting block grid settings values and being able to use them in the same way in an Umbraco 15 block custom Grid.
Code of the typescript with the slots:
Code of the Typescript for the feature:
This is what is rendered. I use the alias of the settings value, but the same value is repeated for each!
I thought I would put in the Feature block on its own, and not within the Layout block to see if that would render the correct values, but no, that still repeated the first value I put in:
Thank you again for any feedback.
I think I need to start learning Typescript from the start to begin to understand this properly. Check out the new Backend Typescript Umbraco code to see how it is put together. It is a shame that the Umbraco documentation on this is as yet very shallow, in Our Umbraco, with no thorough how to to display rich text, strings, text areas and Umbraco url pickers in custom Views.
Kind regards,
Pete
Hi peter,
I think that is an issue, all the property values are inherited from the first one. Will have to look into this for the root cause though.
Hi Peter/Alfreed,
I also came to this conclusion while working on a similar project. I have raised this as an issue on the Umbraco GIT site, but as yet no takers.
https://github.com/umbraco/Umbraco-CMS/issues/17989
regards
John
Hi Afreed/John, Thank you for your feedback.
I have tried other methods to get the actual values for the settings alias / parameter name or alias:
I also had an issue in my manifest, when, if I did not put in the forContentTypeAlias, the first block declared in the manifest would be applied to all the block grids.
So that has made me think that also maybe I simply do know know how to put in the name or alias to get the value - Just for this block grid, maybe there is required a block grid name - I wondered if the Uid, or Key of the block grid item being displayed was needed somewhere in the code that grabs the values so it know what to grab, not just the first value: grid Key or Uid or something > property alias/name to get that value for this item in the grid.
I am waffling a bit, but I hope you see what I mean. I may put in to their errors and bugs list myself. They have been very good in the past responding and fixing bugs and issues I have discovered.
Kind regards,
Pete
Hi Peter/Alfreed,
I have taken a look at the codebase for Umbraco and forked a local copy with GIT.. This issue is still present in the 5.1.2 Contrib branch. I have pinpointed the issue to the following code for 'settingsValues' in 'block-entry-context.ts' at line 234.
By replacing this code with the corresponding code pattern for the 'contentValues' method the problem is resolved. I.e.
I am not very familiar with the Umbraco codebase, or the process for proposing code changes, so I haven't been able perform rigorous testing. I've posted this comment to the issue I raised and I hope someone with more experience will pick this up, test and fix the error.
It is a major issue for anyone who wants to make use of the custom edit views in block grid, and it is impeding our attempts to upgrade our Umbraco sites to the latest version.
regards
John
Hi John,
Thank you for looking in to this and delving in to the solution.
I added a bug issue in the Umbraco Git repository over the weekend. Actually your investigations will help with my understanding of the new code base.
The issue I posted is here: https://github.com/umbraco/umbraco-cms/issues/18024
If you let me know what is the id of the issue you posted John, I will add a further comment to mine with a reference to the issue you post. Kind regards,
Pete
Hi Alfreed/John,
And apologies John, half asleep on a Sunday evening.
I see that you already posted the link to your issue entry earlier.
Kind regards,
Pete
Hi Alfreed and John, I have upgraded to Umbraco 15.2.1 and looked at this issue to see if it has been resolved, and it looks like it has NOT been resolved.
I have added a new issue: Git hub issue: Accessing settings in a Block custom view preview still always returns the first settings value
I have asked them to re-visit this and see what can be done. If in both your experience's this has been resolved, please can you advise.
Kind regards,
Pete
is working on a reply...